题意:就是……食物链嘛orz
解法:并查集+向量偏移。一!定!不!要!多!组!输!入!!!
偏移量0表示同类,1表示吃,2表示被吃
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #define LL long long using namespace std; int father[50005], delta[50005]; void init() { for(int i = 0; i < 50005; i++) father[i] = i; memset(delta, 0, sizeof delta); } int Find(int a) { if(father[a] != a) { int tmp = Find(father[a]); delta[a] = (delta[a] + delta[father[a]]) % 3; father[a] = tmp; } return father[a]; } int main() { int n, k; scanf("%d%d", &n, &k); init(); int ans = 0; for(int i = 0; i < k; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); if(b > n || c > n) { ans++; continue; } int d, e; d = Find(b), e = Find(c); if(a == 1) { if(d == e) { if(delta[b] != delta[c]) ans++; } else { father[d] = e; delta[d] = (delta[c] - delta[b] + 3) % 3; } } else { if(d == e) { if(((delta[b] - delta[c] + 3) % 3) != 1) ans++; } else { father[d] = e; delta[d] = (delta[c] - delta[b] + 4) % 3; } } } printf("%d ", ans); return 0; }