1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;
const int DIR[9][2] = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 0}, {0, 1}, {1, -1}, {1, 0}, {1, 1} };
inline int rin(int &r) { int b = 1, c = getchar(); r = 0; while ( c < '0' || '9' < c ) b = c == '-' ? -1 : b, c = getchar(); while ( '0' <= c && c <= '9' ) r = (r * 10) + (c ^ '0'), c = getchar(); return r *= b; }
const int N = 1e5 + 10, NP = N * 20, NE = N * 80; #define con(typ) const typ &
struct Graph { int head[NP], to[NE], nxt[NE], ncnt, np; void addEdge(con(int) u, con(int) v) { int p = ++ncnt, q = ++ncnt; to[p] = v, nxt[p] = head[u], head[u] = p; to[q] = u, nxt[q] = head[v], head[v] = q; } inline int operator [] (con(int) u) const {return head[u];} } gr;
struct SPNode { int x, y, tag; bool operator < (con(SPNode) p) const { if ( x == p.x ) return y < p.y; return x < p.x; } } nod[N * 9];
bool isans1; int ncas, n, m, nc, nnod, itmp, ndfn; int dfn[NP], low[NP]; bool ptag[NP]; int tmp[2][NP][3], ntmp[2]; pair<int, int> pos[N];
void dfs(con(int) u) { bool iscut = false; int cntson = 0; dfn[u] = low[u] = ++ndfn; for (int it = gr[u]; it; it = gr.nxt[it]) { int v = gr.to[it]; if ( dfn[v] ) low[u] = min(low[u], dfn[v]); else { cntson++; dfs(v); low[u] = min(low[u], low[v]); if ( low[v] >= dfn[u] ) if ( ptag[u] ) iscut = true; } } if ( iscut ) isans1 |= (u != 1 || cntson > 1); } void newTemp() {itmp ^= 1, ntmp[itmp] = 0;} void setTemp(con(int) l, con(int) r, con(bool) tag) { int p = ++gr.np; if ( p >= NP ) exit(0); dfn[p] = 0, gr.head[p] = 0; ptag[p] = tag; tmp[itmp][++ntmp[itmp]][0] = l, tmp[itmp][ntmp[itmp]][1] = r; tmp[itmp][ntmp[itmp]][2] = p; } void arrangeTemp() { for (int i = 1; i < ntmp[itmp]; i++) if ( tmp[itmp][i][1] + 1 == tmp[itmp][i + 1][0] ) gr.addEdge(tmp[itmp][i][2], tmp[itmp][i + 1][2]); if ( !ntmp[0] || !ntmp[1] ) return; for (int i = 1, jl = 1, jr = 0; i <= ntmp[0]; i++) { while ( jr < ntmp[1] && tmp[1][jr + 1][0] <= tmp[0][i][1] ) jr++; while ( jl <= jr && tmp[1][jl][1] < tmp[0][i][0] ) jl++; for (int j = jl; j <= jr; j++) gr.addEdge(tmp[0][i][2], tmp[1][j][2]); } }
bool bf[N];
bool isNeg() { if ( 1ll * n * m - nc > 2 ) return false; if ( 1ll * n * m - nc <= 1 ) return true; #define id(x, y) ((x) * m - m + (y)) for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) bf[id(i, j)] = false; for (int i = 1; i <= nc; i++) bf[id(pos[i].first, pos[i].second)] = true; const int DIR[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if ( !bf[id(i, j)] ) for (int k = 0; k < 4; k++) { int x = i + DIR[k][0], y = j + DIR[k][1]; if ( 1 <= x && x <= n && 1 <= y && y <= m && !bf[id(x, y)] ) return true; } return false; #undef id } int main() { rin(ncas); while ( ncas-- ) { ndfn = nnod = 0; gr.np = gr.ncnt = 0; itmp = 0; isans1 = false; rin(n), rin(m), rin(nc); for (int i = 1; i <= nc; i++) rin(pos[i].first), rin(pos[i].second); if ( isNeg() ) {printf("-1\n"); continue;} sort(pos + 1, pos + 1 + nc); if ( m == 1 ) { int cnti = 0; if ( nc ) { if ( pos[1].first > 1 ) cnti++; for (int i = 2; i <= nc; i++) if ( pos[i - 1].first + 1 <= pos[i].first - 1 ) cnti++; if ( pos[nc].first < n ) cnti++; } else cnti = 1; if ( cnti > 1 ) printf("0\n"); else printf("1\n"); continue; } if ( n == 1 ) { int cnti = 0; if ( nc ) { if ( pos[1].second > 1 ) cnti++; for (int i = 2; i <= nc; i++) if ( pos[i - 1].second + 1 <= pos[i].second - 1 ) cnti++; if ( pos[nc].second < m ) cnti++; } else cnti = 1; if ( cnti > 1 ) printf("0\n"); else printf("1\n"); continue; } for (int i = 1, p, q; i <= nc; i++) { p = pos[i].first, q = pos[i].second; for (int k = 0; k < 9; k++) { int x = p + DIR[k][0], y = q + DIR[k][1]; if ( 1 <= x && x <= n && 1 <= y && y <= m ) { nod[++nnod].x = x, nod[nnod].y = y, nod[nnod].tag = k == 4; } } } sort(nod + 1, nod + 1 + nnod); ntmp[0] = ntmp[1] = 0; if ( nod[1].x > 1 ) newTemp(), setTemp(1, m, false); int lasl = nod[1].x - 1; for (int i = 1; i <= nnod; i++) { if ( lasl < nod[i].x - 1 ) { newTemp(), setTemp(1, m, false); arrangeTemp(); } newTemp(); if ( nod[i].y > 1 ) setTemp(1, nod[i].y - 1, false); int toi = i; while ( toi < nnod && nod[toi + 1].x == nod[i].x ) toi++; for (int j = i; j <= toi; j++) { int toj = j; bool itag = nod[j].tag; while ( toj < toi && nod[toj + 1].y == nod[j].y ) itag |= nod[++toj].tag; if ( !itag) setTemp(nod[j].y, nod[j].y, true); if ( toj < toi && nod[j].y + 1 < nod[toj + 1].y ) setTemp(nod[j].y + 1, nod[toj + 1].y - 1, false); j = toj; } if ( nod[toi].y < m ) setTemp(nod[toi].y + 1, m, false); arrangeTemp(); lasl = nod[i].x, i = toi; } if ( lasl < n ) { newTemp(), setTemp(1, m, false); arrangeTemp(); } dfs(1); if ( ndfn < gr.np ) printf("0\n"); else if ( isans1 ) printf("1\n"); else printf("2\n"); } return 0; }
|