首页 > 代码库 > 亲戚
亲戚
亲戚
Time Limit: 1000MS | Memory Limit: 65535KB |
Submissions: 214 | Accepted: 82 |
Sample Input
6 5 3 1 2 1 5 3 4 5 2 1 3 1 4 2 3 5 6
Sample Output
Yes Yes No
#include <iostream> #include <algorithm> #include <string.h> using namespace std; struct kruskal { int a; int b; }; const int MAXX = 100000; int father[MAXX]; int unionsearch(int x) { return x==father[x]?x:unionsearch(father[x]); } int main() { kruskal edge[MAXX]; memset(edge,0,sizeof(kruskal)); int n,l,v; cin>>l>>n>>v; for(int i =1; i<=l; i++) { father[i] = i; } for(int i =1; i<=n; i++) { cin>>edge[i].a>>edge[i].b; } for(int i =1; i<=n; i++) { int fa = unionsearch(edge[i].a); int fb = unionsearch(edge[i].b); if(fa !=fb) { father[fb] = fa; } } for(int i =1; i<=v; i++) { int a,b;cin>>a>>b; int fa = unionsearch(a); int fb = unionsearch(b); if(fa==fb) cout<<"Yes"<<endl; else cout<<"No"<<endl; } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。