VJ pet(DFS)

来源:互联网 发布:彩虹云秒赞源码 编辑:程序博客网 时间:2024/05/16 05:06

这个题在用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;}

原创粉丝点击