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


  • 相关阅读:
    poj 3669 Meteor Shower
    poj 3009 Curling 2.0
    poj 1979 Red and Black
    区间内素数的筛选
    九度oj 题目1347:孤岛连通工程
    poj 3723 Conscription
    poj 3255 Roadblocks
    Luogu P3975 [TJOI2015]弦论
    AT2165 Median Pyramid Hard 二分答案 脑洞题
    后缀自动机多图详解(代码实现)
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14449802.html
Copyright © 2011-2022 走看看