1118 Birds in Forest (25point(s)) - C語言 PAT 甲級

1118 Birds in Forest (25point(s))

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 (≤104) which is the number of pictures. Then N lines follow, each describes a picture in the format:

K B1 B​2​​ … BK

where K is the number of birds in this picture, and B​i​​’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 10​4​​.

After the pictures there is a positive number Q (≤104​​) 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 只鳥,並給出 k 只鳥的編號,同一張照片的鳥在同一顆樹上,Q 次查詢,每次查詢兩隻鳥是否在同顆樹上

輸出共有多少顆樹,多少隻鳥

設計思路:

並查集

  • 邊讀取數據,邊合併集合
  • 最後有幾個集合,就有幾顆樹
  • 對於每次查詢,直接返回並查集結果
編譯器:C (gcc)
#include <stdio.h>

int father[10010], rank[10010];

void makeset()
{
        int i;
        for (i = 0; i < 10010; i++) {
                father[i] = i;
                rank[i] = 0;
        }
}

int getfather(int v)
{
        if (father[v] == v)
                return v;
        father[v] = getfather(father[v]);
        return father[v];
}

void judge(int x, int y)
{
        int fx = getfather(x);
        int fy = getfather(y);
        if (rank[fx] > rank[fy]) {
                father[fy] = fx;
        } else {
                father[fx] = fy;
                if (rank[fx] == rank[fy])
                        rank[fy]++;
        }
}

int main(void)
{
        int n, k, birdnum = 0, treenum = 0;
        int i, a, b;

        makeset();
        scanf("%d", &n);
        while (n--) {
                scanf("%d", &k);
                if (k--) {
                        scanf("%d", &a);
                        birdnum = (birdnum > a ? birdnum : a);
                }
                while (k--) {
                        scanf("%d", &b);
                        birdnum = (birdnum > b ? birdnum : b);
                        judge(a, b);
                }
        }
        for (i = 1; i <= birdnum; i++)
                if (getfather(i) == i)
                        treenum++;
        printf("%d %d\n", treenum, birdnum);
        scanf("%d", &k);
        while (k--) {
                scanf("%d%d", &a, &b);
                printf("%s\n", getfather(a) == getfather(b) ? "Yes" : "No");
        }

        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章