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
简单并查集。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int f[],n,k,a,b,c,m;
int getf(int x)
{
if(f[x] != x)f[x] = getf(f[x]);
return f[x];
}
void mer(int x,int y)
{
int xx = getf(x);
int yy = getf(y);
f[xx] = yy;
}
void init()
{
for(int i = ;i <= ;i ++)
{
f[i] = i;
}
}
int main()
{
cin>>n;
init();
for(int i = ;i < n;i ++)
{
cin>>k;
cin>>a;
if(a > m)m = a;
for(int j = ;j < k;j ++)
{
cin>>b;
if(b > m)m = b;
mer(a,b);
}
}
for(int i = ;i <= m;i ++)
{
if(getf(i) == i)c ++;
}
cout<<c<<' '<<m<<endl;
cin>>k;
for(int i = ;i < k;i ++)
{
cin>>a>>b;
if(f[a] == f[b])cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
1118. Birds in Forest (25)的更多相关文章
- [并查集] 1118. Birds in Forest (25)
1118. Birds in Forest (25) Some scientists took pictures of thousands of birds in a forest. Assume t ...
- PAT A 1118. Birds in Forest (25)【并查集】
并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...
- PAT题解-1118. Birds in Forest (25)-(并查集模板题)
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...
- 【PAT甲级】1118 Birds in Forest (25分)(并查集)
题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- 1118 Birds in Forest (25 分)
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- PAT 1118 Birds in Forest [一般]
1118 Birds in Forest (25 分) Some scientists took pictures of thousands of birds in a forest. Assume ...
- 1118 Birds in Forest (25 分)
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- PAT 1118 Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
随机推荐
- php缓冲区
我理解的要点: 1.所有缓冲区控制是在一个PHP执行进程中发生的.如:你打开n个demo.php,他们之间开启和关闭缓冲是互不影响的. 2.output_buffering在程序中用ini_set是不 ...
- SpringMvc中@ModelAttribute的运用
/** * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用! * 2. @ModelAttribute 注解也可以来修饰目标方法 POJO ...
- Vue知识整理8:条件、URL、点击
1.通过v-if实现对条件的判断和执行: 2.通过v-bind:href实现对url地址的绑定,其中url写在data中: 3.通过@click="click1"实现点击事件,其中 ...
- 【算法与数据结构】图的最小生成树 MST - Prim 算法
Prim 算法属于贪心算法. #include <stdio.h> #define VERTEXNUM 7 #define INF 10000 typedef struct Graph { ...
- adb 性能测试(内存)
内存测试: 1.使用数据线将手机与电脑连接: 2.手机打开待测APP,即打开进程: 3.打开cmd命令,获取设备列表:输入adb devices; 4.进入该设备的shell环境,输入:adb -s ...
- 【HBase】二、HBase实现原理及系统架构
整个Hadoop生态中大量使用了master-slave的主从式架构,如同HDFS中的namenode和datanode,MapReduce中的JobTracker和TaskTracker,YAR ...
- scala 使用case 关键字定义类不需要创建对象直接调用
1.必须是使用case 定义object类 package config import org.apache.spark.sql.SparkSession import org.apache.spar ...
- 精读《What's new in javascript》
1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...
- 深入理解java:1.1.1. 反射机制
反射 到底什么是反射(Reflection)呢? 反射有时候也被称为内省(Introspection),事实上,反射,就是一种内省的方式, Java不允许在运行时改变程序结构或类型变量的结构,但它允许 ...
- DOM练习(邓邓版)
先来图片: 今天直接粘代码: 下面是html: <h4>01.图片切换</h4> <img width = "100" src = "../ ...