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 }





  • 相关阅读:
    死锁分析-(DML+DDL触发Server层死锁)
    archery 1.80推送工单到飞书webhook失败解决方案
    mysql执行计划 Select tables optimized away
    apparmor mysql_Ubuntu 上更改 MySQL 数据库数据存储目录
    查看docker容器的tcp连接(转)
    Mongo副本集搭建方式
    数据库字段命名方法
    C# DataGridview控件自动下拉到最后一行
    C# datagridview绑定List<string>显示的是数据长度
    DataGridView 清空数据
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/11333287.html
Copyright © 2011-2022 走看看