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


  • 相关阅读:
    BZOJ2527[Poi2011]Meteors——整体二分+树状数组
    [UOJ422][集训队作业2018]小Z的礼物——轮廓线DP+min-max容斥
    BZOJ4817[Sdoi2017]树点涂色——LCT+线段树
    BZOJ4269再见Xor——高斯消元解线性基
    BZOJ4241历史研究——回滚莫队
    [十二省联考2019]字符串问题——后缀自动机+parent树优化建图+拓扑序DP+倍增
    [十二省联考2019]异或粽子——可持久化trie树+堆
    [CF594E]Cutting the Line
    [CF1246F]Cursor Distance
    [CF1246E]To Make 1
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14449802.html
Copyright © 2011-2022 走看看