$$\color{red}{算法}\color{blue}{基础课}\color{purple}{笔记and题解}\color{green}{汇总}$$
笔记:
代码:
#include<bits/stdc++.h>
using namespace std;
int n, fa[150010], q, ans;
int get(int x){
if(x == fa[x])return x;
return fa[x] = get(fa[x]);
}
int main(){
scanf("%d%d", &n, &q);
for(int i = 1;i <= 3 * n; i++) fa[i] = i;
int c, x, y;
for(int i = 1;i <= q; i++){
scanf("%d%d%d", &c, &x, &y);
if(x > n || y > n || (c == 2 && x == y)){ ans++; continue; }
if(c == 1 && (get(x) == get(y+n) || get(x+n) == get(y))){ ans++; continue; }
if(c == 2 && (get(x) == get(y) || get(x) == get(y+n))){ ans++; continue; }
if(c == 1){
fa[get(x)] = get(y);
fa[get(x+n)] = get(y+n);
fa[get(x+2*n)] = get(y+2*n);
}
if(c == 2){
fa[get(x+n)] = get(y);
fa[get(x+2*n)] = get(y+n);
fa[get(x)] = get(y+2*n);
}
}
printf("%d\n", ans);
return 0;
}
0-n存的是self,n-2n存的是吃的,2n到3n存的天敌
牛啊 懂了
###大佬帮忙看看这个哪里不对
你发了两遍一毛一样的?