#include<bits/stdc++.h>
using namespace std;
const int N = 50010;

int fa[N], n, m;
int d[N];
int find(int x)
{
    if(fa[x] != x) {
        int u = fa[x]; //记录老的父节点
        fa[x] = find(fa[x]);
        d[x] += d[u];
    }
    return fa[x];
}


int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++) fa[i] = i;
    int k, x, y, ans = 0;
   
    while(m --)
    { 
        cin >> k >> x >> y;
        if(x > n || y > n) ans ++;
        else
        {
            int fx = find(x), fy = find(y);
            if(k == 1)
            {
                if(fx == fy && (d[x] - d[y]) % 3) ans ++;
                else if(fx != fy)
                {
                    fa[fx] = fy;
                    d[fx] = d[y] - d[x];
                }
            }
            else {
                if(fx == fy && (d[x] - d[y] - 1) % 3) ans ++;
                else if(fx != fy)
                {
                    fa[fx] = fy;
                    d[fx] = d[y] - d[x] + 1;
                }
            }
        }
    }
    cout << ans;
    return 0;
}

0 条评论

目前还没有评论...