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


  • 相关阅读:
    ASP.NET MVC 5 安全性和创建用户角色
    使用ENTITY FRAMEWORK 6以正确的方式管理DBCONTEXT:深入指南
    C#读取二进制格式的shapefile
    ASP.NET MVC 应用程序初学者常见问题汇总
    ASP.NET MVC 应用程序中使用CKEditor 4 的步骤
    html5学习笔记2
    html5学习笔记
    c#接口定义与应用
    Django ORM 优化心得
    携程的那点事
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14449802.html
Copyright © 2011-2022 走看看