博客
关于我
1118 Birds in Forest
阅读量:424 次
发布时间:2019-03-06

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

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:

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 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 void findFather(int x) { 2     int a = x; 3     while (x != fa[x]) { 4         x = fa[x]; 5     } 6     while (a != fa[a]) {       // 路径压缩 7         int z = a; 8         a = fa[a]; 9         fa[z] = x;10     }11     return x;12 }

  这个函数是用来寻找某个节点的祖先结点的

1 void Union(int x, int y) {2     int faA = findFather(x);3     int faB = findFather(y);4     if (faA != faB) fa[faA] = faB;5 }    6

  用来合并两个不同的子集。

  


  首先,我们要初始化fa数组,使每个结点的祖先结点是其本身。 

  然后要做的就是将同一副画中的每一个结点通过Union函数合并到一个集合中。

  用exist数组来存储结点是否存在。cnt数组来存储以某一个节点为祖先结点的集合中节点的个数。

 

Code:

1 #include 
2 3 using namespace std; 4 5 const int maxn = 10005; 6 vector
fa(10005); 7 8 int findFather(int x) { 9 int a = x;10 while (x != fa[x]) {11 x = fa[x];12 }13 while (a != fa[a]) {14 int z = a;15 a = fa[a];16 fa[z] = x;17 }18 return x;19 }20 21 void Union(int x, int y) {22 int faX = findFather(x);23 int faY = findFather(y);24 if (faX != faY) fa[faY] = faX;25 }26 27 int main() {28 int n;29 cin >> n;30 31 for (int i = 0; i < maxn; ++i) {32 fa[i] = i;33 }34 35 int k, id, m;36 vector
exist(10005, false);37 vector
cnt(10005, 0);38 int numTrees = 0;39 int numBirds = 0;40 for (int i = 0; i < n; ++i) {41 cin >> k >> id;42 exist[id] = true;43 for (int j = 1; j < k; ++j) {44 cin >> m;45 exist[m] = true;46 Union(id, m);47 }48 }49 for (int i = 1; i < maxn; ++i) {50 if (exist[i]) {51 int root = findFather(i);52 cnt[root]++;53 }54 }55 for (int i = 1; i < maxn; ++i) {56 if (exist[i] && cnt[i] > 0) {57 numTrees++;58 numBirds += cnt[i];59 }60 }61 cout << numTrees << " " << numBirds << endl;62 int querys;63 cin >> querys;64 for (int i = 0; i < querys; ++i) {65 int bird1, bird2;66 cin >> bird1 >> bird2;67 if (findFather(bird1) == findFather(bird2))68 cout << "Yes" << endl;69 else70 cout << "No" << endl;71 }72 return 0;73 }

 

参考:

 

转载地址:http://antuz.baihongyu.com/

你可能感兴趣的文章
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>