#include<bits/stdc++.h>
using namespace std;
const int maxn=1000000+5;
vector<int>adj[maxn],alldp[maxn];
int par[maxn],vis[maxn],n,t,m,dp[maxn],ted[maxn],mainres;
priority_queue<pair<int,int>>pq;
int val[maxn];
void dfs(int u,int para=0){
par[u]=para;
for(int x:adj[u])
{
if(x!=para){
dfs(x,u);
vis[u]|=vis[x];
if(vis[x]==0){
ted[u]++;
alldp[u].push_back(dp[x]);
}
}
}
if((int)alldp[u].size()>0){
if((int)alldp[u].size()==1){
dp[u]=2;
}
else{
sort(alldp[u].begin(),alldp[u].end());
int maxa1=alldp[u].back(),maxa2=alldp[u][(int)alldp[u].size()-2];
dp[u]=maxa2+ted[u]-1;
dp[u]++;
}
}
else{
dp[u]=1;
}
}
void solve(int had){
int allted=0,now=had;
while(now!=t){
//cout<<"1 "<<now<<endl;
allted+=ted[now];
now=par[now];
}
int fted=allted;
now=had;
int unnow=1;
while(now!=t){
int ffted=allted;
for(int i=(int)alldp[now].size()-1;i>=0;i--){
int x=alldp[now][i]+allted-1+(int)pq.size();
if((int)pq.size()<unnow){
pq.push(make_pair(-x,now));
val[now]++;
allted--;
}
else{
pair<int,int> y=pq.top();
if(-y.first<=x){
pq.pop();
pq.push(make_pair(-x,now));
val[y.second]--;
val[now]++;
allted--;
if(y.second==now){
allted++;
}
}
}
}
allted=ffted-ted[now];
now=par[now];
unnow++;
}
allted=fted;
int resa=(int)pq.size();
now=had;
int fn=0;
while(now!=t){
for(int i=0;i<(int)alldp[now].size()-(val[now]-fn);i++){
int x=alldp[now][i]+allted-1;
// cout<<now<<" asd "<<x<<"\n";
resa=max(resa,x+fn);
}
allted-=ted[now];
fn=val[now];
val[par[now]]+=val[now];
now=par[now];
}
mainres=resa;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>t>>m;
for(int i=0;i<n-1;i++){
int u,v;
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
vis[m]=1;
dfs(t);
solve(m);
cout<<mainres<<"\n";
}
Compilation message
mousetrap.cpp: In function 'void dfs(int, int)':
mousetrap.cpp:28:8: warning: unused variable 'maxa1' [-Wunused-variable]
28 | int maxa1=alldp[u].back(),maxa2=alldp[u][(int)alldp[u].size()-2];
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47264 KB |
Output is correct |
3 |
Correct |
25 ms |
47208 KB |
Output is correct |
4 |
Correct |
23 ms |
47316 KB |
Output is correct |
5 |
Correct |
24 ms |
47312 KB |
Output is correct |
6 |
Correct |
25 ms |
47216 KB |
Output is correct |
7 |
Correct |
25 ms |
47304 KB |
Output is correct |
8 |
Correct |
24 ms |
47300 KB |
Output is correct |
9 |
Correct |
27 ms |
47260 KB |
Output is correct |
10 |
Correct |
24 ms |
47284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
321 ms |
106040 KB |
Output is correct |
2 |
Correct |
302 ms |
112128 KB |
Output is correct |
3 |
Correct |
861 ms |
124612 KB |
Output is correct |
4 |
Correct |
422 ms |
85864 KB |
Output is correct |
5 |
Correct |
847 ms |
124656 KB |
Output is correct |
6 |
Correct |
863 ms |
124732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47264 KB |
Output is correct |
3 |
Correct |
25 ms |
47208 KB |
Output is correct |
4 |
Correct |
23 ms |
47316 KB |
Output is correct |
5 |
Correct |
24 ms |
47312 KB |
Output is correct |
6 |
Correct |
25 ms |
47216 KB |
Output is correct |
7 |
Correct |
25 ms |
47304 KB |
Output is correct |
8 |
Correct |
24 ms |
47300 KB |
Output is correct |
9 |
Correct |
27 ms |
47260 KB |
Output is correct |
10 |
Correct |
24 ms |
47284 KB |
Output is correct |
11 |
Correct |
29 ms |
47320 KB |
Output is correct |
12 |
Correct |
25 ms |
47332 KB |
Output is correct |
13 |
Correct |
25 ms |
47316 KB |
Output is correct |
14 |
Correct |
28 ms |
47404 KB |
Output is correct |
15 |
Correct |
25 ms |
47444 KB |
Output is correct |
16 |
Correct |
25 ms |
47392 KB |
Output is correct |
17 |
Correct |
24 ms |
47392 KB |
Output is correct |
18 |
Correct |
23 ms |
47316 KB |
Output is correct |
19 |
Correct |
24 ms |
47308 KB |
Output is correct |
20 |
Correct |
24 ms |
47412 KB |
Output is correct |
21 |
Correct |
23 ms |
47388 KB |
Output is correct |
22 |
Correct |
29 ms |
47308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
47316 KB |
Output is correct |
2 |
Correct |
24 ms |
47264 KB |
Output is correct |
3 |
Correct |
25 ms |
47208 KB |
Output is correct |
4 |
Correct |
23 ms |
47316 KB |
Output is correct |
5 |
Correct |
24 ms |
47312 KB |
Output is correct |
6 |
Correct |
25 ms |
47216 KB |
Output is correct |
7 |
Correct |
25 ms |
47304 KB |
Output is correct |
8 |
Correct |
24 ms |
47300 KB |
Output is correct |
9 |
Correct |
27 ms |
47260 KB |
Output is correct |
10 |
Correct |
24 ms |
47284 KB |
Output is correct |
11 |
Correct |
321 ms |
106040 KB |
Output is correct |
12 |
Correct |
302 ms |
112128 KB |
Output is correct |
13 |
Correct |
861 ms |
124612 KB |
Output is correct |
14 |
Correct |
422 ms |
85864 KB |
Output is correct |
15 |
Correct |
847 ms |
124656 KB |
Output is correct |
16 |
Correct |
863 ms |
124732 KB |
Output is correct |
17 |
Correct |
29 ms |
47320 KB |
Output is correct |
18 |
Correct |
25 ms |
47332 KB |
Output is correct |
19 |
Correct |
25 ms |
47316 KB |
Output is correct |
20 |
Correct |
28 ms |
47404 KB |
Output is correct |
21 |
Correct |
25 ms |
47444 KB |
Output is correct |
22 |
Correct |
25 ms |
47392 KB |
Output is correct |
23 |
Correct |
24 ms |
47392 KB |
Output is correct |
24 |
Correct |
23 ms |
47316 KB |
Output is correct |
25 |
Correct |
24 ms |
47308 KB |
Output is correct |
26 |
Correct |
24 ms |
47412 KB |
Output is correct |
27 |
Correct |
23 ms |
47388 KB |
Output is correct |
28 |
Correct |
29 ms |
47308 KB |
Output is correct |
29 |
Correct |
24 ms |
47312 KB |
Output is correct |
30 |
Correct |
336 ms |
119352 KB |
Output is correct |
31 |
Correct |
354 ms |
119392 KB |
Output is correct |
32 |
Correct |
332 ms |
185964 KB |
Output is correct |
33 |
Correct |
404 ms |
217284 KB |
Output is correct |
34 |
Correct |
864 ms |
124648 KB |
Output is correct |
35 |
Incorrect |
906 ms |
124868 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |