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 }





  • 相关阅读:
    无法import的原因(ImportError: No module named *****)
    Mac 安装终端软件
    MyEclipse中拷贝J2EE项目,发布到tomcat中名字一样的解决办法
    微PE工具箱 v2.1 正式版,最好用的PE工具箱
    Windows 10 v2004 (OS Build 19041.329)
    Microsoft Visual C++ 2019 14.27.28914.0[2020.06.03]
    Visual C++ 运行库合集包完整版 v20200603
    VMware-workstation-full-15.5.6-16341506官方版及密钥
    [转载]使用PRIMO组件,让你的硬盘快几倍!
    [转载]goldendict下优质词典简介及安装
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11333287.html
Copyright © 2011-2022 走看看