Groundhog was especially careful after the outbreak,so he put on his mask in the 1^{st}1st bedroom early,and then walked on the way to the {n^{th}}nth dormitory to play with Orange. There are {n}n dormitories in ZLZX, which are connected by {n-1}n−1 corridors. Each dormitory can be reached to each other.The length of each corridor is {1}1.The walking speed of Groundhog is {1\ \mathrm{m/s}}1 m/s.
At that moment the bad news came: After Groundhog set out for {t}t seconds,Orange took his temperature, and it turned out to be 41℃ !!! In addition to grief and indignation, Orange decided to run to Groundhog, to tell him the news at the speed of {2\ \mathrm{m/s}}2 m/s.
Groundhog had to run, of course, but he was running too slow at {1\ \mathrm{m/s}}1 m/s. As he ran, he had an idea: if he ran with the best strategy, how soon would Orange catch up with him? Define that every second Groundhog moves and then Orange moves again. Groundhog can choose to stay put.
Groundhog would have solved that, of course, but he is running away now, so he give it to you, the smartest one.
The first line contains two integers {n,t}n,t。 The next {n-1}n−1 lines,each line contains two integers {x,y}x,y, indicating there is a corridor between the {x^{th}}xth dormitory and the {y^{th}}yth dormitory.
An integer, indicating the latest time for Orange to catch Groundhog.
示例1
复制
7 2 1 2 2 5 5 7 5 6 3 6 3 4
复制
1
After {t}t seconds, Groundhog is in the 5^{th}5th dormitory and Orange is in the 7^{th}7th dormitory.At this point, the best way for Groundhog is to goto the 2^{nd}2nd dormitory or the 6^{th}6th dormitory.But wherever he goes, he will be immediately caught by Orange.
先用dis记录距离 然后暴力一下追上的时间
#include
const int maxn = 1e6 + 5;
using namespace std;
int fa[maxn];
int dis[maxn];
int mx = 0;
vector G[maxn];
void dfs(int v, int f, int dep){
dis[v] = dep;
fa[v] = f;
for(auto i : G[v]){
if(i != f){
dfs(i, v, dep + 1);
}
}
}
void dfs1(int v, int f, int ds){
mx = max(mx, ds);
for(auto i : G[v]){
if(i != fa[v]){
dfs1(i, v, ds + 1);
}
}
}
int main(){
int n, t;
cin >> n >> t;
for(int i = 1; i < n; i++){
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
dfs(n, n, 0);
int st = 1;
for(int i = 1; i <= t; i++)
st = fa[st];
dfs1(st, st, 0);
int dg = mx;
int dt = dis[st] + mx;
if(st == n){
cout << 0 << endl;
return 0;
}
if(2 * dg < dt){
cout << max(1, (dt + 1) / 2) << endl;
return 0;
}
else{
for(int i = 1; i <= mx; i++){
if(dis[st] + i <= 2 * i){
cout << i << endl;
return 0;
}
}
cout << 1 << endl;
}
}