# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
200063 | SamAnd | Tax Evasion (LMIO19_mokesciai) | C++17 | 283 ms | 55020 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 N = 200005;
int n, m;
vector<int> a[N];
bool c[N];
int d[N];
int q[N];
vector<int> v;
void dfs(int x)
{
if (v.empty())
d[x] = 1;
else
d[x] = d[v.back()] + 1;
v.push_back(x);
if (c[x])
{
int u = (v.size() / 2) - 1;
++q[v[v.size() - u - 1]];
}
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
dfs(h);
}
v.pop_back();
}
struct ban
{
int x, d;
ban(){}
ban(int x, int d)
{
this->x = x;
this->d = d;
}
};
bool operator<(const ban& a, const ban& b)
{
return a.d < b.d;
}
priority_queue<ban> u[N];
void dfs1(int x)
{
u[x].push(ban(x, d[x]));
for (int i = 0; i < a[x].size(); ++i)
{
int h = a[x][i];
dfs1(h);
if (u[x].size() < u[h].size())
swap(u[x], u[h]);
while (!u[h].empty())
{
u[x].push(u[h].top());
u[h].pop();
}
}
for (int i = 0; i < q[x]; ++i)
{
c[u[x].top().x] = true;
u[x].pop();
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 2; i <= n; ++i)
{
int p;
scanf("%d", &p);
a[p].push_back(i);
}
for (int i = 0; i < m; ++i)
{
int x;
scanf("%d", &x);
c[x] = true;
}
if (c[1])
{
printf("1\n");
return 0;
}
dfs(1);
memset(c, false, sizeof c);
dfs1(1);
int ans = N;
for (int i = 1; i <= n; ++i)
{
if (c[i])
ans = min(ans, d[i]);
}
printf("%d\n", ans);
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... |