hdu_4707

算是水题一道吧,我也没有建树,看别人又用vector,又用bfs,dfs的,对vector不熟,所以就模拟了一下
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int a[100005];
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,k,p,q;
        cin>>n>>k;
        a[0]=0;
        memset(a,0,sizeof(a));
        for(int i=0;i<n-1;i++)
            {
                scanf("%d%d",&p,&q);
                a[q]=a[p]+1;
            }
            p=0;
            for(int i=0;i<n;i++)
                if(a[i]>k)
                 p++;
                 cout<<p<<endl;
    }
    return 0;
}

附上别人的代码

#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 100000 + 10;

int D, head[maxn], nxt[maxn<<1], v[maxn<<1], ecnt, d[maxn], ret;

void init(){
    memset(head, -1, sizeof(head));
    ecnt = 0;
    ret = 0;
}

void addEdge(int uu, int vv){
    v[ecnt] = vv;
    nxt[ecnt] = head[uu];
    head[uu] = ecnt;
    ecnt++;
}

void dfs(int x, int fa){
    if(d[x] > D) ret++;
    for(int e = head[x]; e != -1; e = nxt[e]) if(v[e] != fa){
        d[v[e]] = d[x] + 1;
        dfs(v[e], x);
    }
}

void solve(){
    d[0] = 0;
    dfs(0, -1);
    printf("%d\n", ret);
}

int main()
{
    int T, N, uu, vv;
    scanf("%d", &T);
    while(T--){
        init();
        scanf("%d%d", &N, &D);
        for(int i = 0; i < N-1; i++){
            scanf("%d%d", &uu, &vv);
            addEdge(uu, vv);
            addEdge(vv, uu);
        }
        solve();
    }
    return 0;
}


posted @ 2014-05-07 06:33  JarvisLau  阅读(125)  评论(0编辑  收藏  举报