zoukankan      html  css  js  c++  java
  • PAT甲级——A1118 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 <iostream>
     2 using namespace std;
     3 int a, b, n, k, q, father[10005], birdNums = 0, treeNums = 0;
     4 int findFather(int x)
     5 {
     6     if (father[x] == x)
     7         return x;
     8     int temp = findFather(father[x]);
     9     father[x] = temp;
    10     return temp;
    11 }
    12 void Unit(int a, int b)
    13 {
    14     int ax = findFather(a), bx = findFather(b);
    15     if (ax != bx)
    16         father[ax] = bx;
    17 }
    18 int main()
    19 {
    20     cin >> n;
    21     for (int i = 0; i < 10005; ++i)
    22         father[i] = i;
    23     while(n--)
    24     {
    25         cin >> k;
    26         if (k--)
    27         {
    28             cin >> a;
    29             birdNums = birdNums > a ? birdNums : a;//因为鸟的序号是连续的,故数量即使最大序列号
    30         }
    31         while (k--)
    32         {
    33             cin >> b;            
    34             Unit(a,b);//合并合集
    35             birdNums = birdNums > b ? birdNums : b;
    36         }
    37     }
    38     for (int i = 1; i <= birdNums; ++i)
    39         if (findFather(i) == i)
    40             ++treeNums;
    41     printf("%d %d
    ", treeNums, birdNums);
    42     cin >> q;
    43     while (q--)
    44     {
    45         cin >> a >> b;
    46         if (findFather(a) == findFather(b))
    47             cout << "Yes" << endl;
    48         else
    49             cout << "No" << endl;
    50     }
    51     return 0;
    52 }
  • 相关阅读:
    人见人爱a+b,算出两个时间的和
    远程推送原理
    iOS中的定时器
    四大对象
    核心动画类关系图
    无沙盒缓存原理
    应用程序的生命周期
    同样明文同样算法得到不同密码原理
    线程状态
    iOS中的几个重要方法
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11461672.html
Copyright © 2011-2022 走看看