1118 Birds in Forest (25 分)

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 B​1​​ B​2​​ ... B​K​​

where K is the number of birds in this picture, and B​i​​'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:

  1. 4
  2. 3 10 1 2
  3. 2 3 4
  4. 4 1 5 7 8
  5. 3 9 6 4
  6. 2
  7. 10 5
  8. 3 7

Sample Output:

  1. 2 10
  2. Yes
  3. No

题目大意:给出几张照片,照片中有几只鸟,判断是否在一棵树上。

  1. #include <iostream>
  2. #include<vector>
  3. #include<map>
  4. #include<algorithm>
  5. using namespace std;
  6.  
  7. #define maxn 10001
  8. int bird[maxn];//初始状态都是0,应该将他们都初始化为自己。
  9. int exist[maxn];
  10. int findF(int index){
  11. // if(bird[index]==0)
  12. // return index;
  13. // else return findF(bird[index]);
  14. int r=bird[index];
  15. while(bird[r]!=r){
  16. int t=bird[r];
  17. bird[index]=t;
  18. r=t;
  19. }
  20. return r;
  21. }
  22. void getUnion(int a,int b){//将a和b合并起来,
  23. bird[b]=a;//这样就合并了呗。
  24. }
  25.  
  26. int main(){
  27. int n;
  28. cin>>n;
  29. for(int i=;i<maxn;i++)
  30. bird[i]=i;
  31. int k,t,fa,fb;
  32. for(int i=;i<n;i++){
  33. cin>>k;
  34. vector<int> vt(k);
  35. for(int j=;j<k;j++){
  36. cin>>vt[j];//应该把这三个都合并起来。
  37. exist[vt[j]]=;//表示出现过。
  38. if(j!=){//如果不是当前,那么就不能find它的father.
  39. fa=findF(vt[j-]);//这个应该放到Union函数里的。
  40. fb=findF(vt[j]);
  41. if(fa!=fb)getUnion(fa,fb);
  42. }
  43. }
  44. }
  45. map<int,int> mp;
  46. int father,bd=;
  47. for(int i=;i<maxn;i++){
  48. if(exist[i]==){
  49. bd++;
  50. father=findF(i);
  51. mp[father]++;
  52. }
  53. }
  54. cout<<mp.size()<<" "<<bd<<'\n';
  55. int m;
  56. cin>>m;
  57. int a,b;
  58. for(int i=;i<m;i++){
  59. cin>>a>>b;
  60. if(findF(a)==findF(b)){
  61. cout<<"Yes"<<'\n';
  62. }
  63. else{
  64. cout<<"No"<<'\n';
  65. }
  66. }
  67. return ;
  68. }

//这个AC了,出现的问题在下。

1.忽略了输出要求,原来是要求输出树的数量以及鸟的总数量;

2.需要初始化bird数组为i,这个是需要一个for循环的,不可少的。

3.再次复习了并查集的两个函数怎么写。

PAT 1118 Birds in Forest [一般]的更多相关文章

  1. PAT 1118 Birds in Forest

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  2. PAT甲级——1118 Birds in Forest (并查集)

    此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984   1118 Birds in Forest  ...

  3. 1118 Birds in Forest (25 分)

    1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...

  4. [并查集] 1118. Birds in Forest (25)

    1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...

  5. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  6. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  7. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

  8. PAT A1118 Birds in Forest (25 分)——并查集

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

  9. 1118 Birds in Forest (25 分)

    Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...

随机推荐

  1. EasyUI Tree添加节点

    创建foods tree首先,我们创建foods tree,代码像这样: <div style="width:200px;height:auto;border:1px solid #c ...

  2. Jquery仿IGoogle实现可拖动窗口

    google可谓是ajax的特效用的淋漓尽致,google suggest, google map,igoogle 可拖动窗口等等...今天要做一个网站的类似效果,与编程人生的站长沟通了一下,仿照iG ...

  3. 从此sudo不再输密码

    #sudo visudo 最后一次输入密码. 在最后一行加入: xxx ALL=NOPASSWD: ALL xxx即为你当前使用的用户名,Ctrl+X,保存退出. 从此告别每次都要输密码的时代.

  4. MapReduce与Hadoop之比较

    MapReduce与Hadoop之比较 Hadoop是Apache软件基金会发起的一个项目,在大数据分析以及非结构化数据蔓延的背景下,Hadoop受到了前所未有的关注. Hadoop是一种分布式数据和 ...

  5. 【BZOJ】3172: [Tjoi2013]单词(后缀自动机)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3172 随便搞个sam就行了.(其实一开始看到数据n<=200, 单词长度不超过1e6,然后感觉 ...

  6. linux_shell_find命令

    使用find查找文件 基本格式:find path expression 1.按照文件名查找 (1)find / -name httpd.conf #在根目录下查找文件httpd.conf,表示在整个 ...

  7. 有用的Python代码片段

    我列出的这些有用的Python代码片段,为我节省了大量的时间,并且我希望他们也能为你节省一些时间.大多数的这些片段出自寻找解决方案,查找博客和StackOverflow解决类似问题的答案.下面所有的代 ...

  8. Angular ViewChild

    viewchild // 使用方法 git clone https://git.oschina.net/mumu-osc/learn-component.git cd learn-component ...

  9. 使用UE4.16开发Oculus需要Oculus软件版本高于1.11

    使用UE4.16开发Oculus,需要Oculus App Version大于1.11

  10. Windows远程桌面没有密码的电脑

    你如果想远程一个密码为空的机器,默认情况下是不可以的,需要进行以下设置 1.windows家庭版不支持远程桌面 2. 3.搜索“本地安全策略”