雨辰yoha @ 2019-10-01 11:46:48
看看我哪里错了呗QAQ
#include<bits/stdc++.h>
using namespace std;
int n,m;
const int MAXX=200005;
int z[MAXX] ,x[MAXX],y[MAXX],fa[MAXX];
int getf(int need){
if(fa[need]=need){
return need;
}
fa[need]==getf(fa[need]);
return fa[need];
}
bool find(int a,int b){
int fa=getf(a);
int fb=getf(b);
if(fa==fb){
return true;
}
return false;
}
void merge(int x,int y){
if(!find(x,y)){
fa[y]=fa[x];
}
return ;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>z[i]>>x[i]>>y[i];
if(z[i]==1){
merge(x[i],y[i]);
}else{
if(find(x[i],y[i])){
cout<<"Y"<<endl;
}else{
cout<<"N"<<endl;
}
}
}
}
输入
4 7
2 1 2
1 1 2
2 1 2
1 3 4
2 1 4
1 2 3
2 1 4
输出
N
N
N
N
by ⚡小林孑⚡ @ 2019-10-01 11:47:15
@雨辰yoha 初始化
by 紫陰花 @ 2019-10-01 11:47:17
Orz
by Clear_02 @ 2019-10-01 11:47:22
@雨辰yoha 您这个好麻烦啊
by Clear_02 @ 2019-10-01 11:48:02
#include<bits/stdc++.h>
using namespace std;
int n,m,f[100010];
int find(int k)
{
if(f[k]==k)return k;
return f[k]=find(f[k]);
}
void unionn(int x,int y)
{
f[find(x)]=find(y);
}
int main()
{
cin>>n>>m;
for (int i=1;i<=n;i++)
f[i]=i;
for (int i=1;i<=m;i++)
{
int s1,s2,s3;
cin>>s1>>s2>>s3;
if (s1==1)
unionn(s2,s3);
else
if (find(s2)==find(s3))
cout<<"Y"<<endl;
else
cout<<"N"<<endl;
}
}
by Clear_02 @ 2019-10-01 11:48:14
这个明显好多了啊qwq
by Walker_V @ 2019-10-01 11:48:36
第10行应该是
fa[need]=getf(fa[need]);
大佬程序打成了
fa[need]==getf(fa[need]);
by 雨辰yoha @ 2019-10-01 11:49:51
@YUSS洛水天依 谢谢QAQ
看来我zz了QAQ
by Walker_V @ 2019-10-01 11:49:59
对还差个初始化
void Init() {
for(int i=1;i<=n;i++) {
fa[i]=i;
}
}
by Clear_02 @ 2019-10-01 11:50:14
@YUSS洛水天依 啊qwq
by Clear_02 @ 2019-10-01 11:50:53
@雨辰yoha 您可以用三目运算符来使程序简洁