VJ pet(DFS)

这个题在用DFS递归的时候还要记录下当前的位置。

#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
vector<int> GG[100005];
int y, g = 0,jieguo = 0.;
int DFS(int x)
{
    for(auto &v: GG[x]){
        g += 1;//以记录深度
        if(g > y) jieguo += 1;
        DFS(v);
        g -= 1;//退出时深度减一。
    }
    return jieguo;
}
int main()
{
    int n, i, j, a, b, w;
    scanf("%d", &n);
    for(n;n > 0;n--){
        scanf("%d %d", &i, &j);
        y = j;
        for(w = 0;w < i - 1;w++){
            scanf("%d %d", &a, &b);
            GG[a].push_back(b);
        }
        DFS(0);
        cout << jieguo << endl;
        for(w = 0;w < i - 1;w++)
            GG[w].clear();
        g = 0, jieguo = 0;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lxcxingc/article/details/78601866
vj
Pet