HDU 5652 India and China Origins(并查集)

题意:

$N,M\le 5\times 10^2,N\times M的矩阵,0表示可通过,1不可通过$
$这个矩阵是连通的当且仅当,从第一行的任意一点出发可以到最后一行$
$给定Q\le N\times M次修改,将矩阵的0变1$
$问最早哪一次修改使得矩阵不连通$

分析:

$显然建立一个超源连到第一行,超汇连到最后一行$
$然后直接二分+bfs或者并查集判断连通性就做完了$
$其实可以倒着来想,先全部把1加进去,再1个1个删掉$
$这里要注意源汇的连通性哦,反正我觉得前者好写$
$时间复杂度O((n+m)logm)或者O(n+m)$

代码:

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
//
// Created by TaoSama on 2016-04-07
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 5e2 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
struct DSU {
int n, p[N * N], r[N * N];
void init(int _n) {
n = _n;
for(int i = 0; i < n; ++i) p[i] = i, r[i] = 0;
}
int find(int x) {
return p[x] = p[x] == x ? x : find(p[x]);
}
void unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return;
if(r[x] > r[y]) swap(x, y);
if(r[x] == r[y]) ++ r[y];
p[x] = y;
}
bool same(int x, int y) {
return find(x) == find(y);
}
} dsu;
int n, m, q;
char a[N][N];
pair<int, int> qs[N * N];
inline int ID(int x, int y) {
return x * m + y;
}
void process(int i, int j) {
static int d[][2] = { -1, 0, 0, -1, 1, 0, 0, 1};
if(a[i][j] == '1') return;
for(int k = 0; k < 4; ++k) {
int x = i + d[k][0], y = j + d[k][1];
if(y < 0 || y >= m) continue;
if(x < 0) dsu.unite(ID(i, j), n * m);
else if(x >= n) dsu.unite(ID(i, j), n * m + 1);
else if(a[x][y] == '0') dsu.unite(ID(i, j), ID(x, y));
}
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i) scanf("%s", a[i]);
scanf("%d", &q);
for(int i = 0; i < q; ++i) {
int x, y; scanf("%d%d", &x, &y);
qs[i] = {x, y};
a[x][y] = '1';
}
dsu.init(n * m + 2);
int s = n * m, t = s + 1;
for(int i = 0; i < m; ++i) {
if(a[0][i] == '0') dsu.unite(s, ID(0, i));
if(a[n - 1][i] == '0') dsu.unite(ID(n - 1, i), t);
}
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
process(i, j);
if(dsu.same(s, t)) {puts("-1"); continue;}
for(int i = q - 1; ~i; --i) {
int x = qs[i].first, y = qs[i].second;
a[x][y] = '0';
process(x, y);
if(dsu.same(s, t)) {
printf("%d\n", i + 1);
break;
}
}
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到