#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long double ld;
typedef pair<int, pair<int, int>> ftype;
const int maxn = 1e6+5;
const int inf = 1e9+5;
int n, m, t;
int l, r, mid;
vector<int> g[maxn];
int dp[maxn], onp[maxn];
vector<int> path;
void onp_dfs(int x, int p){
if(x == t){
onp[x] = 1;
return;
}
for(auto v: g[x]){
if(v == p)continue;
onp_dfs(v, x);
if(onp[v]){
dp[x]--;
onp[x] = 1;
}
}
if(onp[x] && x != t)path.push_back(x);
//cout << x << " " << dp[x] << endl;
}
void dfs(int x, int p){
if(x == t)return;
vector<int> val;
for(auto v: g[x]){
if(v == p)continue;
dfs(v, x);
val.push_back(dp[v]);
}
if(val.empty())dp[x] = 0;
else if(val.size() == 1)dp[x] = 1;
else{
sort(val.begin(), val.end());
dp[x] = val[val.size()-2] + val.size();
}
}
int dfs2(int x, int p, int tot = 0){
if(x == t)return 0;
for(auto v: g[x]){
if(v == p)continue;
if(onp[v])tot+=dfs2(v, x, 0);
}
if(onp[x])tot+=g[x].size()-(p!=0)-1;
dp[x] += tot;
for(auto v: g[x]){
if(v == p)continue;
if(!onp[v])dfs2(v, x, tot);
}
//cout << x << " " << dp[x] << " " << tot << endl;
return tot;
}
bool chk(){
int used = 0, cap = 0;
for(auto x: path){
cap++;
int last = used;
for(auto v: g[x]){
if(onp[v])continue;
if(dp[v] + last > mid)used++;
}
//cout << used << endl;
if(used > cap)return 0;
}
return 1;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
//freopen("mousetrap.inp", "r", stdin);
// freopen("mousetrap.out", "w", stdout);
cin >> n >> t >> m;
for(int i = 0; i < n-1; i++){
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
onp_dfs(m, 0);
reverse(path.begin(), path.end());
for(auto x: path){
for(auto v: g[x])if(!onp[v])dfs(v, x);
}
dfs2(m, 0);
int l = 0, r = 2*maxn;
while(l < r){
mid = (l+r)>>1;
if(chk())r = mid;
else l = mid+1;
}
cout << l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
59036 KB |
Output is correct |
2 |
Correct |
330 ms |
67452 KB |
Output is correct |
3 |
Correct |
1010 ms |
73372 KB |
Output is correct |
4 |
Correct |
455 ms |
48412 KB |
Output is correct |
5 |
Correct |
963 ms |
73332 KB |
Output is correct |
6 |
Correct |
1010 ms |
73340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
23764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |