很像SCOI的游戏(其实基本就是一样)
首先我们可以推出一个性质,当且仅当某一个连通块中没有环存在输出NIE(无解的意思)
因为有环的话。。。就可以构造一棵基环外向树
所以做法就和SCOI的游戏很像了,并查集维护即可。
1 /************************************************************** 2 Problem: 1116 3 User: rausen 4 Language: C++ 5 Result: Accepted 6 Time:252 ms 7 Memory:1292 kb 8 ****************************************************************/ 9 10 #include <cstdio> 11 12 using namespace std; 13 const int N = 100005; 14 int n, m, fa[N]; 15 bool vis[N]; 16 17 inline int read(){ 18 int x = 0; 19 char ch = getchar(); 20 while (ch < '0' || ch > '9') 21 ch = getchar(); 22 while (ch >= '0' && ch <= '9'){ 23 x = x * 10 + ch - '0'; 24 ch = getchar(); 25 } 26 return x; 27 } 28 29 int find_fa(const int x){ 30 return x == fa[x] ? x : fa[x] = find_fa(fa[x]); 31 } 32 33 int main(){ 34 int i, x, y; 35 n = read(), m = read(); 36 for (i = 1; i <= n; ++i) 37 fa[i] = i; 38 for (i = 1; i <= m; ++i){ 39 x = find_fa(read()); 40 y = find_fa(read()); 41 if (x != y){ 42 fa[x] = y; 43 vis[y] |= vis[x]; 44 } else vis[x] = 1; 45 } 46 for (i = 1; i <= n; ++i) 47 if (!vis[find_fa(i)]){ 48 puts("NIE"); 49 return 0; 50 } 51 puts("TAK"); 52 return 0; 53 }
By Xs酱~ 转载请说明
博客地址:http://www.cnblogs.com/rausen