标签:
食物链 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
100 7 1 101 1 2 1 2 2 2 3 2 3 3 1 1 3 2 3 1 1 5 5
Sample Output
3
#include<iostream> #include<cstdio> using namespace std; #define N 50005 int n, k, d, x, y, f[N], r[N]; int found(int q) { int k = f[q]; if(f[q] != q) { f[q] = found(f[q]); r[q] = (r[k] + r[q]) % 3; // 食物链3种状态,0与根结点同类,1吃,2被吃 } return f[q]; } int main() { scanf("%d%d", &n, &k); int nx, ny, cou = 0; for(int i = 0; i <= n; i++) { f[i] = i; r[i] = 0; } while(k--) { scanf("%d%d%d", &d, &x, &y); nx = found(x), ny = found(y); if(x > n || y > n || (x == y && d == 2)) // 睁眼说瞎话 cou++; else if(nx == ny && (r[y]+d-1) % 3 != r[x]) // 如果之前有关系,当前关系和已知根结点关系矛盾,就不对,cou++ cou++; else if(nx != ny) { f[nx] = ny; r[nx] = ( d-1 - r[x]+r[y] + 3) % 3; // 之前没关系,把当前根结点放到一棵树上,有关系了。r【nx】根据r[x]x和y的关系 r[y]判断 } } printf("%d\n", cou); return 0; }
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4674632.html