# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
763982 |
2023-06-23T04:26:54 Z |
pavement |
Torrent (COI16_torrent) |
C++17 |
|
2000 ms |
32204 KB |
#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 = LLONG_MAX, u[300005], v[300005], cache[300005];
bool mrk[300005];
vector<int> edges;
vector<ii> adj[300005];
void col(int u, bool c, int x, int e = -1) {
mrk[u] = c;
for (auto [v, idx] : adj[u]) if (v != e && idx != x) {
col(v, c, x, u);
}
}
int dp(int n, int e = -1) {
basic_string<int> vec;
int ret = 0;
for (auto [u, _] : adj[n]) if (u != e && mrk[n] == mrk[u]) {
vec.pb(dp(u, 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];
col(a, 0, x);
col(b, 1, x);
return cache[x] = max(dp(a), dp(b));
}
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]) : LLONG_MAX;
if (lp > split(edges[mid])) {
ans = split(edges[mid]);
lo = mid + 1;
} else {
hi = ll - 1;
}
}
cout << ans << '\n';
}
Compilation message
torrent.cpp:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9828 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2070 ms |
32204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |