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:

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
#include <bits/stdc++.h>
using namespace std;
int p[10010];
int visit[10010];
int a[10010];
int n,m,k;
int found(int a)
{
    if(a==p[a]){
        return a;
    }
    return p[a]=found(p[a]);
}
void unite(int a,int b)
{
    int x = found(a);
    int y = found(b);
    if(x!=y){
        p[x] = y;
    }
    return ;
}
bool istrue(int a,int b){
    return found(a) == found(b);
}
int main()
{
    int x,y;
    set<int> s;

    for(int i=1;i<=10010;i++){
        p[i] = i;
    }
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&m);
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&a[j]);
            s.insert(a[j]);
        }
        for(int j=2;j<=m;j++)
        {
            unite(a[j-1],a[j]);
        }
    }
    int sum = s.size();
    set<int> ss;
    for(int i=1;i<=sum;i++)
    {
        ss.insert(found(i));
    }
    cout<<ss.size()<<" "<<sum<<endl;
    scanf("%d",&k);
    while(k--)
    {
        scanf("%d%d",&x,&y);
        if(istrue(x,y)){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/tonyyy/p/10545377.html

时间: 2024-10-08 23:05:31

1118 Birds in Forest (25 分)的相关文章

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 pai

PAT A 1118. Birds in Forest (25)

并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX]; int findfather(int x){ if(x==father[x]) return x; else{ int F=findfather(father[x]); father[x]=F; return F; } } void Union(int a , int b){ int faA=findfa

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

此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 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. Yo

PAT 1118 Birds in Forest

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 pai

4-9 二叉树的遍历 (25分)

4-9 二叉树的遍历   (25分) 输出样例(对于图中给出的树): Inorder: D B E F A G H C I Preorder: A B D F E C G H I Postorder: D E F B H G I C A Levelorder: A B C D F G I E H 代码:(都是遍历的算法) 1 // 4-9 二叉树的遍历 2 // 3 // Created by Haoyu Guo on 04/02/2017. 4 // Copyright ? 2017 Haoy

5-24 树种统计 (25分)

5-24 树种统计   (25分) 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(\le 10^5≤10?5??),随后N行,每行给出卫星观测到的一棵树的种类名称.种类名称由不超过30个英文字母和空格组成(大小写不区分). 输出格式: 按字典序递增输出各种树的种类名称及其所占总数的百分比,其间以空格分隔,保留小数点后4位. 输入样例: 29 Red Alder Ash Aspe

5-20 表达式转换 (25分)

5-20 表达式转换 (25分) 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式.日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间.请设计程序将中缀表达式转换为后缀表达式. 输入格式: 输入在一行中给出不含空格的中缀表达式,可包含+.-.*.\以及左右括号( ),表达式不超过20个字符. 输出格式: 在一行中输出转换后的后缀表达式,要求不同对象(运算数.运算符号)之间以空格分隔,但结尾不得有多余空格. 输入样例: 2+3*(7-4)+8/4 输出样例: 2 3 7 4

5-17 汉诺塔的非递归实现 (25分)

5-17 汉诺塔的非递归实现   (25分) 借助堆栈以非递归(循环)方式求解汉诺塔的问题(n, a, b, c),即将N个盘子从起始柱(标记为"a")通过借助柱(标记为"b")移动到目标柱(标记为"c"),并保证每个移动符合汉诺塔问题的要求. 输入格式: 输入为一个正整数N,即起始柱上的盘数. 输出格式: 每个操作(移动)占一行,按柱1 -> 柱2的格式输出. 输入样例: 3 输出样例: a -> c a -> b c -&g

5-3 树的同构 (25分)

5-3 树的同构   (25分) 给定两棵树T1和T2.如果T1可以通过若干次左右孩子互换就变成T2,则我们称两棵树是"同构"的.例如图1给出的两棵树就是同构的,因为我们把其中一棵树的结点A.B.G的左右孩子互换后,就得到另外一棵树.而图2就不是同构的. 图1 图2 现给定两棵树,请你判断它们是否是同构的. 输入格式: 输入给出2棵二叉树树的信息.对于每棵树,首先在一行中给出一个非负整数NN (\le 10≤10),即该树的结点数(此时假设结点从0到N-1N?1编号):随后NN行,第i