标签:
题意:就是……食物链嘛orz
解法:并查集+向量偏移。一!定!不!要!多!组!输!入!!!
代码:
难看死了
#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) { //cout << "case1: " << delta[b] << " " << delta[c] << endl; if(delta[b] != delta[c]) { ans++; //cout << "wrong: " << a << " " << b << " " << c << endl; } } 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++; //cout << "wrong: " << a << " " << b << " " << c << endl; } } else { father[d] = e; delta[d] = (delta[c] - delta[b] + 4) % 3; } } } printf("%d\n", ans); return 0; }
标签:
原文地址:http://www.cnblogs.com/Apro/p/4504564.html