玄关,WA80Pts
查看原帖
玄关,WA80Pts
1128559
guoguo160楼主2025/1/23 22:49

思路就是把每个点用三个状态标示,分别表示当前点是哪一种动物。

#include <bits/stdc++.h>

#define int long long 

using namespace std;

const int N = 5 * 1e5 * 3 + 10;

int n, k;
int opt, x, y;
int fa[N];
int fake;

int findgen(int n) {
    return fa[n] == n ? n : fa[n] = findgen(fa[n]);
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> k;
    for (int i = 1;i <= n * 3;++i)
        fa[i] = i;
    while (k--) {
        cin >> opt >> x >> y;
        int fx1 = findgen(x * 3 + 1), fx2 = findgen(x * 3 + 2), fx3 = findgen(x * 3 + 3);
        int fy1 = findgen(y * 3 + 1), fy2 = findgen(y * 3 + 2), fy3 = findgen(y * 3 + 3);
        if (x > n || y > n) {
            fake++;
            continue;
        }
        else if (opt == 1) {
            if (fx2 == fy1 || fx3 == fy1) {
                fake++;
                continue;
            }
            else {
                fa[fx1] = fy1, fa[fx2] = fy2, fa[fx3] = fy3;
            }
        }
        else {
            if (fx1 == fy1 || fx3 == fy1) {
                fake++;
                continue;
            }
            else {
                fa[fx1] = fy3, fa[fx2] = fy1, fa[fx3] = fy2;
            }
        }
    }
    cout << fake << "\n";
    return 0;
}
2025/1/23 22:49
加载中...