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
题意:如果一棵树上有b(bird)1,另一树上也有b(bird)1,则为一棵树,若没有重复的鸟,默认不为同一棵树(因为是求最多有多少棵树),求最多有多少棵树、有多少只鸟,以及给出两只鸟,判断是否在同一树上。
思路:这是一道并查集题目,有套路模版,代码注释掉的部分为柳神并查方法,由于之前我没有设计过并查集的问题,我的第一思路是使用map来做,虽然代码看似复杂,但是逻辑正是常规思考的逻辑,map的key值为鸟的编号,value值为树的编号,在输入是判断,若发现a树的鸟已经出现在b树上了,那就在把a树上的鸟的编号全部赋值为b,因为有可能c树的鸟会同时出现在a、b树上(3棵及以上树的并入),所以应用set来解决这个痛点,详细代码如下~有问题可以留言交流
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<unordered_map>
#include<unordered_set>
using namespace std;
// const int maxn = 10010;
// int fa[maxn] = {0}, cnt[maxn] = {0};
// int findFather(int x) {
// int a = x;
// while(x != fa[x])
// x = fa[x];
// while(a != fa[a]) {
// int z = a;
// a = fa[a];
// fa[z] = x;
// }
// return x;
// }
// void Union(int a, int b) {
// int faA = findFather(a);
// int faB = findFather(b);
// if(faA != faB) fa[faA] = faB;
// }
// bool exist[maxn];
int main(){
unordered_map<int, int> un_map;
vector<int> total_vec[10001];
int n;
cin >> n;
int tree_cnt = 0;
int max_bird = 0;
// for(int i = 1; i <= maxn; i++)
// fa[i] = i;
for(int i = 1; i <= n; i++){
int k;
scanf("%d", &k);
vector<int> vec(k);
unordered_set<int> un_set;
bool flag = true;
int num_tree = 0x7fffffff;
for(int j = 0; j < k; j++){
scanf("%d", &vec[j]);
max_bird = max(max_bird, vec[j]);
if(un_map[vec[j]] != 0 && un_map[vec[j]] != i){
flag = false;
un_set.insert(un_map[vec[j]]);
num_tree = min(un_map[vec[j]], num_tree);
// cout << num_tree << endl;
}
un_map[vec[j]] = i;
}
total_vec[i] = vec;
if(flag){
tree_cnt++;
}else
{
tree_cnt = tree_cnt - (un_set.size() - 1);
un_set.erase(num_tree);
un_set.insert(i);
for(auto it : un_set){
for(int j = 0; j < total_vec[it].size(); j++){
un_map[total_vec[it][j]] = num_tree;
}
}
}
// exist[vec[0]] = true;
// for(int j = 1; j < k; j++) {
// Union(vec[0], vec[j]);
// exist[vec[j]] = true;
// }
}
// for(int i = 1; i <= maxn; i++) {
// if(exist[i] == true) {
// int root = findFather(i);
// cnt[root]++;
// }
// }
// int numTrees = 0, numBirds = 0;
// for(int i = 1; i <= maxn; i++) {
// if(exist[i] == true && cnt[i] != 0) {
// numTrees++;
// numBirds += cnt[i];
// }
// }
printf("%d %d\n", tree_cnt, max_bird);
int q;
cin >> q;
for(int i = 0; i < q; i++){
int temp_a, temp_b;
scanf("%d %d", &temp_a, &temp_b);
// printf("%s\n", (findFather(temp_a) == findFather(temp_b)) ? "Yes" : "No");
if(un_map[temp_a] == un_map[temp_b] && temp_a <= max_bird && temp_b <= max_bird && temp_a >= 1 && temp_b >= 1){
printf("Yes\n");
}else
{
printf("No\n");
}
}
return 0;
}
本文深入探讨了并查集算法的应用,特别是在处理一系列图片中鸟类归属问题时的高效解决方案。通过实例讲解,读者将理解如何利用并查集确定森林中树木的最大数量及鸟类分布情况,同时掌握判断任意两只鸟类是否位于同一棵树上的方法。
&spm=1001.2101.3001.5002&articleId=104130003&d=1&t=3&u=edf01837090843179e7f0811578b5103)
6万+

被折叠的 条评论
为什么被折叠?



