SOS!!!!!SOS!!!!!HELP!!!!HELP!!!!

P3367 【模板】并查集

vivaEleanor @ 2019-04-18 19:10:32

那位大佬能告诉蒟蒻我这有什么问题....

include<iostream>

include<cmath>

include<cstdio>

include<cstring>

using namespace std; int N,M; int pre[10010]; int zi,xi,yi; int find(int root) { if(pre[root]=root)return root; return pre[root]=find(pre[root]); }

void join(int root1,int root2){ int x,y; x=find(root1); y=find(root2); if(x!=y) pre[x]=y; //合并

}

int main(){ cin>>N>>M; cin>>zi>>xi>>yi;
int i,j; int t1,t2;
if(zi=1){join(xi,yi);} else { t1=find(xi) ; t2=find(yi); if(t1==t2){cout<<"Y"<<endl;} else{cout<<"N"<<endl;}
return 0; }

}


by Juan_feng @ 2019-04-18 19:27:54

@vivaEleanor 所以我跟您说的预处理pre数组您也没照做啊qnq......


by will5438 @ 2019-04-18 19:28:43

神老%%%


by Juan_feng @ 2019-04-18 19:28:54

您求助了, 蒟蒻给您说了正确的方法, 您不照做, 那蒟蒻也没啥办法了┑( ̄Д  ̄)┍


by yyk504 @ 2019-04-18 19:29:18

@vivaEleanor 少一个正义的for循环


by LJB00131 @ 2019-04-18 19:30:36

你只读入了一组当然只有一个输出啊

话说您是不是还没学过for

没学过的话建议不做这题了


by yyk504 @ 2019-04-18 19:30:58

@vivaEleanor 建议先通过新手村再来做这题


by vivaEleanor @ 2019-04-18 19:31:46

现在是

include<iostream>

include<cmath>

include<cstdio>

include<cstring>

using namespace std; int N,M; int pre[10010]; int zi,xi,yi; int find(int root) { if(pre[root]==root)return root; return pre[root]=find(pre[root]); }

void join(int root1,int root2){ int x,y; x=find(root1); y=find(root2); if(x!=y) pre[x]=y; //合并

}

int main(){ cin>>N>>M; int i,j; for(i=1;i<=N;i++) pre[i]=i; for(i=1;i<=M;i++) cin>>zi>>xi>>yi;

int t1,t2;
if(zi==1){join(xi,yi);} else { t1=find(xi) ; t2=find(yi); if(t1==t2){cout<<"Y"<<endl;} else{cout<<"N"<<endl;}
return 0; }

}


by F1aMiR3 @ 2019-04-18 19:32:20

而且能不能用markdown……


by yyk504 @ 2019-04-18 19:32:36

@vivaEleanor 判断那里还要一个循环


by F1aMiR3 @ 2019-04-18 19:33:10

建议lz先去选完语法再来捉此题


上一页 | 下一页