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 (<= 10^4^) which is the number of pictures. Then N lines follow, each describes a picture in the format: K 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 10^4^.

    After the pictures there is a positive number Q (<= 10^4^) 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
    
     并查集的使用;
    找节点个数:节点中最大值即为节点个数    找树的个数:即父节点个数
    在查找的时候,需要压缩路劲,否则会有一个点超时
     1 #include<iostream>
     2 #include<vector>
     3 using namespace std;
     4 vector<int> v(10010), num(10010, 0);
     5 int cnt=0, maxx=0;
     6 int find(int n){
     7   int root=n;
     8   while(v[root]!=root) root=v[root];
     9   int x=n, y;
    10   while(x!=root){
    11       y=v[x];
    12       v[x] = root;
    13       x=y;
    14   }
    15   return root;
    16 }
    17 
    18 void unon(int a, int b){
    19   int aroot=find(a), broot=find(b);
    20   if(aroot!=broot) v[aroot] = broot;
    21 }
    22 int main(){
    23   int n, i, j;
    24   cin>>n;
    25   for(i=0; i<=10000; i++) v[i]=i;
    26   for(i=1; i<=n; i++){
    27     int k, temp, t;
    28     cin>>k>>temp;
    29     if(maxx<temp) maxx=temp;
    30     for(j=1; j<k; j++){
    31       scanf("%d", &t);
    32       unon(temp, t);
    33       if(maxx<t) maxx=t;
    34       temp = t;
    35     }
    36   }
    37   cin>>n;
    38   for(i=1; i<=maxx; i++) if(v[i]==i) cnt++;
    39   cout<<cnt<<" "<<maxx<<endl;
    40   for(i=0; i<n; i++){
    41     int p, q;
    42     cin>>p>>q;
    43     p = find(p);
    44     q = find(q);
    45     if(p==q) cout<<"Yes"<<endl;
    46     else cout<<"No"<<endl;
    47   }
    48   return 0;
    49 }
     
     
     
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    ASP.NET Web API模型验证以及异常处理方式
    Javascript基础恶补
    求一个集合的集合下所有集合元素求值
    C#创建唯一的订单号, 考虑时间因素
    git的几十个基本面
    报错:ASP.NET Web API中找不到与请求匹配的HTTP资源
    使用RAML描述API文档信息的一些用法整理
    Postman测试Web API
    Javascript中的Prototype到底是啥
    AngularJS和DataModel
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9178140.html
Copyright © 2011-2022 走看看