#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 used <= mid;
}
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 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23792 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23816 KB |
Output is correct |
7 |
Correct |
13 ms |
23724 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23776 KB |
Output is correct |
10 |
Correct |
13 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
59032 KB |
Output is correct |
2 |
Correct |
284 ms |
55460 KB |
Output is correct |
3 |
Correct |
964 ms |
60124 KB |
Output is correct |
4 |
Correct |
466 ms |
41932 KB |
Output is correct |
5 |
Correct |
975 ms |
60124 KB |
Output is correct |
6 |
Correct |
942 ms |
60340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23792 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23816 KB |
Output is correct |
7 |
Correct |
13 ms |
23724 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23776 KB |
Output is correct |
10 |
Correct |
13 ms |
23808 KB |
Output is correct |
11 |
Correct |
12 ms |
23820 KB |
Output is correct |
12 |
Correct |
14 ms |
23812 KB |
Output is correct |
13 |
Correct |
13 ms |
23768 KB |
Output is correct |
14 |
Correct |
13 ms |
23944 KB |
Output is correct |
15 |
Correct |
13 ms |
23816 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
13 ms |
23768 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
15 ms |
23852 KB |
Output is correct |
20 |
Correct |
13 ms |
23852 KB |
Output is correct |
21 |
Correct |
12 ms |
23840 KB |
Output is correct |
22 |
Correct |
12 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
13 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23792 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
15 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23816 KB |
Output is correct |
7 |
Correct |
13 ms |
23724 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
13 ms |
23776 KB |
Output is correct |
10 |
Correct |
13 ms |
23808 KB |
Output is correct |
11 |
Correct |
305 ms |
59032 KB |
Output is correct |
12 |
Correct |
284 ms |
55460 KB |
Output is correct |
13 |
Correct |
964 ms |
60124 KB |
Output is correct |
14 |
Correct |
466 ms |
41932 KB |
Output is correct |
15 |
Correct |
975 ms |
60124 KB |
Output is correct |
16 |
Correct |
942 ms |
60340 KB |
Output is correct |
17 |
Correct |
12 ms |
23820 KB |
Output is correct |
18 |
Correct |
14 ms |
23812 KB |
Output is correct |
19 |
Correct |
13 ms |
23768 KB |
Output is correct |
20 |
Correct |
13 ms |
23944 KB |
Output is correct |
21 |
Correct |
13 ms |
23816 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
13 ms |
23768 KB |
Output is correct |
24 |
Correct |
12 ms |
23764 KB |
Output is correct |
25 |
Correct |
15 ms |
23852 KB |
Output is correct |
26 |
Correct |
13 ms |
23852 KB |
Output is correct |
27 |
Correct |
12 ms |
23840 KB |
Output is correct |
28 |
Correct |
12 ms |
23764 KB |
Output is correct |
29 |
Correct |
13 ms |
23820 KB |
Output is correct |
30 |
Correct |
322 ms |
72388 KB |
Output is correct |
31 |
Correct |
319 ms |
72444 KB |
Output is correct |
32 |
Correct |
410 ms |
158708 KB |
Output is correct |
33 |
Correct |
392 ms |
182124 KB |
Output is correct |
34 |
Correct |
986 ms |
73548 KB |
Output is correct |
35 |
Correct |
982 ms |
73424 KB |
Output is correct |
36 |
Correct |
987 ms |
82116 KB |
Output is correct |
37 |
Correct |
1010 ms |
81940 KB |
Output is correct |
38 |
Correct |
732 ms |
84804 KB |
Output is correct |
39 |
Correct |
683 ms |
84780 KB |
Output is correct |
40 |
Correct |
735 ms |
84940 KB |
Output is correct |