记录编号 |
418737 |
评测结果 |
AAAAAA |
题目名称 |
无根树转有根树 |
最终得分 |
100 |
用户昵称 |
HeHe |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.271 s |
提交时间 |
2017-07-01 10:05:29 |
内存使用 |
9.40 MiB |
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
namespace IO{
inline char getc(void);
inline int in(void);
inline void write(int x);
inline void write();
inline void write_all();
char ops[1 << 18], *opt = ops, *const opt_end = ops + (1 << 18);
char *p = new char[20]{0};
inline char getc(void){
static char buf[1 << 18], *fs, *ft;
return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
}
inline int in(void){
register int res = 0;
register char tmp = getc();
while(!isdigit(tmp)) tmp = getc();
while(isdigit(tmp))
res = (res + (res << 2) << 1) + (tmp ^ 48),
tmp = getc();
return res;
}
inline void write(int x){
if(x < 0){
*(opt++) = '-';
if(opt == opt_end) write_all();
x = -x;
}
do{
*(++p) = (x % 10) | 0x30;
x /= 10;
}while(x);
while(*p){
*(opt++) = *(p--);
if(opt == opt_end) write_all();
}
*(opt++) = ' ';
if(opt == opt_end) write_all();
return ;
}
inline void write(void){
*(opt++) = '\n';
if(opt == opt_end) write_all();
return ;
}
inline void write_all(void){
fwrite(ops, 1, opt - ops, stdout);
opt = ops;
return ;
}
}
using IO::in;
using IO::write;
using IO::write_all;
const int MAXN = 1e6 + 10;
struct EDGE{
int to;
EDGE *ne;
EDGE(){;}
EDGE(int _to, EDGE *_ne){
to = _to;
ne = _ne;
}
};
inline void add_edge(int fr, int to);
inline void bfs(void);
EDGE *head[MAXN];
int fa[MAXN];
int N, U;
int main(){
#ifndef LOCAL
freopen("wgs.in", "r", stdin);
freopen("wgs.out", "w", stdout);
#else
freopen("test.in", "r", stdin);
#endif
memset(fa, 0xff, sizeof(fa));
N = in(), U = in();
for(int i = 1; i < N; ++i) add_edge(in(), in());
bfs();
N = in();
for(int i = 0; i < N; ++i) write(fa[in()]);
write_all();
return 0;
}
inline void add_edge(int fr, int to){
head[fr] = new EDGE(to, head[fr]);
head[to] = new EDGE(fr, head[to]);
return ;
}
inline void bfs(void){
static queue<int> que;
static bool vis[MAXN];
static int u, v;
while(!que.empty()) que.pop();
memset(vis, 0x00, sizeof(vis));
que.push(U);
vis[U] = true;
while(!que.empty()){
u = que.front();
que.pop();
for(register EDGE *e = head[u]; e; e = e->ne){
if(vis[v = e->to]) continue;
que.push(v);
vis[v] = true;
fa[v] = u;
}
}
return ;
}