记录编号 | 217005 | 评测结果 | AAAAAAAAAAAA | ||
---|---|---|---|---|---|
题目名称 | 亲戚 | 最终得分 | 100 | ||
用户昵称 | 是否通过 | 通过 | |||
代码语言 | C++ | 运行时间 | 0.456 s | ||
提交时间 | 2016-01-01 20:30:48 | 内存使用 | 0.39 MiB | ||
#include<fstream> using namespace std; int father[20001]; int m,n,x,y,i,q; int find(int x){ if(father[x]!=x){ father[x]=find(father[x]); } return father[x]; } void hb(int r1,int r2){ father[r2]=r1; }//合并 int main(){ ifstream fin("relations.in"); ofstream fout("relations.out"); fin>>n>>m; for(i=0;i<n;i++){ father[i]=i; }//初始化并查集 for(i=0;i<m;i++){ fin>>x>>y; if(find(x)!=find(y)){ hb(x,y); } } fin>>q; for(i=0;i<q;i++){ fin>>x>>y; if(find(x)==find(y)){ fout<<"Yes"<<endl; } else{ fout<<"No"<<endl; } } fin.close(); fout.close(); return 0; }