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. 关于iOS常用的26中公共方法,可copy的代码

    1. 获取磁盘总空间大小 //磁盘总空间 + (CGFloat)diskOfAllSizeMBytes{ CGFloat size = 0.0; NSError *error; NSDictionar ...

  2. java垃圾回收机制,以及常用的回收算法

    记得之前去平安面试的时候,面试官问到了垃圾回收,我当时也就是说说了垃圾回收的原理,但是具体有哪些实现策略,我当时是懵的. 概念: Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定 ...

  3. 数据挖掘进阶之序列模式分析算法GSP的实现

    序列模式分析算法GSP的实现 一.算法简介 序列模式定义:给定一个由不同序列组成的集合,其中,每个序列由不同的元素按顺序有序排列,每个元素由不同项目组成,同时给定一个用户指定的最小支持度阈值,序列模式 ...

  4. Leetcode_237_Delete Node in a Linked List

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334649 Write a function to de ...

  5. charles抓取https请求包

    说明: 用charles抓取https请求,会出现SSL Proxying disabled in Proxy Settings这样的提示,如下图.要通过charles抓取数据,还需要进行一些简单的设 ...

  6. iOS下如何获取一个类的所有子类

    因为项目中,不同用户切换时,用的是不同数据库,路径不同,而JKDBModel,数据库创建和字段检测,在app一次生命周期里,只会执行一次,所以得考虑账号切换时,创建数据库,需要获取所有JKDBMode ...

  7. TCP的基本概念三次握手,四次挥手

    TCP的特性 TCP提供一种面向连接的.可靠的字节流服务 在一个TCP连接中,仅有两方进行彼此通信.广播和多播不能用于TCP TCP使用校验和,确认和重传机制来保证可靠传输 TCP使用累积确认 TCP ...

  8. Kafka消费者-从Kafka读取数据

    (1)Customer和Customer Group (1)两种常用的消息模型 队列模型(queuing)和发布-订阅模型(publish-subscribe). 队列的处理方式是一组消费者从服务器读 ...

  9. 我应该跟libuv说声对不起,我错怪了libuv(转)

    一开始,我得向Libuv库和Libuv库开发者以及相关粉丝们道一个歉,对不起,我错怪你们了.深深感到自己的无知,是多么羞愧的事情!!    事情的经过是这样的.    原先按照公司要求,我在开发Win ...

  10. Oracle常用数据库对象(片段)

    1:用户和权限 1.1 用户的创建 a)语法---    create user 用户名  identified by 密码: b)创建用户abcd,并设定密码为abcd;---注意:操作数据库对象是 ...