1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <cstdio> #include <cstring>
int father[50050]; int relation[50050];
int find_set(int i){ if(i == father[i]) return i; int t = father[i]; father[i] = find_set(father[i]); relation[i] = (relation[i] + relation[t]) % 3; return father[i]; }
void union_set(int d, int x, int y){ int fx = find_set(x); int fy = find_set(y); father[fy] = fx; relation[fy] = (3 - relation[y] + (d-1) + relation[x]) % 3; }
int main(){ memset(relation, 0, sizeof(relation)); int n, k, d, x, y, ans = 0; scanf("%d %d", &n, &k); for(int i = 1; i <= n; i++) father[i] = i; while(k-- > 0){ scanf("%d %d %d", &d, &x, &y); if(x > n || y > n || (d == 2 && x == y)) ans++; else if(find_set(x) == find_set(y)) { if(d == 1 && relation[x] != relation[y]) ans++; if(d == 2 && (relation[x] + 1) % 3 != relation[y]) ans++; } else union_set(d, x, y); } printf("%d\n", ans); return 0; }
|