[HDU] [5326] Work

Problem Description

Loading...

Problem Analysis

When finding the parent node, add 1 to the parent node’s level
This way, the ran array can store how many people are below it
Finally, just count the number of entries where ran[i]==k

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include<stdio.h>
int par[120],ran[120];
void find(int m) {
    if(m!=par[m]) {
        ran[par[m]]++;
        find(par[m]);
    }
}
void unite(int x,int y) {
    if(x!=y)
        par[y]=x;
}
int main() {
    int n,k;
    while(scanf("%d %d",&n,&k)!=EOF) {
        for(int i=1; i<=n; i++) {
            ran[i]=0;
            par[i]=i;
        }
        for(int i=1; i<n; i++) {
            int a,b;
            scanf("%d %d",&a,&b);
            unite(a,b);
        }
        for(int i=1; i<=n; i++)
            find(i);
        int res=0;
        for(int i=1; i<=n; i++) {
            if(ran[i]==k)
                res++;
        }
        printf("%d\n",res);
    }
    return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...