10分求助
查看原帖
10分求助
287502
蒟蒻小雪球楼主2020/5/14 19:22

10分求助

#include<bits/stdc++.h>
using namespace std;
int n,k,f[50001],h[50001],s;
int zqq(int x){
	if(f[x]==x)return x;
	return f[x]=zqq(f[x]);
}
int zpp(int y){
	if(h[y]==y)return y;
	return h[y]=zpp(h[y]);
}
struct q{
	int a,b,c;
}w[100001];
int main(){
	cin>>n>>k;
	for(int i=0;i<n;i++){
		f[i]=i;
		h[i]=i;
	}
	for(int i=0;i<k;i++){
		cin>>w[i].a>>w[i].b>>w[i].c;
		if(w[i].a==1){
			if((h[zpp(w[i].b)]==f[zqq(w[i].c)])||(h[zpp(w[i].c)]==f[zqq(w[i].b)])||w[i].b>n||w[i].c>n)
				s++;
			else
				f[zpp(w[i].b)]==f[zqq(w[i].c)];
		}
		else{
			if(w[i].b==w[i].c||w[i].b==w[i].c||w[i].b>n||w[i].c>n)
				s++;
			else
				h[zpp(w[i].b)]==f[zqq(w[i].c)];
		}
	}
	cout<<s;
    return 0;
}
2020/5/14 19:22
加载中...