#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define foru(i,a,b) for(int i = (a); i <= (b); ++i)
#define ford(i,a,b) for(int i = (a); i >= (b); --i)
template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }
using namespace std;
const int N = 1e6 + 5;
const int inf = 1e18;
vector <int> adj[N];
int n, t, m, dp[N], par[N];
void dfs(int pos, int prev)
{
if (prev != -1)
adj[pos].erase(find(adj[pos].begin(), adj[pos].end(), prev));
int mx = 0, sc = 0;
for (auto x : adj[pos])
{
par[x] = pos;
dfs(x, pos);
if (dp[x] >= mx) sc = mx, mx = dp[x];
else sc = max(sc, dp[x]);
}
dp[pos] = sc + adj[pos].size();
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>t>>m;
t--; m--;
for (int i=0; i<n-1; i++)
{
int a, b; cin>>a>>b;
a--; b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(t, -1);
int ttld = 0, l = -1, r = n;
while (l + 1 < r)
{
int mid = (l+r)/2, ok = 1, dif = 0, prev = -1, canuse = 0, cur = m, used = 0;
while (cur != t)
{
canuse++; int tmp = 0;
for (auto x:adj[cur])
{
if (x==prev) continue;
if (dp[x] - dif + used > mid)
{
if (!canuse) ok = 0;
else canuse--, tmp++;
}
}
used += tmp, dif += adj[cur].size() - (prev != -1);
prev = cur, cur = par[cur];
}
ttld = dif;
if (ok) r = mid;
else l = mid;
}
cout<<r + ttld;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
27104 KB |
Output is correct |
10 |
Correct |
6 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
201 ms |
91924 KB |
Output is correct |
2 |
Correct |
181 ms |
86600 KB |
Output is correct |
3 |
Correct |
471 ms |
90184 KB |
Output is correct |
4 |
Correct |
223 ms |
60488 KB |
Output is correct |
5 |
Correct |
528 ms |
90184 KB |
Output is correct |
6 |
Correct |
537 ms |
90220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
27104 KB |
Output is correct |
10 |
Correct |
6 ms |
26960 KB |
Output is correct |
11 |
Correct |
6 ms |
26960 KB |
Output is correct |
12 |
Correct |
7 ms |
26960 KB |
Output is correct |
13 |
Correct |
6 ms |
26960 KB |
Output is correct |
14 |
Correct |
9 ms |
27216 KB |
Output is correct |
15 |
Correct |
6 ms |
27216 KB |
Output is correct |
16 |
Correct |
6 ms |
26964 KB |
Output is correct |
17 |
Correct |
6 ms |
26960 KB |
Output is correct |
18 |
Correct |
6 ms |
27108 KB |
Output is correct |
19 |
Correct |
6 ms |
26960 KB |
Output is correct |
20 |
Correct |
7 ms |
26960 KB |
Output is correct |
21 |
Correct |
6 ms |
26960 KB |
Output is correct |
22 |
Correct |
8 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
26960 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
6 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
27104 KB |
Output is correct |
10 |
Correct |
6 ms |
26960 KB |
Output is correct |
11 |
Correct |
201 ms |
91924 KB |
Output is correct |
12 |
Correct |
181 ms |
86600 KB |
Output is correct |
13 |
Correct |
471 ms |
90184 KB |
Output is correct |
14 |
Correct |
223 ms |
60488 KB |
Output is correct |
15 |
Correct |
528 ms |
90184 KB |
Output is correct |
16 |
Correct |
537 ms |
90220 KB |
Output is correct |
17 |
Correct |
6 ms |
26960 KB |
Output is correct |
18 |
Correct |
7 ms |
26960 KB |
Output is correct |
19 |
Correct |
6 ms |
26960 KB |
Output is correct |
20 |
Correct |
9 ms |
27216 KB |
Output is correct |
21 |
Correct |
6 ms |
27216 KB |
Output is correct |
22 |
Correct |
6 ms |
26964 KB |
Output is correct |
23 |
Correct |
6 ms |
26960 KB |
Output is correct |
24 |
Correct |
6 ms |
27108 KB |
Output is correct |
25 |
Correct |
6 ms |
26960 KB |
Output is correct |
26 |
Correct |
7 ms |
26960 KB |
Output is correct |
27 |
Correct |
6 ms |
26960 KB |
Output is correct |
28 |
Correct |
8 ms |
26960 KB |
Output is correct |
29 |
Correct |
6 ms |
26976 KB |
Output is correct |
30 |
Correct |
206 ms |
92100 KB |
Output is correct |
31 |
Correct |
197 ms |
91968 KB |
Output is correct |
32 |
Correct |
339 ms |
162488 KB |
Output is correct |
33 |
Correct |
300 ms |
162648 KB |
Output is correct |
34 |
Correct |
470 ms |
90184 KB |
Output is correct |
35 |
Correct |
479 ms |
90052 KB |
Output is correct |
36 |
Correct |
526 ms |
97864 KB |
Output is correct |
37 |
Correct |
471 ms |
97864 KB |
Output is correct |
38 |
Correct |
420 ms |
102216 KB |
Output is correct |
39 |
Correct |
405 ms |
102496 KB |
Output is correct |
40 |
Correct |
399 ms |
102216 KB |
Output is correct |