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;
    }
  • 相关阅读:
    跟随标准与Webkit源码探究DOM -- 获取元素之querySelector,querySelectorAll
    跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByClassName
    跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByTagName
    跟随标准与Webkit源码探究DOM -- 获取元素之getElementsByName
    跟随标准与Webkit源码探究DOM -- 获取元素之getElementById
    原生DOM探究 -- NodeList v.s. HTMLCollection
    Jira 在流程节点中每个状态权限设置
    Eureka集群配置
    Linux下搭建 redis
    python框架django中结合vue进行前后端分离
  • 原文地址:https://www.cnblogs.com/littlepage/p/12851577.html
Copyright © 2011-2022 走看看