首先吐槽一下这什么破游戏啊。。。
网上的做法都是跑二分图匹配,当我知道有并查集做法的时候眼瞎了。。。
Orz BLADEVIL (←看这个题解吧。。。)
1 /************************************************************** 2 Problem: 1854 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:760 ms 7 Memory:5688 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 #include <algorithm> 12 13 using namespace std; 14 const int N = 1000001; 15 int n, fa[N]; 16 bool vis[N]; 17 18 inline int read(){ 19 int x = 0; 20 char ch = getchar(); 21 while (ch < '0' || ch > '9') 22 ch = getchar(); 23 while (ch >= '0' && ch <= '9'){ 24 x = x * 10 + ch - '0'; 25 ch = getchar(); 26 } 27 return x; 28 } 29 30 int find_fa(int x){ 31 return x == fa[x] ? x : fa[x] = find_fa(fa[x]); 32 } 33 34 inline void Union(int x, int y){ 35 if (x < y) swap(x, y); 36 vis[y] = 1; 37 fa[y] = x; 38 } 39 40 int main(){ 41 n = read(); 42 int i, x, y; 43 for (i = 1; i <= n + 1; ++i) 44 fa[i] = i; 45 for (i = 1; i <= n; ++i){ 46 x = read(), x = find_fa(x); 47 y = read(), y = find_fa(y); 48 if (x == y) vis[x] = 1; 49 else Union(x, y); 50 } 51 for (i = 1; i <= n + 1; ++i) 52 if (!vis[i]){ 53 printf("%d\n", i - 1); 54 break; 55 } 56 return 0; 57 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen