Park Visit
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2338 Accepted Submission(s): 1040
Problem Description
Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all of them. After consideration, she decides to visit only K spots among them. She takes out a map of the park, and luckily, finds that there're entrances at each feature spot! Claire wants to choose an entrance, and find a way of visit to minimize the distance she has to walk. For convenience, we can assume the length of all paths are 1.
Claire is too tired. Can you help her?
Claire is too tired. Can you help her?
Input
An integer T(T≤20) will exist in the first line of input, indicating the number of test cases.
Each test case begins with two integers N and M(1≤N,M≤10 5), which respectively denotes the number of nodes and queries.
The following (N-1) lines, each with a pair of integers (u,v), describe the tree edges.
The following M lines, each with an integer K(1≤K≤N), describe the queries.
The nodes are labeled from 1 to N.
Each test case begins with two integers N and M(1≤N,M≤10 5), which respectively denotes the number of nodes and queries.
The following (N-1) lines, each with a pair of integers (u,v), describe the tree edges.
The following M lines, each with an integer K(1≤K≤N), describe the queries.
The nodes are labeled from 1 to N.
Output
For each query, output the minimum walking distance, one per line.
Sample Input
1 4 2 3 2 1 2 4 2 2 4
Sample Output
1 4
Source
两遍bfs完事
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x7FFFFFFF
#define LINF 1000000000000000000LL
#define eps 1e-8
typedef long long ll;
const int maxn=1e5+10;
vector< int > e[maxn];
int dis[maxn];
int vis[maxn];
int n,m,k;
void bfs(int s)
{
CLR(dis);
CLR(vis);
dis[s]=0;
vis[s]=1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int u=q.front();
//cout<<u<<endl;
q.pop();
REP(i,e[u].size())
{
int v=e[u][i];
if(!vis[v])
{
dis[v]=dis[u]+1;
q.push(v);
vis[v]=1;
}
}
}
}
int solve(int s)
{
int len=0,tmp;
bfs(s);
FOR(i,1,n)
if(dis[i]>len)
len=dis[i],tmp=i;
bfs(tmp);
len=0;
FOR(i,1,n)
len=max(len,dis[i]);
return len;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
FOR(i,0,n)
e[i].clear();
int u,v;
REP(i,n-1)
{
scanf("%d%d",&u,&v);
e[u].PB(v);
e[v].PB(u);
}
int ans=solve(1);
ans++;
REP(i,m)
{
cin>>k;
if(k<=ans) printf("%d\n",k-1);
else printf("%d\n",(k-ans)*2+ans-1);
}
}
return 0;
}