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 (≤10​4​​) 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 10​4​​.

After the pictures there is a positive number Q (≤10​4​​) 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 <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
using namespace std;
int n,k,m;
const int maxn=;
int father[maxn];
set<int> bird,tree;
void init(){
for(int i=;i<maxn;i++){
father[i]=i;
}
}
int findfather(int x){
int a=x;
while(x!=father[x]){
x=father[x];
}
while(a!=father[a]){
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
void munion(int a,int b){
int fa=findfather(a);
int fb=findfather(b);
if(fa!=fb){
father[fa]=fb;
}
}
int main(){
scanf("%d",&n);
init();
for(int i=;i<=n;i++){
scanf("%d",&k);
int fi;
scanf("%d",&fi);
bird.insert(fi);
for(int j=;j<k;j++){
int tmp;
scanf("%d",&tmp);
munion(fi,tmp);
bird.insert(tmp);
}
}
for(auto it=bird.begin();it!=bird.end();it++){
tree.insert(findfather(*it));
}
printf("%d %d\n",tree.size(),bird.size());
scanf("%d",&k);
for(int i=;i<k;i++){
int b1,b2;
scanf("%d %d",&b1,&b2);
if(findfather(b1)==findfather(b2))printf("Yes\n");
else printf("No\n");
}
}

注意点:第一次做到并查集的题目,看过的都已经忘记了,照着答案敲了一遍,还是要多加复习。

PAT A1118 Birds in Forest (25 分)——并查集的更多相关文章

  1. PAT A 1118. Birds in Forest (25)【并查集】

    并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX] ...

  2. PAT题解-1118. Birds in Forest (25)-(并查集模板题)

    如题... #include <iostream> #include <cstdio> #include <algorithm> #include <stri ...

  3. L2-007 家庭房产 (25分) 并查集

    题目链接 题解:并查集把一个家的并在一起,特殊的一点是编号大的并到小的去.这个题有个坑编号可能为0000,会错数据3和5. 1 #include<bits/stdc++.h> 2 usin ...

  4. L2-013 红色警报 (25分) 并查集复杂度

    代码: 1 /* 2 这道题也是简单并查集,并查集复杂度: 3 空间复杂度为O(N),建立一个集合的时间复杂度为O(1),N次合并M查找的时间复杂度为O(M Alpha(N)), 4 这里Alpha是 ...

  5. 【PAT甲级】1118 Birds in Forest (25分)(并查集)

    题意: 输入一个正整数N(<=10000),接着输入N行数字每行包括一个正整数K和K个正整数,表示这K只鸟是同一棵树上的.输出最多可能有几棵树以及一共有多少只鸟.接着输入一个正整数Q,接着输入Q ...

  6. PAT-1021 Deepest Root (25 分) 并查集判断成环和联通+求树的深度

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

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

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

  8. PAT 1118 Birds in Forest [一般]

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

  9. PAT 1009 Product of Polynomials (25分) 指数做数组下标,系数做值

    题目 This time, you are supposed to find A×B where A and B are two polynomials. Input Specification: E ...

随机推荐

  1. 【Spring】24、<load-on-startup>0</load-on-startup>配置

    load-on-startup标记容器是否在启动的时候实例化并调用其init()方法的优先级. 它的值表示servlet应该被载入的顺序 当值为0或者大于0时,表示容器在应用启动时就加载并初始化这个s ...

  2. [VUE ERROR] Invalid default value for prop "slides": Props with type Object/Array must use a factory function to return the default value

    错误原因: 当给子组件设置 props 属性时,如果参数类型是 Array 或 Object ,它的默认值必须是由工场函数返回,不能直接赋值 错误代码: proE: { type: Array, de ...

  3. Nginx 负载均衡原理简介与负载均衡配置详解

    Nginx负载均衡原理简介与负载均衡配置详解   by:授客  QQ:1033553122   测试环境 nginx-1.10.0 负载均衡原理 客户端向反向代理发送请求,接着反向代理根据某种负载机制 ...

  4. 《Inside C#》笔记(十二) 委托与事件

    C#的委托与C++的函数指针类似,但委托是类型安全的,意味着指针始终会指向有效的函数.委托的使用主要有两种:回调和事件. 一 将委托作为回调函数 在需要给一个函数传递一个函数指针,随后通过函数指针调用 ...

  5. 《Inside C#》笔记(九) 表达式和运算符

    赋值和比较操作是一门语言最基本的功能. 一 基本概念 a)基本的运算符有加.减.乘.除.取余.赋值. 运算结果需要保存在内存的某个区域,有时直接保存在操作数本身,不管怎样,如果没有保存运算结果,编译器 ...

  6. Android 系统工具类

    系统工具类 public class systemUtil { //隐藏ipad底部虚拟按键栏 @RequiresApi(api = Build.VERSION_CODES.KITKAT) publi ...

  7. JavaScript大杂烩2 - 理解JavaScript的函数

    JavaScript中的字面量 书接上回,我们已经知道在JavaScript中存在轻量级的string,number,boolean与重量级的String,Number,Boolean,而且也知道了之 ...

  8. python第十八天

    学习内容: json 模块,pickle模块,shelve模块,xml模块 json 模块  序列化: import json,pickle info={ 'name':'a', 'age':34, ...

  9. SQL Server ltrim(rtrim()) 去不掉空格

    原因:中间存在回车符或者换行符,所以要先将此符号替换掉: LTRIM(RTRIM(REPLACE(REPLACE( A,char(13),''),char(10),'') )) LTRIM(A) -- ...

  10. MySQL--eq_range_index_dive_limit参数学习,MYSQL 5.6 5.7处理数据分布不均的问题

    官方文档如下描述:This variable indicates the number of equality ranges in an equality comparison condition w ...