Time Limit: 3.0 Seconds Memory Limit: 65536K
Total Runs: 1511 Accepted Runs: 687 Multiple test files
When in his junior middle school, nuanran had been a fan of the famous singer Chen Huilin. She is from Hong Kong, and her English name is Kelly. One of Kelly's best friends is Lin Yilian (another famous singer). Therefore, Lin Yilian is also nuanran's idol. Here, we assume that if A, B are friends and B, C are friends, then A, C are also friends. Nevertheless, all Kelly's friends are nuanran's idols. Because Kelly may have other friends and her friends can also have friends, nuanran have so many idols that he can't know whether a given singer is his idol or not. Can you help him?
Input
Input will consist of multiple test cases, with each test case including the following three parts.
1) A line contains three integers N (the number of singers), M (the number of relations), K (the number of inquiries). 1 ≤ N, M, K ≤ 100000.
2) M lines follow. Each line contains two integers x and y (1 ≤ x, y ≤ N), denoting that x and y are friends. And Kelly is always represented by 1.
3) K lines follow. Each line contains a single integer p (1 ≤ p ≤ N), representing an inquiry.
Input will terminate by the end of file(EOF).
Output
For each inquiry p, you should output "Yes" if p is an idol of nuanran. Otherwise, you should output "No".
Sample Input
10 9 4 1 2 3 4 5 6 5 7 9 10 3 6 4 9 5 9 1 5 10 9 8 3
Sample Output
Yes Yes No Yes
#include<iostream>
#include<cstdio>
using namespace std;
int fa[100003];
int find(int x)
{
while(fa[x] != x)
{
x = fa[x];
}
return x;
}
void merge(int x,int y)
{
int f1 = find(x);
int f2 = find(y);
if(f1 < f2) fa[f2] = f1;
else if(f1 > f2) fa[f1] = f2;
}
int main()
{
int ns,nr,k,a,b,q;
while(~scanf("%d%d%d",&ns,&nr,&k))
{
//初始化
for(int i = 1; i <= ns; i++)
{
fa[i] = i;
}
for(int i = 0; i < nr; i++)
{
scanf("%d%d",&a,&b);
merge(a,b);
}
for(int i = 0; i < k; i++)
{
scanf("%d",&q);
if(find(q) == 1) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}