This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif
const int N = 1001000;
int n, a, b, m, q;
int par[N];
int son[N];
int cost[N];
int blk[N];
int dp[N]; // ile żeby uwięzić i wyprowadzić do ojca
vector<int> v[N];
void dfs_dp(int x) {
vector<int> war = {0, 0};
for (int i : v[x]) {
if (i == par[x]) {
continue;
}
par[i] = x;
dfs_dp(i);
war.push_back(dp[i]);
}
nth_element(war.begin(), war.begin() + 1, war.end(), [&](int a, int b) {
return a > b;
});
dp[x] = war[1] + war.size() - 2;
}
bool check(int wyn) {
int x = m, nad = 1, usd = 0;
while (x != q) {
int ile = 0;
for (int i : v[x]) {
if (i != par[x] && i != son[x]) {
if (dp[i] + blk[x] + usd > wyn) {
++ile;
}
}
}
if (ile > nad) {
return false;
}
nad -= ile;
usd += ile;
x = par[x];
++nad;
}
return usd <= wyn;
}
int main() {
scanf("%d%d%d", &n, &q, &m);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &a, &b);
v[a].push_back(b);
v[b].push_back(a);
}
dfs_dp(q);
int x = m;
while (x != q) {
son[par[x]] = x;
x = par[x];
}
while (x != m) {
x = son[x];
blk[x] = blk[par[x]];
for (int i : v[x]) {
blk[x] += (i != par[x]) && (i != son[x]);
}
}
int bp = -1, bk = min(n, 1000), bs;
while (bk - bp > 1) {
bs = (bp + bk) / 2;
if (check(bs)) {
bk = bs;
}
else {
bp = bs;
}
}
printf("%d\n", bk);
return 0;
}
Compilation message (stderr)
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | scanf("%d%d%d", &n, &q, &m);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
mousetrap.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |