亲戚
亲戚
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; }