1042: Fight
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 26 Solved: 8
[Submit][Status][Web Board]
Description
如今给你一些信息,请你根据这些信息推断是否一定存在猪一样的队友。
Input
Output
Sample Input
3 3
1 2
2 3
1 3
4 2
1 2
3 4
Sample Output
YES
NO
HINT
a攻击b且b攻击c 则可判断 a,c同伙,若a攻击c则有矛盾,即必定存在误伤。
Source
解析:參见ACdream 1056 Bad Horse (种类并查集)
AC代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; int f[2 * maxn + 2]; int Find(int x){ return x == f[x] ?x : f[x] = Find(f[x]); } void unin(int x, int y){ x = Find(x); y = Find(y); if(x != y) f[x] = y; } int main(){ #ifdef sxk freopen("in.txt", "r", stdin); #endif // sxk int n, m, a, b; while(~scanf("%d%d", &n, &m)){ for(int i=1; i<=2 * maxn; i++) f[i] = i; int flag = 0; for(int i=0; i<m; i++){ scanf("%d%d", &a, &b); if(Find(a) == Find(b) || Find(a + maxn) == Find(b + maxn)) flag = 1; else{ unin(a, b + maxn); unin(a + maxn, b); } } puts(flag ?
"YES" : "NO"); } return 0; }