1118 Birds in Forest (25分)(map解决,非并查集常规方法)

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

题意:如果一棵树上有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;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值