#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef string str;
const ll Mod = 1e9 + 7;
const int Maxn = 1e6 + 100;
vector<ll> G[Maxn];
ll dp[Maxn], son[Maxn], m1[Maxn], m2[Maxn];
ll t, m;
vector<ll> V;
bool DFS(ll u, ll p){
bool res = (u == m), r2;
ll cnt = 0;
ll mx = 0;
ll mx2 = 0;
for(auto adj : G[u]){
if(adj == p) continue;
r2 =DFS(adj, u);
res |= r2;
if(!r2){
cnt ++;
if(dp[adj] > mx){
mx2 = mx;
mx = dp[adj];
} else mx2 = max(mx2, dp[adj]);
}
}
dp[u] = cnt + mx2;
m2[u] = mx2;
m1[u] = mx;
son[u] = cnt;
if(res){
V.pb(u);
}
return res;
}
ll dp2[Maxn];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n;
cin >> n >> t >> m;
ll u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
G[u].pb(v);
G[v].pb(u);
}
DFS(t, -1);
ll sz = V.size();
dp2[sz-1] = 0;
ll sm = 0;
for(int i = sz - 2; i >= 0; i--){
dp2[i] = min( max(dp2[i + 1], sm + m1[V[i]] + son[V[i]]), 1LL + max(dp2[i + 1], sm + m2[V[i]] + son[V[i]] - 1) );
sm += son[V[i]];
}
cout << dp2[0] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
25 ms |
23932 KB |
Output is correct |
5 |
Incorrect |
25 ms |
23928 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
491 ms |
94284 KB |
Output is correct |
2 |
Correct |
440 ms |
87288 KB |
Output is correct |
3 |
Correct |
1043 ms |
92640 KB |
Output is correct |
4 |
Correct |
519 ms |
58232 KB |
Output is correct |
5 |
Correct |
1048 ms |
92824 KB |
Output is correct |
6 |
Correct |
1054 ms |
92696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
25 ms |
23932 KB |
Output is correct |
5 |
Incorrect |
25 ms |
23928 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23928 KB |
Output is correct |
4 |
Correct |
25 ms |
23932 KB |
Output is correct |
5 |
Incorrect |
25 ms |
23928 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |