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


  • 相关阅读:
    [Linear Algebra] Inverse and Transpose
    使用vue-cli4快速搭建项目环境、使用webpack4打包自己的library类库、封装vue插件并发布
    Python多线程批量Ping主机IP的脚本
    rk3288 系统设置中无法正常使用wifi分析
    chrome 修改为linux的ua
    js生成用户编号(时间戳+随机数)
    【软考】信息系统开发方法
    【软考】信息安全审计
    【软考】信息系统安全等级保护
    【软考】软件测试
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14449802.html
Copyright © 2011-2022 走看看