原文链接:http://kakazai.cn/index.php/Kaka/Pat/query/id/208
题目
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805354108403712
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤10^4) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 … BK
where K is the number of birds in this picture, and Bi’s are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 10^4.
After the pictures there is a positive number Q (≤10^4) which is the number of queries. Then Q lines follow, each contains the indices of two birds.
Output Specification:
For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line Yes
if the two birds belong to the same tree, or No
if not.
Sample Input:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
Sample Output:
2 10
Yes
No
题意分析
一张照片上的鸟儿们属于一棵树。如果鸟儿a属于树A,鸟儿b属于树B,而鸟儿a和鸟儿b又出现在同一张照片中,那么它们属于同一颗树,要合并树a和树b。
如果将树看成集合,问题变成,若集合A中任意一个元素与集合B中任意一个元素有关系,则合并集合A和集合B。因此,可以用并查集。
知识点与坑点
- 知识点
1)并查集
- 坑点
1)
一、并查集
算法思路
1 一开始,每个鸟儿属于不同的树,树的编号为鸟儿编号。
2 如果两个鸟儿出现在同一张照片中,那么合并它们所在的树。
3 如果两个鸟儿属于同一棵树,就输出yes,否则就输出no
代码-c++版
#include <iostream>
#include<algorithm>
#include<set>
using namespace std;
/*数值范围*/
const int maxn = 10001; //最多有10001只鸟
/*所有变量*/
int father[maxn]; //并查集数组
/* 并查集-初始化*/
void initiate(){
for(int i=0;i<maxn;i++){
father[i]=i;
}
}
/* 并查集-找根结点+压缩路径*/
int findroot(int a){
int x = a;
while(a != father[a]) {
a = father[a];
}
int temp;
while(x != father[x]){
temp = father[x];
father[x] = a;
x = temp;
}
return a;
}
/* 并查集-合并集合*/
void union_ab(int a,int b){
int fa = findroot(a);
int fb = findroot(b);
if(fa <= fb){
father[fb] = fa;
}else{
father[fa] = fb;
}
}
int main(){
initiate();
int n;
scanf("%d",&n);
/* 处理每一张照片 */
int k,id1,id2;
set<int> all; //存放所有鸟儿
set<int>::iterator it;
for(int i = 0; i < n; i++){
scanf("%d %d",&k,&id1);
all.insert(id1);
for(int j = 1; j < k; j++){
scanf("%d",&id2);
all.insert(id2);
union_ab(id1,id2); //合并鸟儿所在的集合
}
}
/* 统计有多少棵树 */
set<int> root;
for(it=all.begin();it!=all.end();it++){
root.insert(findroot(*it));
}
/* 按要求输出 */
printf("%d %d\n",root.size(),all.size());
/* 判断两只鸟儿是否属于同一棵树 */
scanf("%d",&k);
for(int i = 0; i < k; i++){
scanf("%d %d",&id1,&id2);
if(findroot(id1)==findroot(id2)){
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
代码-python版
有两个测试点运行超时,python的效率比不上c++
#!/usr/bin/python3
#code-python(3.6)
# 并查集-初始化
father = []
for i in range(10010):
father.append(i)
#并查集-找根结点
def findroot(a):
while(father[a]!=a):
a = father[a]
return a
#并查集-合并集合+保证根结点最小
def union_ab(a,b):
fa = findroot(a)
fb = findroot(b)
if(fa <= fb):
father[fb] = fa
else:
father[fa] = fb
n = int(input())
all = [] #存放所有鸟
for i in range(n):
line = input().split(" ") #接受每行,并用空格分开
line = list(map(int,line)) #将字符串转为整数
all += line[1:] #所有鸟儿放进all里面
bird1 = line[1] #第一只鸟
for x in line[2:]:
union_ab(bird1,x) #合并鸟儿所在的集合
all = list(set(all)) #所有鸟儿,去重
root =[] #存放所有根结点
#找出所有根结点
for p in all:
root.append(findroot(p))
root = list(set(root))
#输出鸟儿的数目,树的数目
print("%d %d"% (len(root),len(all)))
#查询鸟儿是否在同一棵树
q = int(input())
for i in range(q):
line = input().split(" ") #接受每行,并用空格分开
line = list(map(int,line)) #将字符串转为整数
if findroot(line[0])==findroot(line[1]):
print("Yes")
else:
print("No")