HDU5326.Work

签到题,数据也比较小,直接dfs即可。你也可以用你喜欢的任一种方法AC。

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=1001;
vector<int>G[maxn];
int tree[maxn];
int vis[maxn];

void dfs(int u){
    vis[u]=1;
    tree[u]=0;
    for(int i=0;i<G[u].size();i++){
        if(vis[G[u][i]]){
            tree[u]=tree[u]+tree[G[u][i]]+1;
            continue;
        }
        dfs(G[u][i]);
        tree[u]=tree[u]+tree[G[u][i]]+1;
    }
    return ;
}

int main(){
    int n,k;
    //freopen("1011.in","r",stdin);
    //freopen("1011.out","w",stdout);
    while(scanf("%d%d",&n,&k)!=EOF){
        for(int i=1;i<=n;i++)
            G[i].clear();
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d%d",&x,&y);
            G[x].push_back(y);
        }
        mem0(vis);
        mem0(tree);
        for(int i=1;i<=n;i++){
            if(!vis[i])
                dfs(i);
        }
        int count1=0;
        for(int i=1;i<=n;i++){
            if(tree[i]==k)
                count1++;
        }
        printf("%d\n",count1);
    }
    return 0;
}

你可能感兴趣的:(HDU5326.Work)