Topic background
If a family is too large staff to determine whether two relatives, does still not easy, and now relatives are given a map, two people ask whether any given relatives.
Title Description
Provisions: x and y are relatives, y and z are relatives, then the x and z are also relatives. If the x, y relatives, then x is y relatives of relatives, relatives are also x y relatives.
Input Format
First row: three integers n, m, p, (n <= 5000, m <= 5000, p <= 5000), respectively, there are n individual, m a relative relationship, relatives asked for p.
M the following lines: two per row numbers Mi, Mj, 1 <= Mi, Mj <= N, represented by Mi and Mj having relatives.
The next p lines: Each line two numbers Pi, Pj, Pi and Pj asked whether relatives.
Output Format
P rows of a 'Yes' or 'No'. Denotes the i th answer to the inquiry as a "have" or "have" relatives.
Sample input and output
6 5 3
1 2
1 5
3 4
5 2
1 3
1 4
2 3
5 6
Yes Yes No
Code
using namespace std;
const int range=10005;
int fa[range];
if(fa[x]!=x)
fa[x]=find(fa[x]);
return fa[x];
}
int fx=find(x),fy=find(y);
if(fx!=fy) fa[fy]=fx;
}
int n,m,p;
scanf("%d%d%d",&n,&m,&p);
for(int i=1; i<=n; i++)
fa[i]=i;
int x,y;
while(m--) {
scanf("%d%d",&x,&y);
fa[find(x)]=find(y);
}
while(p--) {
scanf("%d%d",&x,&y);
if(find(x)==find(y)) printf("Yes\n");
else printf("No\n");
}
return 0;