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 <stdio.h>
#include <vector>
std::vector<int> AdjList[1000005];
int PhaseII[1000005];
int m, t, T;
int MT[1000005];
int between;
void findMT(int u, int pa)
{
if (u == t)
{
MT[u] = t;
return;
}
int i, v, s = AdjList[u].size();
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa)
{
findMT(v, u);
if (MT[v])
MT[u] = v;
}
}
}
void calcPhaseII(int u, int pa)
{
int i, v, s = AdjList[u].size();
int S = s;
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa && MT[v])
S--;
}
if (S == 1 || S == 2)
PhaseII[u] = 1 - (pa == T);
else if (pa == T)
PhaseII[u] = S - 3;
else
PhaseII[u] = S - 2;
PhaseII[u] += PhaseII[pa];
// printf("u%d PhaseII%d\n", u, PhaseII[u]);
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa && !MT[v])
calcPhaseII(v, u);
}
}
int PhaseI[1000005];
int min(int a, int b)
{
return (a < b)?a:b;
}
void calcPhaseI(int u, int pa)
{
int i, v, s = AdjList[u].size();
int S = s;
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa && MT[v])
S--;
}
int p = PhaseII[u], q = PhaseII[u];
for (i = 0; i < s; i++)
{
v = AdjList[u][i];
if (v != pa && !MT[v])
{
calcPhaseI(v, u);
if (PhaseI[v] >= p)
{
q = p;
p = PhaseI[v];
}
else if (PhaseI[v] >= q)
q = PhaseI[v];
}
}
if (S == 1)
PhaseI[u] = q;
else
PhaseI[u] = 1 + q;
// printf("u%d PhaseI%d\n", u, PhaseI[u]);
}
int main()
{
int n;
scanf("%d %d %d", &n, &t, &m);
int i, u, v;
for (i = 0; i < n - 1; i++)
{
scanf("%d %d", &u, &v);
AdjList[u].push_back(v);
AdjList[v].push_back(u);
}
findMT(m, 0);
for (i = 1; i <= n; i++)
{
// printf("MT %d\n", MT[i]);
if (MT[i] && i != m && i != t)
between += AdjList[i].size() - 2;
}
int ans = 0;
u = m;
while (u != t)
{
T = MT[u];
calcPhaseII(u, T);
calcPhaseI(u, T);
ans += PhaseI[u];
u = MT[u];
break;
// printf("ans %d\n", ans);
}
// printf("ans %d between %d\n", ans, between);
ans += between;
printf("%d", ans);
}
Compilation message (stderr)
mousetrap.cpp: In function 'int main()':
mousetrap.cpp:92:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &n, &t, &m);
^
mousetrap.cpp:96:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
^
# | 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... |