比赛 |
至少完成十道练习 |
评测结果 |
AAAAAA |
题目名称 |
无根树转有根树 |
最终得分 |
100 |
用户昵称 |
FFF团 |
运行时间 |
0.644 s |
代码语言 |
C++ |
内存使用 |
19.39 MiB |
提交时间 |
2017-05-21 13:28:57 |
显示代码纯文本
- #include<bits/stdc++.h>
- #include<iostream>
- #include<cstdio>
- #include<vector>
- using namespace std;
- const int maxn=1000005;
- vector<int>G[maxn];
- int fa[maxn],a[maxn],n,m,root;
- void dfs(int node,int f){
- if(fa[node]!=f)fa[node]=f;
- int l=G[node].size();
- for(int i=0;i<l;i++){
- if(G[node][i]!=f)dfs(G[node][i],node);
- }
- return ;
- }
- int main(){
- freopen("wgs.in","r",stdin);
- freopen("wgs.out","w",stdout);
- scanf("%d%d",&n,&root);
- for(int i=1;i<n;i++){
- int x,y;
- scanf("%d%d",&x,&y);
- G[x].push_back(y);
- G[y].push_back(x);
- }
- fa[root]=-1;
- dfs(root,-1);
- scanf("%d",&m);
- for(int i=1;i<=m;i++)scanf("%d",&a[i]);
- for(int i=1;i<=m;i++)printf("%d ",fa[a[i]]);
- return 0;
- }