题目

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)N(\le10^4)N(≤104) which is the number of pictures. Then NNN lines follow, each describes a picture in the format:

KKK B1B_1B1​ B2B_2B2​… B​KB_​KB​​K

where KKK is the number of birds in this picture, and BiB_iBi​'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 10410^4104.

After the pictures there is a positive number Q(≤104)Q(\le10^4)Q(≤104) which is the number of queries. Then QQQ 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

题目大意

有很多张图片,图片上面有树有鸟,规定每张图片上的鸟都属于同一棵树(鸟的编号连续),要求求出有多少棵树,有多少只鸟,并且有K次查询,判断两只鸟是否在同一棵树上。

思路

我的第一天想法是图的连通区域问题,每遍历一个连同区域时,对这个连通区域编号,即对这棵树编号,那么访问连同区域的没一只鸟时,标记这只鸟属于那棵树,后边遍历直接比较即可;按这种方法可以解题,但仔细一想,这与经典的求连通区域问题有点区别,似乎更像是并查集的问题,但是目前还不会并查集,后序会跟新并查集的做法;(更新并查集的解法,连通分量解法见另一篇博客)

代码

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;int parent[10001];
bool exist[10001];int find(int x){if(parent[x] == x)return x;elsereturn parent[x] = find(parent[x]);
}void merge(int x, int y){x = find(x);y = find(y);if(x != y)parent[x] = y;
}bool check(int x, int y){return find(x) == find(y);
}int main(){int n;for(int i=1; i<=10001; i++)parent[i] = i;fill(exist, exist+10001, false);scanf("%d", &n);for(int i=0; i<n; i++){int k, root;scanf("%d%d", &k, &root);exist[root] = true;for(int j=1, t; j<k; j++){scanf("%d", &t);exist[t] = true;merge(root, t);}}int tree = 0, bird = 0;for(int i=1; i<=10001; i++){if(exist[i] == false)break;if(parent[i] == i)tree++;bird++;}printf("%d %d\n", tree, bird);int k;scanf("%d", &k);for(int i=0, a, b; i<k; i++){scanf("%d %d", &a, &b);if(check(a, b))printf("Yes\n");elseprintf("No\n");}return 0;
}

1118 Birds in Forest (25分)——(并查集)相关推荐

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

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

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

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

  3. 【题解】1118 Birds in Forest (25分)⭐⭐ 【并查集】

    [题解]1118 Birds in Forest (25分)⭐⭐ [并查集] 题解: 简单并查集,并一下查一下就好了,没学的同学抓紧学一下 经验小结: #include<bits/stdc++. ...

  4. 1118. Birds in Forest (25)

    并查集...要用路径压缩,不然会超时, #include<iostream> #include<string> #include<map> #include< ...

  5. PAT甲级1118 Birds in Forest :[C++题解]并查集

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集的合并和查询. 问:一张照片上的鸟如何合并?相邻的合并(笔者采用的方式)或者全合并到第一只鸟就行,遍历一遍.所有照片中的鸟,合并 ...

  6. PAT甲级题目翻译+答案 AcWing(并查集)

    1013 Battle Over Cities (25 分) 题意 :给图,问去掉所询问的一个点后,需要添加多少条边可以使图连通,N<1000N<1000N<1000 思路 :并查集 ...

  7. C++学习之路 | PTA(甲级)—— 1114 Family Property (25分)(带注释)(并查集)(精简)

    1114 Family Property (25分) This time, you are supposed to help us collect the data for family-owned ...

  8. C++学习之路 | PTA(天梯赛)—— L2-007 家庭房产 (25分)(带注释)(并查集)(精简)

    L2-007 家庭房产 (25分) 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(≤1000),随后N行,每行按 ...

  9. C++学习之路 | PTA(天梯赛)—— L2-010 排座位 (25分)(带注释)(并查集)(精简)

    L2-010 排座位 (25分) 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位.无论如何,总不能把两个死对头排到同一张宴会桌旁!这个艰巨任务现在就交给你,对任何一对客人,请编写程序告诉主人他 ...

  10. C++学习之路 | PTA(天梯赛)—— L2-013 红色警报 (25分)(带注释)(并查集)(精简)

    L2-013 红色警报 (25分) 战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通, ...

最新文章

  1. R语言ggplot2可视化密度图(density plot)、改变密度图下的填充色实战
  2. 华南主板bios怎么恢复出厂设置_主板电池放电清BIOS恢复出厂设置怎么操作?配图文...
  3. 手机端viewport的设置规范
  4. php 封装JavaScript类
  5. gfs mysql_linux搭建gfs系统--iscsi+GFS实现网络存储
  6. 电脑常用音频剪辑软件_5款好用的音频剪辑软件推荐
  7. 【java】之常用四大线程池用法以及ThreadPoolExecutor详解
  8. Wpf ScrollBar自定义样式
  9. python 生成wifi密码字典_python生成密码字典的方法
  10. python rbf神经网络_RBF神经网络是什么?
  11. python全栈工程师视频_python全栈工程师视频教程
  12. lob 索引 oracle,oracle 12c lob索引
  13. 曙光服务器硬盘架,曙光服务器硬盘阵列
  14. FileNotFoundError: [Errno 2] No such file or directory: ‘./train/hazy/hazy‘
  15. docker教程(简介)
  16. 事务及事务的四大特征是什么?
  17. 计算机系统(八):网络层(上篇)
  18. 我们的程序员为何你月薪达不到30K,奈若何?
  19. select.select
  20. JSON—JavaScript中的JSON

热门文章

  1. Kubernetes init 提示 [ERROR ImagePull]: failed to pull image registry.aliyuncs.com/google_containers/c
  2. Concatenated Multiples(思维,数学)
  3. 【学习笔记】数据结构-单链表
  4. linux skb_buf大小,linux网络 skb_buff
  5. windows10专业版安装应用商店方法
  6. STM32毕业设计题目大全
  7. 大数据核心技术有哪些 怎么样学好大数据开发
  8. Python零基础速成班-第3讲-Python基础(中),list数组、tuple元组、dict字典、set集合
  9. 如何使用OpenCV-Python-dlib实现有关闭眼的检测、眨眼次数的计算?(附源码,绝对可用)
  10. 使用 JDBC 进行 MySQL 编程