# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
96328 | Bruteforceman | Mousetrap (CEOI17_mousetrap) | C++11 | 1062 ms | 81400 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
vector <int> g[1000010];
int dp[1000010], cn[1000010];
int dep[1000010];
int m, t;
void dfs(int x, int par) {
if(x == t) {
cn[x] = 1;
dp[x] = 0;
dep[x] = 0;
return ;
}
int deg = 0;
int sib = 0;
for(auto i : g[x]) {
if(i - par) {
dfs(i, x);
cn[x] |= cn[i];
++deg;
if(cn[i]) {
dep[x] = dep[i];
sib = i;
}
}
}
dep[x] += deg - 1;
vector <int> can;
for(auto i : g[x]) {
if(i - par) {
if(cn[i]) {
can.emplace_back(dp[i]);
} else {
if(cn[x]) {
can.emplace_back(dp[i] + dep[x]);
} else {
can.emplace_back(dp[i] + deg);
}
}
}
}
sort(can.begin(), can.end());
reverse(can.begin(), can.end());
if(cn[x]) {
dp[x] = dp[sib];
} else {
dp[x] = 0;
}
if(can.size() > 1) {
dp[x] = max(dp[x], can[1]);
} else if (!cn[x] && can.size() == 1) {
dp[x] = 1;
}
}
int main(int argc, char const *argv[])
{
int n;
scanf("%d %d %d", &n, &t, &m);
for(int i = 1; i < n; i++) {
int p, q;
scanf("%d %d", &p, &q);
g[p].emplace_back(q);
g[q].emplace_back(p);
}
dfs(m, 0);
printf("%d\n", dp[m]);
return 0;
}
Compilation message (stderr)
# | 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... |