#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//~ #define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
#define g4(a) get<4>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
//~ using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
using iiiii = tuple<int, int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int n, a, b, ans = INT_MAX, u[300005], v[300005], cache[300005];
bool mrk[300005];
vector<int> edges;
vector<ii> adj[300005];
int dp(int n, int x, int e = -1) {
basic_string<int> vec;
int ret = 0;
for (auto [u, idx] : adj[n]) if (u != e && idx != x) {
vec.pb(dp(u, x, n));
}
sort(vec.begin(), vec.end(), greater<int>());
for (int i = 0; i < (int)vec.size(); i++) {
ret = max(ret, vec[i] + i + 1);
}
return ret;
}
int split(int x) {
if (cache[x] != -1) return cache[x];
return cache[x] = max(dp(a, x), dp(b, x));
}
bool get_edges(int u, int ed, int e = -1) {
if (u == ed) return 1;
for (auto [v, idx] : adj[u]) if (v != e) {
if (get_edges(v, ed, u)) {
edges.pb(idx);
return 1;
}
}
return 0;
}
main() {
memset(cache, -1, sizeof cache);
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> a >> b;
for (int i = 1; i < n; i++) {
cin >> u[i] >> v[i];
adj[u[i]].eb(v[i], i);
adj[v[i]].eb(u[i], i);
}
get_edges(a, b);
int lo = 0, hi = (int)edges.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2, ll = mid;
{
int lo = 0, hi = mid;
while (lo <= hi) {
int mid2 = (lo + hi) / 2;
if (split(edges[mid]) == split(edges[mid2])) {
ll = mid2;
hi = mid2 - 1;
} else {
lo = mid2 + 1;
}
}
}
// [ll, mid] all same
int lp = ll ? split(edges[ll - 1]) : INT_MAX;
if (lp > split(edges[mid])) {
ans = split(edges[mid]);
lo = mid + 1;
} else {
hi = ll - 1;
}
}
cout << ans << '\n';
}
Compilation message
torrent.cpp:62:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
62 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8532 KB |
Output is correct |
2 |
Correct |
5 ms |
8532 KB |
Output is correct |
3 |
Correct |
6 ms |
8532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1621 ms |
24804 KB |
Output is correct |
2 |
Correct |
1932 ms |
26604 KB |
Output is correct |
3 |
Correct |
1778 ms |
28268 KB |
Output is correct |
4 |
Correct |
1735 ms |
27748 KB |
Output is correct |
5 |
Correct |
1494 ms |
24228 KB |
Output is correct |
6 |
Correct |
1014 ms |
25356 KB |
Output is correct |
7 |
Correct |
1773 ms |
29712 KB |
Output is correct |