• PAT甲级——1118 Birds in Forest (并查集)


     
    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

    题目大意: 有N张照片,每张照片里面有K只鸟,它们属于同一棵树,每只鸟从1开始连续编号。接着有Q条查询指令,每条指令包含两只鸟的编号;要求先输出树的数量和鸟的数量,再对每条查询指令判断这两只鸟是否属于同一棵树。

    思路:并查集的操作没啥好说的,略过~~,定义一个大数组S作为集合,并将所有元素初始化为-1;由于鸟的编号是连续的,那么最大的那个编号就是鸟的数量,同时也是集合S的size,遍历集合S,S中小于0的元素的数量就是树的数量。

     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 int findRoot(int X);//寻找树的根
     5 void unionSet(int root, int Y);
     6 vector <int> S(10001, -1);//将集合元素初始化为-1
     7 int main()
     8 {
     9     int N, setSize = 0, Q, treeNum = 0;
    10     scanf("%d", &N);
    11     for (int i = 0; i < N; i++) {
    12         int K, B, root;
    13         scanf("%d%d", &K, &B);
    14         root = findRoot(B);
    15         if (setSize < B) setSize = B;
    16         for (int j = 1; j < K; j++) {
    17             scanf("%d", &B);
    18             if (setSize < B) setSize = B;
    19             unionSet(root, B);
    20         }
    21     }
    22     for (int i = 1; i <= setSize; i++) 
    23         if (S[i] < 0) 
    24             treeNum++;
    25     printf("%d %d
    ", treeNum, setSize);
    26     scanf("%d", &Q);
    27     for (int i = 0; i < Q; i++) {
    28         int X, Y;
    29         scanf("%d%d", &X, &Y);
    30         printf(findRoot(X) == findRoot(Y) ? "Yes
    " : "No
    ");
    31     }
    32 }
    33 void unionSet(int root, int Y) {
    34     int rootY = findRoot(Y);
    35     S[rootY] = root;
    36 }
    37 int findRoot(int X) {
    38     if (S[X] < 0) {
    39         return X;
    40     }
    41     else {
    42         return S[X] = findRoot(S[X]);//递归地进行路径压缩
    43     }
    44 }
  • 相关阅读:
    NodeJ node.js Jquery Ajax 跨域请求
    NodeJ node.js基础
    VueX--- actions→mutations or getters→state 四大金刚
    Vue--- VueX组件间通信链接(共有方法放入了扩展目录store里面) 1.2
    arrayList 和hashSet的区别
    java面试的题目兔子、素数、水仙花
    解决java compiler level does not match the version of the installed java project facet
    Target runtime com.genuitec.runtime.generic.jee50 is not defined工程错误
    eclipse导入tomcat时Unknown version of Tomcat was specified
    eclipse开发SVN下文件显示修改时间和提交作者的方法
  • 原文地址:https://www.cnblogs.com/yinhao-ing/p/10810535.html
Copyright © 2020-2023  润新知