雨辰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 Walker_V @ 2019-10-01 12:52:22
@雨辰yoha 那个Init要放在读入n之后
by 雨辰yoha @ 2019-10-01 12:53:33
还是不对耶QAQAQ
by 雨辰yoha @ 2019-10-01 12:54:12
@YUSS洛水天依
by Walker_V @ 2019-10-01 12:54:23
@AT是女孩子 但您这写法……
void Merge(int x,int y) {
getf(fa[y])=getf(fa[x]);
return;
}
by Walker_V @ 2019-10-01 12:56:17
@雨辰yoha ???
by HaveFun @ 2019-10-01 12:56:18
@雨辰yoha 初始化加了吗?qwq
by 雨辰yoha @ 2019-10-01 12:57:06
@QwQ永动鸭 加了QAQ
by 雨辰yoha @ 2019-10-01 12:57:18
#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];
}
void Init() {
for(int i=1;i<=n+10;i++) {
fa[i]=i;
}
}
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[getf(fa[y])]=fa[getf(fa[x])];
}
return ;
}
int main()
{
cin>>n>>m;
Init();
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;
}
}
}
}
by Walker_V @ 2019-10-01 12:58:22
噢,等等 merge应该是这样写:
void merge(int x,int y) {
fa[getf(y)]=fa[x];
return;
}
by Walker_V @ 2019-10-01 12:58:52
我的锅我的锅