PAT1118:Birds in Forest
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 (<= 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的更多相关文章
- PAT1118. Birds in Forest (并查集)
思路:并查集一套带走. AC代码 #include <stdio.h> #include <string.h> #include <algorithm> using ...
- 1118 Birds in Forest (25 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- PAT_A1118#Birds in Forest
Source: PAT A1118 Birds in Forest (25 分) Description: Some scientists took pictures of thousands of ...
- A1118. Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT A1118 Birds in Forest (25 分)——并查集
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- 1118 Birds in Forest (25 分)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
随机推荐
- Win8 HTML5与JS编程学习笔记(一)
微软的Visual Studio提供了多种构成win8应用的方式,其中最让我感到激动的是基于网页设计语言的开发模式,它提供了结合HTML5与Javascript来开发应用的方法,通过这种方法进行开发, ...
- android采用SurfaceView实现文字滚动效果
前言 为了实现文字的滚动效果,之前也重写了TextView效果都不太好,后来对SurfaceView进行完善. 声明 欢迎转载,但请保留文章原始出处:) 小崔博客:http://blog.c ...
- web容器的会话机制
基本所有web应用开发的朋友都很熟悉session会话这个概念,在某个特定时间内,我们说可以在一个会话中存储某些状态,需要的时候又可以把状态取出来,这整个过程的时间空间可以抽象成"会话&qu ...
- Linux下进程通信之管道
每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进程2再从内核缓冲区把 ...
- HOW to Use QP_PREQ_PUB.PRICE_REQUEST API to Price an Item
In this Document Goal Solution References APPLIES TO: Oracle Advanced Pricing - Version 11.5.10 ...
- WinCE系统声音定制
作者:ARM-WinCE 2010的第一篇Blog,介绍一下WinCE系统声音的定制.说白了,就是设置注册表.WinCE系统启动的开机音乐,点击触摸屏以及键盘输入的按键音,还有系统运行过程中的各种声音 ...
- 【46】java对象的三个特征(oop)
对象的三个特征: 对象的行为: 对象有哪些行为和方法 同一个类的实例,由于支持相同的行为具有家族式的特征,对象的行为是由方法定义的. 对象的状态: 施加方法时.对象的响应 每个对象都保存着描述当前特征 ...
- 对于CocoaPods的简单理解,实践安装使用过程和常见问题
(本文是自己通过其他文章进行的自我编辑和简单修改,请大家凑活看看) 一.什么是CocoaPods CocoaPods是iOS项目的依赖管理工具,该项目源码在Github上管理.开发iOS项目不可避免地 ...
- cocapods 使用及问题
一.CocoaPods的安装 (1)使用淘宝的Ruby镜像替换官方的ruby源,在终端输入命令 $ gem sources --remove https://rubygems.org/ $ gem s ...
- Django(二)如何在IIS中部署django项目
环境配置 windows7 Django 2.0 python 3.6 wfastcgi 3.0 关键步骤 打开CGI功能 控制面板/程序和功能/打开或关闭windwos功能,如图: 安装wfastc ...