加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

1118 Birds in Forest (25 分)

发布时间:2020-12-14 04:23:00 所属栏目:大数据 来源:网络整理
导读: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 numb
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

分析:并查集,水题。
 1 /**  2 * Copyright(c)  3 * All rights reserved.  4 * Author : Mered1th  5 * Date : 2019-02-27-20.04.59  6 * Description : A1118  7 */
 8 #include<cstdio>
 9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std; 19 const int maxn=10010; 20 int father[maxn]; 21 int findFather(int x){ 22     int a=x; 23     while(x!=father[x]){ 24         x=father[x]; 25  } 26     while(a!=father[a]){ 27         int z=a; 28         a=father[a]; 29         father[z]=x; 30  } 31     return x; 32 } 33 
34 void Union(int a,int b){ 35     int faA=findFather(a); 36     int faB=findFather(b); 37     if(faA!=faB){ 38         father[faB]=faA; 39  } 40 } 41 
42 int main(){ 43 #ifdef ONLINE_JUDGE 44 #else
45     freopen("1.txt","r",stdin); 46 #endif
47     int n,k,t,t1,maxin=-1; 48     scanf("%d",&n); 49     for(int i=0;i<maxn;i++) father[i]=i; 50     for(int i=0;i<n;i++){ 51         scanf("%d%d",&k,&t); 52         maxin=max(maxin,t); 53         for(int j=0;j<k-1;j++){ 54             scanf("%d",&t1); 55             maxin=max(maxin,t1); 56  Union(t,t1); 57  } 58  } 59     set<int> tree; 60     for(int i=1;i<=maxin;i++){ 61  tree.insert(findFather(i)); 62  } 63     printf("%d %dn",tree.size(),maxin); 64     int query,u,v; 65     scanf("%d",&query); 66     for(int i=0;i<query;i++){ 67         scanf("%d%d",&u,&v); 68         if(findFather(u)==findFather(v)) printf("Yesn"); 69         else printf("Non"); 70  } 71     return 0; 72 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读