Submission #253980

# Submission time Handle Problem Language Result Execution time Memory
253980 2020-07-29T08:11:34 Z Berted Tax Evasion (LMIO19_mokesciai) C++14
100 / 100
223 ms 67180 KB
#include <iostream>
#include <set>
#include <vector>
#include <assert.h>
#include <map>
#define vi vector<int>
#define pub push_back
#define pii pair<int, int>
#define fst first
#define snd second
using namespace std;

int n, k, cnt[200001] = {}, mn = 1e9;
bool spc[200001];
vi adj[200001];
vi par;

void dfs(int u, int p)
{
	int h = par.size();
	par.push_back(u);
	cnt[par[h / 2 + 1]] += spc[u];
	spc[u] = 0;
	for (auto v : adj[u])
	{
		if (v != p) dfs(v, u);
	}
	par.pop_back();
}


map<int, int>* dfs2(int u, int p, int h)
{
	map<int, int>* ret = new map<int, int>;
	(*ret)[-h]++;
	for (auto v : adj[u])
	{
		if (v != p)
		{
			map<int, int>* rr = dfs2(v, u, h + 1);
			if (rr -> size() > ret -> size()) {swap(rr, ret);}
			for (auto v : *rr) {(*ret)[v.fst] += v.snd;}
		}
	}
	// assert(cnt[u] <= ret -> size());
	for (int i = 0; i < cnt[u]; i++)
	{
		mn = min(mn, -(ret -> begin() -> fst));
		(ret -> begin() -> snd)--;
		if (ret -> begin() -> snd == 0) {ret -> erase(ret -> begin());}
	}
	return ret;
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i < n; i++)
	{
		int v; cin >> v;
		adj[v - 1].push_back(i);
		adj[i].push_back(v - 1);
	}
	for (int i = 0; i < k; i++)
	{
		int x; cin >> x; spc[x - 1] = 1;
	}
	if (spc[0]) {cout << "1\n";}
	else
	{
		dfs(0, -1); dfs2(0 ,-1, 0);
		cout << mn + 1 << '\n';
	}
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4992 KB Output is correct
2 Correct 3 ms 4992 KB Output is correct
3 Correct 6 ms 6528 KB Output is correct
4 Correct 6 ms 6528 KB Output is correct
5 Correct 6 ms 6528 KB Output is correct
6 Correct 137 ms 67180 KB Output is correct
7 Correct 132 ms 67176 KB Output is correct
8 Correct 6 ms 6528 KB Output is correct
9 Correct 157 ms 63444 KB Output is correct
10 Correct 157 ms 63720 KB Output is correct
11 Correct 143 ms 65388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4992 KB Output is correct
2 Correct 6 ms 6144 KB Output is correct
3 Correct 6 ms 6144 KB Output is correct
4 Correct 5 ms 5888 KB Output is correct
5 Correct 6 ms 6144 KB Output is correct
6 Correct 6 ms 5888 KB Output is correct
7 Correct 137 ms 67180 KB Output is correct
8 Correct 132 ms 67176 KB Output is correct
9 Correct 6 ms 6528 KB Output is correct
10 Correct 105 ms 49900 KB Output is correct
11 Correct 110 ms 49900 KB Output is correct
12 Correct 125 ms 39024 KB Output is correct
13 Correct 117 ms 54640 KB Output is correct
14 Correct 87 ms 39288 KB Output is correct
15 Correct 136 ms 38776 KB Output is correct
16 Correct 116 ms 54640 KB Output is correct
17 Correct 223 ms 40568 KB Output is correct
18 Correct 139 ms 57972 KB Output is correct
19 Correct 121 ms 45820 KB Output is correct
20 Correct 113 ms 44668 KB Output is correct
21 Correct 110 ms 44792 KB Output is correct
22 Correct 112 ms 44536 KB Output is correct
23 Correct 103 ms 43752 KB Output is correct
24 Correct 108 ms 44408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 5 ms 6144 KB Output is correct
7 Correct 5 ms 6144 KB Output is correct
8 Correct 6 ms 6144 KB Output is correct
9 Correct 6 ms 5888 KB Output is correct
10 Correct 5 ms 5888 KB Output is correct
11 Correct 7 ms 6272 KB Output is correct
12 Correct 6 ms 5888 KB Output is correct
13 Correct 6 ms 6144 KB Output is correct
14 Correct 5 ms 5888 KB Output is correct
15 Correct 6 ms 6272 KB Output is correct
16 Correct 6 ms 6272 KB Output is correct
17 Correct 7 ms 6016 KB Output is correct
18 Correct 6 ms 6144 KB Output is correct
19 Correct 7 ms 5888 KB Output is correct
20 Correct 8 ms 5888 KB Output is correct
21 Correct 5 ms 5888 KB Output is correct
22 Correct 3 ms 4992 KB Output is correct
23 Correct 3 ms 4992 KB Output is correct
24 Correct 6 ms 6528 KB Output is correct
25 Correct 6 ms 6528 KB Output is correct
26 Correct 6 ms 6528 KB Output is correct
27 Correct 4 ms 4992 KB Output is correct
28 Correct 6 ms 6144 KB Output is correct
29 Correct 6 ms 6144 KB Output is correct
30 Correct 5 ms 5888 KB Output is correct
31 Correct 6 ms 6144 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 6 ms 6528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 5120 KB Output is correct
2 Correct 3 ms 5120 KB Output is correct
3 Correct 3 ms 4992 KB Output is correct
4 Correct 3 ms 4992 KB Output is correct
5 Correct 3 ms 4992 KB Output is correct
6 Correct 5 ms 6144 KB Output is correct
7 Correct 5 ms 6144 KB Output is correct
8 Correct 6 ms 6144 KB Output is correct
9 Correct 6 ms 5888 KB Output is correct
10 Correct 5 ms 5888 KB Output is correct
11 Correct 7 ms 6272 KB Output is correct
12 Correct 6 ms 5888 KB Output is correct
13 Correct 6 ms 6144 KB Output is correct
14 Correct 5 ms 5888 KB Output is correct
15 Correct 6 ms 6272 KB Output is correct
16 Correct 6 ms 6272 KB Output is correct
17 Correct 7 ms 6016 KB Output is correct
18 Correct 6 ms 6144 KB Output is correct
19 Correct 7 ms 5888 KB Output is correct
20 Correct 8 ms 5888 KB Output is correct
21 Correct 5 ms 5888 KB Output is correct
22 Correct 3 ms 4992 KB Output is correct
23 Correct 3 ms 4992 KB Output is correct
24 Correct 6 ms 6528 KB Output is correct
25 Correct 6 ms 6528 KB Output is correct
26 Correct 6 ms 6528 KB Output is correct
27 Correct 4 ms 4992 KB Output is correct
28 Correct 6 ms 6144 KB Output is correct
29 Correct 6 ms 6144 KB Output is correct
30 Correct 5 ms 5888 KB Output is correct
31 Correct 6 ms 6144 KB Output is correct
32 Correct 6 ms 5888 KB Output is correct
33 Correct 137 ms 67180 KB Output is correct
34 Correct 132 ms 67176 KB Output is correct
35 Correct 6 ms 6528 KB Output is correct
36 Correct 105 ms 49900 KB Output is correct
37 Correct 110 ms 49900 KB Output is correct
38 Correct 125 ms 39024 KB Output is correct
39 Correct 117 ms 54640 KB Output is correct
40 Correct 87 ms 39288 KB Output is correct
41 Correct 136 ms 38776 KB Output is correct
42 Correct 116 ms 54640 KB Output is correct
43 Correct 223 ms 40568 KB Output is correct
44 Correct 139 ms 57972 KB Output is correct
45 Correct 121 ms 45820 KB Output is correct
46 Correct 113 ms 44668 KB Output is correct
47 Correct 110 ms 44792 KB Output is correct
48 Correct 112 ms 44536 KB Output is correct
49 Correct 103 ms 43752 KB Output is correct
50 Correct 108 ms 44408 KB Output is correct
51 Correct 157 ms 63444 KB Output is correct
52 Correct 157 ms 63720 KB Output is correct
53 Correct 143 ms 65388 KB Output is correct
54 Correct 111 ms 50156 KB Output is correct
55 Correct 116 ms 50156 KB Output is correct
56 Correct 114 ms 51772 KB Output is correct
57 Correct 107 ms 40056 KB Output is correct
58 Correct 125 ms 38768 KB Output is correct
59 Correct 123 ms 46836 KB Output is correct
60 Correct 142 ms 37240 KB Output is correct
61 Correct 138 ms 51952 KB Output is correct
62 Correct 171 ms 40824 KB Output is correct
63 Correct 141 ms 55660 KB Output is correct
64 Correct 126 ms 44536 KB Output is correct
65 Correct 119 ms 43896 KB Output is correct
66 Correct 116 ms 41464 KB Output is correct
67 Correct 120 ms 43000 KB Output is correct
68 Correct 116 ms 43768 KB Output is correct
69 Correct 121 ms 39288 KB Output is correct
70 Correct 61 ms 23544 KB Output is correct
71 Correct 108 ms 44276 KB Output is correct