标签:
#include <iostream> #include <cstdio> #include <cstring> #define REP(i, s, n) for(int i = s; i <= n; i ++) #define REP_(i, s, n) for(int i = n; i >= s; i --) #define MAX_N 100000 + 10 using namespace std; int n, m, head[MAX_N], top = 0, mx, ans = 0; struct node{ int v, next; }E[MAX_N << 1]; bool used[MAX_N]; void add(int u, int v){ E[++ top].v = v; E[top].next = head[u]; head[u] = top; } void dfs(int d, int x){ used[x] = 0; if(d > ans) ans = d, mx = x; for(int i = head[x]; i; i = E[i].next){ if(used[E[i].v]) dfs(d + 1, E[i].v); } used[x] = 1; } int main(){ int T; scanf("%d", &T); while(T --){ int u, v, q; top = 0; memset(used, 1, sizeof(used)); memset(head, 0, sizeof(head)); scanf("%d%d", &n, &m); REP(i, 1, n - 1){ scanf("%d%d", &u, &v); add(u, v); add(v, u); } ans = 0; dfs(0, 1); dfs(0, mx); ans ++; while(m -- ){ scanf("%d", &q); if(q <= ans) printf("%d\n", q - 1); else printf("%d\n", (q - ans) * 2 + ans - 1); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/ALXPCUN/p/4532386.html