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

    并查集
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=10010;
    int father[maxn]={0};
    bool exist[maxn];
    int cnt[maxn];
    int findFather(int x){
        int a=x;
        while(x!=father[x]){
            x=father[x];
        }
        while(a!=father[a]){
            int z=a;
            a=father[a];
            father[z]=x;
        }
        return x;
    }
    void Union(int a,int b){
        int x=findFather(a);
        int y=findFather(b);
        if(x!=y){
            father[x]=y;
        }
    }
    int main(){
        int n;
        scanf("%d",&n);
        for(int i=1;i<=maxn;i++){
            father[i]=i;
        }
        for(int i=0;i<n;i++){
            int k,id,temp;
            scanf("%d %d",&k,&id);
            exist[id]=true;
            for(int j=0;j<k-1;j++){
                scanf("%d",&temp);
                Union(id,temp);
                exist[temp]=true;
            }
        }
        for(int i=1;i<=maxn;i++){
            if(exist[i]==true){
                int root=findFather(i);
                cnt[root]++;
            }
        }
        int numtree=0,numchild=0;
        for(int i=1;i<=maxn;i++){
            if(exist[i]==true&&cnt[i]!=0){
                numtree++;
                numchild+=cnt[i];
            }
        }
        printf("%d %d
    ",numtree,numchild);
        int m,a,b;
        scanf("%d",&m);
        for(int i=0;i<m;i++){
            scanf("%d %d",&a,&b);
            if(findFather(a)==findFather(b)){
                printf("Yes
    ");
            }
            else{
                printf("No
    ");
            }
                
        }
        
    
        return 0;
    }
    
    
    


  • 相关阅读:
    根据人脸关键点实现平面三角剖分和最近邻搜索 ( KNN, K=1 ), opencv3.4.2, C++
    KDTree  C++实现
    python 保留小数
    Clion提示:Single-argument constructors must be marked explicitly to avoid unintentional implicit conversions 解法办法
    二叉搜索树的C++ 实现
    排列组合之组合问题 网易深度学习工程师面试题 C++ 使用10方法
    OS X 安装命令行看图工具 chafa 以及其依赖libtool
    leetcode704 C++ 72ms 二分查找
    Deep Interest Network for Click-Through Rate Prediction
    归并排序
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14449802.html
Copyright © 2011-2022 走看看