zoukankan      html  css  js  c++  java
  • 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:

    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 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

    并查集,初始化id唯一,我们可以通过findFather寻找父亲,如果找到父亲不一样,我们可以进行联合为相同的父亲。在找父亲的过程中,可以进行转移到一层父亲,这样可以增快查询效率。

    #include <iostream>
    #include <unordered_map>
    #include <unordered_set>
    using namespace std;
    int N, K, Q, a, b, fa, tmp, bird[10100];
    unordered_map<int, int> m;
    unordered_set<int> s, s_fa;
    int findFather(int num) {
        int f = num;
        while(f != bird[f]) f = bird[f];
        while(num != f) {
            int x = bird[num];
            bird[num] = f;
            num = x;
        }
        return f;
    }
    void Union(int a, int b) {
        int fa = findFather(a);
        int fb = findFather(b);
        if(fa != fb) bird[fb] = fa;
    }
    int main() {
        scanf("%d", &N);
        for(int i = 0; i < 10100; i++)
            bird[i] = i;
        while(N--) {
            scanf("%d%d", &K, &fa);
            s.insert(fa);
            for(int i = 1; i < K; i++) {
                scanf("%d", &tmp);
                Union(fa, tmp);
                s.insert(tmp);
            }
        }
        for(auto x: s) s_fa.insert(findFather(x));
        printf("%lu %lu
    ", s_fa.size(), s.size());
        scanf("%d", &Q);
        while(Q--) {
            scanf("%d%d", &a, &b);
            int fa = findFather(a);
            int fb = findFather(b);
            printf("%s
    ", fa == fb ? "Yes": "No");
        }
        return 0;
    }
  • 相关阅读:
    JS 这一次彻底理解选择排序
    JS 这一次彻底理解冒泡排序
    JS script脚本async和defer的区别
    精读JavaScript模式(九),JS类式继承与现代继承模式其二
    google recaptcha 谷歌人机身份验证超详细使用教程,前端/后端集成说明
    JS 究竟是先有鸡还是有蛋,Object与Function究竟谁出现的更早,Function算不算Function的实例等问题杂谈
    NetFramework 专栏总集篇
    详解 服务发现 的基本实现
    CF150E Freezing with Style(点分治)
    LOJ6032.「雅礼集训 2017 Day2」水箱
  • 原文地址:https://www.cnblogs.com/littlepage/p/12851577.html
Copyright © 2011-2022 走看看