# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
303060 | Kenzo_1114 | Mousetrap (CEOI17_mousetrap) | C++17 | 911 ms | 64148 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;
const int MAXN = 1000010;
int n, t, m, dp[MAXN], par[MAXN];
vector<int> graph[MAXN];
void dfs(int v, int p)
{
int mx[2] = {-1, -1};
par[v] = p;
for(int i = 0; i < graph[v].size(); i++)
{
int u = graph[v][i];
if(u == p) continue;
dfs(u, v);
if(dp[u] >= mx[0]) mx[1] = mx[0], mx[0] = dp[u];
else if(dp[u] > mx[1]) mx[1] = dp[u];
}
dp[v] = ((int) graph[v].size() == 2) ? 1 : mx[1] + graph[v].size() - 1;
if((int) graph[v].size() == 1) dp[v] = 0;
}
int main ()
{
scanf("%d %d %d", &n, &t, &m);
for(int i = 0, u, v; i < n - 1; i++)
{
scanf("%d %d", &u, &v);
graph[u].push_back(v);
graph[v].push_back(u);
}
if(t == m || (int) graph[m].size() == 0)
{ printf("0\n"); return 0; }
dfs(t, t);
int cur = m, ans = dp[m];
while(true)
{
cur = par[cur];
if(cur == t) break;
ans += (int) graph[cur].size() - 2;
}
// for(int i = 1; i <= n; i++) printf("dp[%d] = %d\n", i, dp[i]);
printf("%d\n", ans);
}
/*
10 1 4
1 2
2 3
2 4
3 9
3 5
4 7
4 6
6 8
7 10
*/
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... |