92分
查看原帖
92分
55377
封禁用户楼主2017/8/31 00:18
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
const int maxn = 1E5 + 10;
const int T = 24;
const int dx[8] = {0,1,0,-1,1,1,-1,-1};
const int dy[8] = {1,0,-1,0,1,-1,1,-1};
struct Point{
    int x,y; Point(){}
    Point(int x,int y): x(x),y(y){}
    bool operator < (const Point &b) const
    {
        if (x < b.x) return 1;
        if (x > b.x) return 0;
        return y < b.y;
    }
}p[maxn],q[maxn*T];

int TT,n,m,c,tot,cnt,Cnt,dfs_clock,dfn[maxn*T],

    low[maxn*T],scc_num[maxn*T],bel[maxn];
bool flag,is_cut[maxn*T],vis[maxn*T],Vis[maxn];
map <Point,int> m1,m2;
vector <int> v0[maxn],v[maxn*T],Node[maxn];
void Dfs0(int x)
{
    bel[x] = Cnt;
    for (int i = 0; i < v0[x].size(); i++)
    {
        int to = v0[x][i];
        if (Vis[to]) continue;
        Vis[to] = 1; Dfs0(to);
    }
}
void Dfs(int x,int from)
{
    int son = 0;
    low[x] = dfn[x] = ++dfs_clock; scc_num[x] = cnt;
    for (int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        if (to == from) continue;
        if (!vis[to]) 
        {
            vis[to] = 1; ++son; Dfs(to,x);
            low[x] = min(low[x],low[to]);
            if (low[to] >= dfn[x]) is_cut[x] = 1;
        }
        else low[x] = min(low[x],dfn[to]);
    }
    if (from == -1 && son == 1) is_cut[x] = 0;
}
bool Check(int x)
{
    for (int i = 0; i < 8; i++)
    {
        int xx = q[x].x + dx[i];
        int yy = q[x].y + dy[i];
        if (m1.count(Point(xx,yy))) return 1;
    }
    return 0;
}
bool Not_Connect()
{
    for (int i = 1; i <= c; i++)
        for (int j = 0; j < 8; j++)
        {
            Point g = Point(p[i].x + dx[j],p[i].y + dy[j]);
            if (m1.count(g)) v0[i].push_back(m1[g]);
        }
    for (int i = 1; i <= c; i++)
        if (!Vis[i]) Vis[i] = 1,++Cnt,Dfs0(i);
    for (int i = 1; i <= c; i++)
        for (int x = -2; x <= 2; x++)
            for (int y = -2; y <= 2; y++)
            {
                Point g = Point(p[i].x + x,p[i].y + y);
                if (g.x <= 0 || g.x > n || g.y <= 0 || g.y > m) continue;
                if (!m1.count(g) && !m2.count(g))
                {
                    q[++tot] = g; m2[g] = tot;
                    Node[bel[i]].push_back(tot);
                }
            }
    for (int i = 1; i <= tot; i++)
        for (int j = 0; j < 4; j++)
        {
            Point g = Point(q[i].x + dx[j],q[i].y + dy[j]);
            if (m2.count(g)) v[i].push_back(m2[g]);
        }
    for (int i = 1; i <= tot; i++)
        if (!vis[i]) vis[i] = 1,++cnt,Dfs(i,-1);
    for (int i = 1; i <= tot; i++)
        if (is_cut[i] && Check(i)) flag = 1;
    for (int i = 1; i <= Cnt; i++)
    {
        int Num = -1;
        for (int j = 0; j < Node[i].size(); j++)
        {
            int now = Node[i][j];
            if (Num == -1) Num = scc_num[now];
            else if (scc_num[now] != Num) return 1;
        }
    }
    return 0;
}
int getint()
{
    char ch = getchar(); int ret = 0;
    while (ch < '0' || '9' < ch) ch = getchar();
    while ('0' <= ch && ch <= '9')
        ret = ret*10 + ch - '0',ch = getchar();
    return ret;
}
void Clear()
{
    for (int i = 1; i <= tot; i++)
        v[i].clear(),vis[i] = is_cut[i] = 0;
    for (int i = 1; i <= c; i++)
        Node[i].clear(),v0[i].clear(),Vis[i] = 0;
    flag = cnt = tot = dfs_clock = Cnt = 0;
    m1.clear(); m2.clear();
}
void Work()
{
    n = getint(); m = getint(); c = getint();
    for (int i = 1; i <= c; i++)
    {
        int x = getint(),y = getint();
        p[i] = Point(x,y); m1[p[i]] = i;
    }
    if (1LL*n*m - c <= 1) {puts("-1"); Clear(); return;}
    if (Not_Connect()) {puts("0"); Clear(); return;}
    if (1LL*n*m - c == 2)
    {
        if (cnt == 1 || !c) puts("-1");
        else puts("0"); Clear(); return;
    }
    if (n == 1 || m == 1) flag = 1;
    if (flag) puts("1"); else puts("2"); Clear();
}
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif
    TT = getint(); while (TT--) Work();
    return 0;
}
2017/8/31 00:18
加载中...