标签:
3 1 1 2 1 3 1 1
3
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; const int maxn = 10100; int mark[maxn],cot[maxn]; vector<int> G[10100]; int dfs(int root) { mark[root] = 1; if(G[root].size() == 0){ cot[root] = 1; return 1; } vector<int>:: iterator endd = G[root].end(); for(vector<int>:: iterator i = G[root].begin(); i!= endd; i++){ if(mark[*i]) continue; cot[root] += dfs(*i); } cot[root]++; return cot[root]; } int main() { int n,m,q,x; int a,b; while(~scanf("%d%d",&n,&m)){ memset(mark,0,sizeof(mark)); memset(cot,0,sizeof(cot)); for(int i = 1; i < n ;i++){ scanf("%d%d",&a,&b); G[a].push_back(b); G[b].push_back(a); } dfs(m); scanf("%d",&q); for(int i = 1; i <= q; i++){ scanf("%d",&x); printf("%d\n",cot[x]); } for(int i = 1; i <= n;i++) G[i].clear(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zero-begin/p/4502201.html