#include using namespace std; const int N = 50005; int p[N]; int d[N]; // 0 : 同类 1:吃 2:被吃 // 分析:https://blog.csdn.net/yjr3426619/article/details/82315133 // A吃B B吃C ==> A被C吃 Relation(a,c) = Relation(a,b)+Relation(b,c) // A被B吃 B被C吃 ==> A吃C Relation(a,c) = (Relation(a,b)+Relation(b,c))%3 int find(int x) { if (x != p[x]) { int t = p[x]; p[x] = find(p[x]); d[x] = (d[x] + d[t]) % 3; } return p[x]; } int main() { //输入+输出重定向 freopen("../AcWing/N5/poj-1182.txt", "r", stdin); int n, k; cin >> n >> k; int ans = 0; for (int i = 1; i <= n; i++) { p[i] = i; } while (k--) { int relation, x, y; scanf("%d%d%d", &relation, &x, &y); if (x > n || y > n || (relation == 2 && x == y)) { ans++; } else { int fx = find(x); int fy = find(y); if (fx == fy) { if ((relation - 1) != ((d[x] - d[y] + 3) % 3)) ans++; } else { p[fx] = fy; d[fx] = (-d[x] + relation - 1 + d[y]) % 3; } } } cout << ans; //关闭文件 fclose(stdin); return 0; }