zoukankan      html  css  js  c++  java
  • 1118 Birds in Forest (25 分)

    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


    用个并查集就阔以了。


     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 int n, m, x, y;
     4 map<int,int> mp;
     5 int pos = 1, fa[10001];
     6 vector<int> v[10001];
     7 set<int> st, s;
     8 
     9 int find(int x){
    10     return x==fa[x]?x:fa[x] = find(fa[x]);
    11 }
    12 
    13 int main(){
    14     cin >> n;
    15     for(int i = 1; i <= 10000; i++) fa[i] = i;
    16     for(int i = 0; i < n; i++){
    17         cin >> m;
    18         if(m) cin >> y;
    19         st.insert(y);
    20         for(int j = 1; j < m; j++){
    21             cin >> x;
    22             st.insert(x);
    23             int xx = find(x);
    24             int yy = find(y);
    25             fa[xx] = yy;
    26             y = x;
    27         }
    28     }
    29     for(int i = 1; i <= st.size(); i++){
    30         s.insert(find(i));
    31     }
    32     cout <<s.size()<<" "<<st.size()<<endl;
    33     cin >> m;
    34     while(m--){
    35         cin >> x >>y;
    36         if(find(x) == find(y)){
    37             cout <<"Yes"<<endl;
    38         }else{
    39             cout <<"No"<<endl;
    40         }
    41     }
    42 
    43     return 0;
    44 }





  • 相关阅读:
    Package manager has died异常PackageInfo 引发 Crash
    Android Bitmap变迁与原理解析(4.x-8.x)
    Rxjava2不能再发射Null了
    [转]C语言的int最值问题,以及原码反码及补码
    自定义gradle插件
    ReentrantLock(重入锁)的使用
    HashSet、TreeSet和LinkedHashSet分别基于HashMap、TreeMap和LinkedHashMap
    Java类加载双亲委托模式优点
    为什么HTTPS比HTTP安全,以及两者的优缺点
    android4.4之后的HttpUrlConnection的实现是基于okhttp
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11333287.html
Copyright © 2011-2022 走看看