博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1118 Birds in Forest【25】
阅读量:4542 次
发布时间:2019-06-08

本文共 2478 字,大约阅读时间需要 8 分钟。

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 (≤) which is the number of pictures. Then N lines follow, each describes a picture in the format:

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 1.

After the pictures there is a positive number Q (≤) 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:

43 10 1 22 3 44 1 5 7 83 9 6 4210 53 7

Sample Output:

2 10YesNo
1 #include 
2 using namespace std; 3 int a, b, n, k, q, father[10005], birdNums = 0, treeNums = 0; 4 int findFather(int x) 5 { 6 if (father[x] == x) 7 return x; 8 int temp = findFather(father[x]); 9 father[x] = temp;10 return temp;11 }12 void Unit(int a, int b)13 {14 int ax = findFather(a), bx = findFather(b);15 if (ax != bx)16 father[ax] = bx;17 }18 int main()19 {20 cin >> n;21 for (int i = 0; i < 10005; ++i)22 father[i] = i;23 while(n--)24 {25 cin >> k;26 if (k--)27 {28 cin >> a;29 birdNums = birdNums > a ? birdNums : a;//因为鸟的序号是连续的,故数量即使最大序列号30 }31 while (k--)32 {33 cin >> b; 34 Unit(a,b);//合并合集35 birdNums = birdNums > b ? birdNums : b;36 }37 }38 for (int i = 1; i <= birdNums; ++i)39 if (findFather(i) == i)40 ++treeNums;41 printf("%d %d\n", treeNums, birdNums);42 cin >> q;43 while (q--)44 {45 cin >> a >> b;46 if (findFather(a) == findFather(b))47 cout << "Yes" << endl;48 else49 cout << "No" << endl;50 }51 return 0;52 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11461672.html

你可能感兴趣的文章
如何查看方法在哪里被调用
查看>>
HUE的自动化安装部署
查看>>
图片服务器(FastDFS)的搭建
查看>>
myBatis应用
查看>>
RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.
查看>>
[PHP] 用AppServ一步到位安装PHP服务器
查看>>
mac brew install redis 报错
查看>>
Work? working!
查看>>
开源收藏
查看>>
scipy插值interpolation
查看>>
C# BackgroundWorker
查看>>
移动对meta的定义
查看>>
leetcode 76. Minimum Window Substring
查看>>
如何用Eclipse打jar包
查看>>
学习是一种投资
查看>>
banking
查看>>
Android笔记(十七) Android中的Service
查看>>
第一次作业总结
查看>>
2013年11月15日
查看>>
Android5.0Demo
查看>>