PAT甲级——1118 Birds in Forest (并查集)

此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984

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 (≤) 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 1.

After the pictures there is a positive number Q (≤) 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

题目大意: 有N张照片,每张照片里面有K只鸟,它们属于同一棵树,每只鸟从1开始连续编号。接着有Q条查询指令,每条指令包含两只鸟的编号;要求先输出树的数量和鸟的数量,再对每条查询指令判断这两只鸟是否属于同一棵树。

思路:并查集的操作没啥好说的,略过~~,定义一个大数组S作为集合,并将所有元素初始化为-1;由于鸟的编号是连续的,那么最大的那个编号就是鸟的数量,同时也是集合S的size,遍历集合S,S中小于0的元素的数量就是树的数量。

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 int findRoot(int X);//寻找树的根
 5 void unionSet(int root, int Y);
 6 vector <int> S(10001, -1);//将集合元素初始化为-1
 7 int main()
 8 {
 9     int N, setSize = 0, Q, treeNum = 0;
10     scanf("%d", &N);
11     for (int i = 0; i < N; i++) {
12         int K, B, root;
13         scanf("%d%d", &K, &B);
14         root = findRoot(B);
15         if (setSize < B) setSize = B;
16         for (int j = 1; j < K; j++) {
17             scanf("%d", &B);
18             if (setSize < B) setSize = B;
19             unionSet(root, B);
20         }
21     }
22     for (int i = 1; i <= setSize; i++)
23         if (S[i] < 0)
24             treeNum++;
25     printf("%d %d\n", treeNum, setSize);
26     scanf("%d", &Q);
27     for (int i = 0; i < Q; i++) {
28         int X, Y;
29         scanf("%d%d", &X, &Y);
30         printf(findRoot(X) == findRoot(Y) ? "Yes\n" : "No\n");
31     }
32 }
33 void unionSet(int root, int Y) {
34     int rootY = findRoot(Y);
35     S[rootY] = root;
36 }
37 int findRoot(int X) {
38     if (S[X] < 0) {
39         return X;
40     }
41     else {
42         return S[X] = findRoot(S[X]);//递归地进行路径压缩
43     }
44 }

原文地址:https://www.cnblogs.com/yinhao-ing/p/10810535.html

时间: 2024-10-08 23:05:34

PAT甲级——1118 Birds in Forest (并查集)的相关文章

PAT A 1118. Birds in Forest (25)

并查集合并 #include<iostream> using namespace std; const int MAX = 10010; int father[MAX],root[MAX]; int findfather(int x){ if(x==father[x]) return x; else{ int F=findfather(father[x]); father[x]=F; return F; } } void Union(int a , int b){ int faA=findfa

PAT 1118 Birds in Forest

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 pai

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 pai

PAT Advanced 1107 Social Clusters (30) [并查集]

题目 When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. A "social cluster" is a set of people who have some of their hobbies in common. You are supposed to

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 pai

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级题分类汇编——杂项

集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 Sort with Swap(0, i) 25 通过与0号元素交换来排序 数学 1068 Find More Coins 30 子集和问题 算法 1070 Mooncake 25 背包问题 算法 1078 Hashing 25 散列 散列 1085 Perfect Sequence 25 符合约束的最大数列长度 集合 1092 To