1118. Birds in Forest (25)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

After the pictures there is a positive number Q (<= 104) 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.输入的时候将每只鸟合并到相关的集合中,并确定共同的源点鸟来代表这个集合,用一个bool数字birds标识一只鸟的存在。
2.集合数就是树的棵数,birds中为true值的变量总数就是鸟的数量
3.判断两只鸟是否在一棵树上其实就是判断它们的源点鸟是否一样就行。 代码
#include<iostream>
#include<vector>
using namespace std;
const int maxnum = 10002;
//并查集
vector<int> sources(maxnum); //某个点的源点
vector<int> cntnum(maxnum,0); //该源点点下的鸟个数
vector<bool> birds(maxnum,false); //标识鸟的存在 void Init() //初始化
{
for(int i = 1;i < maxnum;i++)
sources[i] = i;
} int findsource(int x)
{
int y = x; //索引
while( x != sources[x]) //找最初源点
{
x = sources[x];
} while( y != sources[y])
{
int tmp = y;
y = sources[y]; //继续找
sources[tmp] = x; //将所有相关点的源点统一
} return x;
} void Union(int x,int y) //合并两个相关集
{
int xsource = findsource(x);
int ysource = findsource(y);
if(xsource != ysource)
{
sources[xsource] = ysource; //合并
}
} int main()
{
int N;
Init();
while(cin >> N)
{
//输入
for(int i = 0;i < N;i++)
{
int K,first;
cin >> K >> first;
birds[first] = true;
for(int j = 0 ;j < K - 1;j++)
{
int tmp;
cin >> tmp;
birds[tmp] = true;
Union(first,tmp);
}
} //处理
int treenum = 0,birdsum = 0;
for(int i = 1;i < maxnum;i++)
{
if(birds[i])
++cntnum[sources[i]];
} for(int i = 1;i < maxnum;i++)
{
if(cntnum[i] != 0)
{
if(sources[i] == i)
treenum++;
birdsum += cntnum[i];
}
}
//输出多少棵树多少只鸟
cout << treenum << " " << birdsum << endl;
//查询
int Q;
cin >> Q;
for(int i = 0;i < Q;i++)
{
int a,b;
cin >> a >> b;
if(findsource(a) == findsource(b))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
}

  

PAT1118:Birds in Forest的更多相关文章

  1. PAT1118. Birds in Forest (并查集)

    思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...

  2. 1118 Birds in Forest (25 分)

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

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

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

  4. PAT 1118 Birds in Forest [一般]

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

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

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

  6. PAT_A1118#Birds in Forest

    Source: PAT A1118 Birds in Forest (25 分) Description: Some scientists took pictures of thousands of ...

  7. A1118. Birds in Forest

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

  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. Linux常用命令(第二版) --系统开关机命令

    系统开关机命令 说明-服务器不会经常的关机,重启,没有故障,服务器不会关机.因此这些命令就显得不是很常用. 1.shutdown /usr/sbin/shutdown e.g. shutdown -h ...

  2. 【LaTeX排版】LaTeX论文排版<一>

    本文及接下来的几篇文章主要讲关于毕设论文的排版. 1.论文的整体构架     学校规定论文字数不得少于15000:说明论文属于中篇论文.一般来说,中长篇论文采用book文类,短篇论文采用article ...

  3. 最新App Store审核10大被拒理由

    最近,苹果在官网给出了截至2015年2月份应用被拒绝的十大理由,其中50%以上的应用被拒绝都是因为这10个原因,其中7个理由和2014年相同,其中排名前三的原因分别是:需要补充更多信息.存在明显的bu ...

  4. CUDA跟OpenCV的混合编程,注意OpenCV需要重新编译

    1.注意事项 编译的办法参见: http://blog.csdn.net/wangyaninglm/article/details/39997113   以下是程序代码,网上搜的例子: 注意事项:32 ...

  5. Xcode and #pragma mark

    原帖地址:http://macdevelopertips.com/xcode/xcode-and-pragma-mark.html I've started using #pragma mark di ...

  6. CF959F

    题目大意:给定n个数,有Q次询问,每次询问由两个数l,x组成,表示前缀[1,l]构成的子序列有多少异或起来为x,个数%1e9+7 做法:考虑一个由x个数构成的线性基,如果这个线性基由Y个数构成,可以通 ...

  7. java 中 printf()语句的理解

    对print和println的理解很简单,今天突然接触到printf(),有点懵,整理了下也帮自己理一理 printf是格式化输出的形式 下在举个例子: package other; public c ...

  8. java并发包小结(一)

    java.util.concurrent 包含许多线程安全.高性能的并发构建块.换句话讲,创建 java.util.concurrent 的目的就是要实现 Collection 框架对数据结构所执行的 ...

  9. 转:<mvc:annotation-driven/>的注解意义

    <mvc:annotation-driven /> 是一种简写形式,完全可以手动配置替代这种简写形式,简写形式可以让初学都快速应用默认配置方案.<mvc:annotation-dri ...

  10. NewLife.Net——开始网络编程

    网络编程的重要性就不说了,先上源码:https://github.com/nnhy/NewLife.Net.Tests 一个服务端,就是监听一些端口,接收客户端连接和数据,进行处理,然后响应. /// ...