# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
110005 |
2019-05-08T16:03:47 Z |
luciocf |
Mergers (JOI19_mergers) |
C++17 |
|
1585 ms |
77532 KB |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+10;
int n, k;
int pai[maxn], peso[maxn];
int s[maxn], tot[maxn];
int in[maxn], out[maxn], val[maxn], sz[maxn], tt;
int qtdZero, qtdTot, sub[maxn];
int grau[maxn];
vector<int> grafo[maxn];
void Init(void)
{
for (int i = 1; i <= n; i++)
pai[i] = i, peso[i] = 1;
}
int Find(int x)
{
if (pai[x] == x) return x;
return pai[x] = Find(pai[x]);
}
void Join(int x, int y)
{
x = Find(x), y = Find(y);
if (x == y) return;
if (peso[x] < peso[y]) swap(x, y);
pai[y] = x, peso[x] += peso[y];
}
void dfs(int u, int p)
{
sz[u] = 1;
in[u] = ++tt, val[tt] = s[u];
for (auto v: grafo[u])
{
if (v == p) continue;
dfs(v, u);
sz[u] += sz[v];
}
out[u] = tt;
}
void solve(int u, int p, bool heavy)
{
int maior = -1, ind = -1;
for (auto v: grafo[u])
if (v != p && sz[v] > maior)
maior = sz[v], ind = v;
for (auto v: grafo[u])
if (v != p && v != ind)
solve(v, u, 0);
if (ind != -1)
solve(ind, u, 1);
--sub[s[u]];
if (sub[s[u]] == 0)
++qtdZero;
if (sub[s[u]] == tot[s[u]]-1)
--qtdTot;
for (auto v: grafo[u])
{
if (v != p && v != ind)
{
for (int i = in[v]; i <= out[v]; i++)
{
--sub[val[i]];
if (sub[val[i]] == 0)
++qtdZero;
if (sub[val[i]] == tot[val[i]]-1)
--qtdTot;
}
}
}
if (qtdZero+qtdTot < k)
Join(u, p);
if (!heavy)
{
for (int i = in[u]; i <= out[u]; i++)
{
++sub[val[i]];
if (sub[val[i]] == 1)
--qtdZero;
if (sub[val[i]] == tot[val[i]])
++qtdTot;
}
}
}
int main(void)
{
scanf("%d %d", &n, &k);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d %d", &u, &v);
grafo[u].push_back(v);
grafo[v].push_back(u);
}
for (int i = 1; i <= n; i++)
{
scanf("%d", &s[i]);
tot[s[i]]++;
sub[s[i]]++;
}
qtdTot = k;
Init();
dfs(1, 0);
solve(1, 0, 0);
for (int i = 1; i <= n; i++)
for (auto v: grafo[i])
if (Find(v) != Find(i))
grau[Find(v)]++;
int leaf = 0;
for (int i = 1; i <= n; i++)
if (grau[i] == 1)
leaf++;
printf("%d\n", (leaf+1)/2);
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:119:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:124:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
mergers.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &s[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
13 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
15 ms |
12160 KB |
Output is correct |
6 |
Correct |
14 ms |
12160 KB |
Output is correct |
7 |
Correct |
13 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
17 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12288 KB |
Output is correct |
12 |
Correct |
14 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
14 ms |
12160 KB |
Output is correct |
16 |
Correct |
14 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12140 KB |
Output is correct |
18 |
Correct |
15 ms |
12288 KB |
Output is correct |
19 |
Correct |
14 ms |
12160 KB |
Output is correct |
20 |
Correct |
13 ms |
12160 KB |
Output is correct |
21 |
Correct |
13 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12160 KB |
Output is correct |
23 |
Correct |
14 ms |
12160 KB |
Output is correct |
24 |
Correct |
14 ms |
12160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
13 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
15 ms |
12160 KB |
Output is correct |
6 |
Correct |
14 ms |
12160 KB |
Output is correct |
7 |
Correct |
13 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
17 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12288 KB |
Output is correct |
12 |
Correct |
14 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
14 ms |
12160 KB |
Output is correct |
16 |
Correct |
14 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12140 KB |
Output is correct |
18 |
Correct |
15 ms |
12288 KB |
Output is correct |
19 |
Correct |
14 ms |
12160 KB |
Output is correct |
20 |
Correct |
13 ms |
12160 KB |
Output is correct |
21 |
Correct |
13 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12160 KB |
Output is correct |
23 |
Correct |
14 ms |
12160 KB |
Output is correct |
24 |
Correct |
14 ms |
12160 KB |
Output is correct |
25 |
Correct |
14 ms |
12160 KB |
Output is correct |
26 |
Correct |
18 ms |
12408 KB |
Output is correct |
27 |
Correct |
15 ms |
12288 KB |
Output is correct |
28 |
Correct |
20 ms |
12544 KB |
Output is correct |
29 |
Correct |
15 ms |
12416 KB |
Output is correct |
30 |
Correct |
17 ms |
12416 KB |
Output is correct |
31 |
Correct |
13 ms |
12160 KB |
Output is correct |
32 |
Correct |
15 ms |
12544 KB |
Output is correct |
33 |
Correct |
14 ms |
12132 KB |
Output is correct |
34 |
Correct |
15 ms |
12288 KB |
Output is correct |
35 |
Correct |
18 ms |
12408 KB |
Output is correct |
36 |
Correct |
17 ms |
12416 KB |
Output is correct |
37 |
Correct |
16 ms |
12288 KB |
Output is correct |
38 |
Correct |
12 ms |
12160 KB |
Output is correct |
39 |
Correct |
16 ms |
12288 KB |
Output is correct |
40 |
Correct |
20 ms |
12416 KB |
Output is correct |
41 |
Correct |
17 ms |
12288 KB |
Output is correct |
42 |
Correct |
15 ms |
12416 KB |
Output is correct |
43 |
Correct |
17 ms |
12544 KB |
Output is correct |
44 |
Correct |
14 ms |
12160 KB |
Output is correct |
45 |
Correct |
17 ms |
12520 KB |
Output is correct |
46 |
Correct |
20 ms |
12288 KB |
Output is correct |
47 |
Correct |
14 ms |
12160 KB |
Output is correct |
48 |
Correct |
16 ms |
12284 KB |
Output is correct |
49 |
Correct |
18 ms |
12416 KB |
Output is correct |
50 |
Correct |
18 ms |
12408 KB |
Output is correct |
51 |
Correct |
16 ms |
12416 KB |
Output is correct |
52 |
Correct |
15 ms |
12288 KB |
Output is correct |
53 |
Correct |
15 ms |
12420 KB |
Output is correct |
54 |
Correct |
15 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
13 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
15 ms |
12160 KB |
Output is correct |
6 |
Correct |
14 ms |
12160 KB |
Output is correct |
7 |
Correct |
13 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
17 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12288 KB |
Output is correct |
12 |
Correct |
14 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
14 ms |
12160 KB |
Output is correct |
16 |
Correct |
14 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12140 KB |
Output is correct |
18 |
Correct |
15 ms |
12288 KB |
Output is correct |
19 |
Correct |
14 ms |
12160 KB |
Output is correct |
20 |
Correct |
13 ms |
12160 KB |
Output is correct |
21 |
Correct |
13 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12160 KB |
Output is correct |
23 |
Correct |
14 ms |
12160 KB |
Output is correct |
24 |
Correct |
14 ms |
12160 KB |
Output is correct |
25 |
Correct |
14 ms |
12160 KB |
Output is correct |
26 |
Correct |
90 ms |
18672 KB |
Output is correct |
27 |
Correct |
137 ms |
18552 KB |
Output is correct |
28 |
Correct |
15 ms |
12288 KB |
Output is correct |
29 |
Correct |
13 ms |
12288 KB |
Output is correct |
30 |
Correct |
12 ms |
12160 KB |
Output is correct |
31 |
Correct |
134 ms |
18552 KB |
Output is correct |
32 |
Correct |
18 ms |
12416 KB |
Output is correct |
33 |
Correct |
182 ms |
27080 KB |
Output is correct |
34 |
Correct |
166 ms |
18552 KB |
Output is correct |
35 |
Correct |
15 ms |
12408 KB |
Output is correct |
36 |
Correct |
163 ms |
19392 KB |
Output is correct |
37 |
Correct |
17 ms |
12416 KB |
Output is correct |
38 |
Correct |
16 ms |
12416 KB |
Output is correct |
39 |
Correct |
89 ms |
18792 KB |
Output is correct |
40 |
Correct |
16 ms |
12544 KB |
Output is correct |
41 |
Correct |
158 ms |
18424 KB |
Output is correct |
42 |
Correct |
188 ms |
21292 KB |
Output is correct |
43 |
Correct |
15 ms |
12160 KB |
Output is correct |
44 |
Correct |
181 ms |
27520 KB |
Output is correct |
45 |
Correct |
152 ms |
22904 KB |
Output is correct |
46 |
Correct |
15 ms |
12288 KB |
Output is correct |
47 |
Correct |
15 ms |
12388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
18672 KB |
Output is correct |
2 |
Correct |
110 ms |
19980 KB |
Output is correct |
3 |
Correct |
15 ms |
12408 KB |
Output is correct |
4 |
Correct |
15 ms |
12416 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
12 ms |
12160 KB |
Output is correct |
7 |
Correct |
14 ms |
12288 KB |
Output is correct |
8 |
Correct |
153 ms |
19192 KB |
Output is correct |
9 |
Correct |
15 ms |
12288 KB |
Output is correct |
10 |
Correct |
170 ms |
18936 KB |
Output is correct |
11 |
Correct |
12 ms |
12160 KB |
Output is correct |
12 |
Correct |
166 ms |
18744 KB |
Output is correct |
13 |
Correct |
152 ms |
19100 KB |
Output is correct |
14 |
Correct |
183 ms |
19576 KB |
Output is correct |
15 |
Correct |
85 ms |
18672 KB |
Output is correct |
16 |
Correct |
17 ms |
12416 KB |
Output is correct |
17 |
Correct |
12 ms |
12160 KB |
Output is correct |
18 |
Correct |
120 ms |
19812 KB |
Output is correct |
19 |
Correct |
171 ms |
24308 KB |
Output is correct |
20 |
Correct |
15 ms |
12424 KB |
Output is correct |
21 |
Correct |
12 ms |
12160 KB |
Output is correct |
22 |
Correct |
100 ms |
19480 KB |
Output is correct |
23 |
Correct |
15 ms |
12288 KB |
Output is correct |
24 |
Correct |
167 ms |
18824 KB |
Output is correct |
25 |
Correct |
153 ms |
22648 KB |
Output is correct |
26 |
Correct |
15 ms |
12416 KB |
Output is correct |
27 |
Correct |
15 ms |
12416 KB |
Output is correct |
28 |
Correct |
20 ms |
12288 KB |
Output is correct |
29 |
Correct |
16 ms |
12416 KB |
Output is correct |
30 |
Correct |
16 ms |
12424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12160 KB |
Output is correct |
2 |
Correct |
12 ms |
12160 KB |
Output is correct |
3 |
Correct |
13 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
15 ms |
12160 KB |
Output is correct |
6 |
Correct |
14 ms |
12160 KB |
Output is correct |
7 |
Correct |
13 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
12 ms |
12160 KB |
Output is correct |
10 |
Correct |
17 ms |
12160 KB |
Output is correct |
11 |
Correct |
13 ms |
12288 KB |
Output is correct |
12 |
Correct |
14 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
15 ms |
12160 KB |
Output is correct |
15 |
Correct |
14 ms |
12160 KB |
Output is correct |
16 |
Correct |
14 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12140 KB |
Output is correct |
18 |
Correct |
15 ms |
12288 KB |
Output is correct |
19 |
Correct |
14 ms |
12160 KB |
Output is correct |
20 |
Correct |
13 ms |
12160 KB |
Output is correct |
21 |
Correct |
13 ms |
12160 KB |
Output is correct |
22 |
Correct |
13 ms |
12160 KB |
Output is correct |
23 |
Correct |
14 ms |
12160 KB |
Output is correct |
24 |
Correct |
14 ms |
12160 KB |
Output is correct |
25 |
Correct |
14 ms |
12160 KB |
Output is correct |
26 |
Correct |
18 ms |
12408 KB |
Output is correct |
27 |
Correct |
15 ms |
12288 KB |
Output is correct |
28 |
Correct |
20 ms |
12544 KB |
Output is correct |
29 |
Correct |
15 ms |
12416 KB |
Output is correct |
30 |
Correct |
17 ms |
12416 KB |
Output is correct |
31 |
Correct |
13 ms |
12160 KB |
Output is correct |
32 |
Correct |
15 ms |
12544 KB |
Output is correct |
33 |
Correct |
14 ms |
12132 KB |
Output is correct |
34 |
Correct |
15 ms |
12288 KB |
Output is correct |
35 |
Correct |
18 ms |
12408 KB |
Output is correct |
36 |
Correct |
17 ms |
12416 KB |
Output is correct |
37 |
Correct |
16 ms |
12288 KB |
Output is correct |
38 |
Correct |
12 ms |
12160 KB |
Output is correct |
39 |
Correct |
16 ms |
12288 KB |
Output is correct |
40 |
Correct |
20 ms |
12416 KB |
Output is correct |
41 |
Correct |
17 ms |
12288 KB |
Output is correct |
42 |
Correct |
15 ms |
12416 KB |
Output is correct |
43 |
Correct |
17 ms |
12544 KB |
Output is correct |
44 |
Correct |
14 ms |
12160 KB |
Output is correct |
45 |
Correct |
17 ms |
12520 KB |
Output is correct |
46 |
Correct |
20 ms |
12288 KB |
Output is correct |
47 |
Correct |
14 ms |
12160 KB |
Output is correct |
48 |
Correct |
16 ms |
12284 KB |
Output is correct |
49 |
Correct |
18 ms |
12416 KB |
Output is correct |
50 |
Correct |
18 ms |
12408 KB |
Output is correct |
51 |
Correct |
16 ms |
12416 KB |
Output is correct |
52 |
Correct |
15 ms |
12288 KB |
Output is correct |
53 |
Correct |
15 ms |
12420 KB |
Output is correct |
54 |
Correct |
15 ms |
12288 KB |
Output is correct |
55 |
Correct |
14 ms |
12160 KB |
Output is correct |
56 |
Correct |
90 ms |
18672 KB |
Output is correct |
57 |
Correct |
137 ms |
18552 KB |
Output is correct |
58 |
Correct |
15 ms |
12288 KB |
Output is correct |
59 |
Correct |
13 ms |
12288 KB |
Output is correct |
60 |
Correct |
12 ms |
12160 KB |
Output is correct |
61 |
Correct |
134 ms |
18552 KB |
Output is correct |
62 |
Correct |
18 ms |
12416 KB |
Output is correct |
63 |
Correct |
182 ms |
27080 KB |
Output is correct |
64 |
Correct |
166 ms |
18552 KB |
Output is correct |
65 |
Correct |
15 ms |
12408 KB |
Output is correct |
66 |
Correct |
163 ms |
19392 KB |
Output is correct |
67 |
Correct |
17 ms |
12416 KB |
Output is correct |
68 |
Correct |
16 ms |
12416 KB |
Output is correct |
69 |
Correct |
89 ms |
18792 KB |
Output is correct |
70 |
Correct |
16 ms |
12544 KB |
Output is correct |
71 |
Correct |
158 ms |
18424 KB |
Output is correct |
72 |
Correct |
188 ms |
21292 KB |
Output is correct |
73 |
Correct |
15 ms |
12160 KB |
Output is correct |
74 |
Correct |
181 ms |
27520 KB |
Output is correct |
75 |
Correct |
152 ms |
22904 KB |
Output is correct |
76 |
Correct |
15 ms |
12288 KB |
Output is correct |
77 |
Correct |
15 ms |
12388 KB |
Output is correct |
78 |
Correct |
83 ms |
18672 KB |
Output is correct |
79 |
Correct |
110 ms |
19980 KB |
Output is correct |
80 |
Correct |
15 ms |
12408 KB |
Output is correct |
81 |
Correct |
15 ms |
12416 KB |
Output is correct |
82 |
Correct |
12 ms |
12160 KB |
Output is correct |
83 |
Correct |
12 ms |
12160 KB |
Output is correct |
84 |
Correct |
14 ms |
12288 KB |
Output is correct |
85 |
Correct |
153 ms |
19192 KB |
Output is correct |
86 |
Correct |
15 ms |
12288 KB |
Output is correct |
87 |
Correct |
170 ms |
18936 KB |
Output is correct |
88 |
Correct |
12 ms |
12160 KB |
Output is correct |
89 |
Correct |
166 ms |
18744 KB |
Output is correct |
90 |
Correct |
152 ms |
19100 KB |
Output is correct |
91 |
Correct |
183 ms |
19576 KB |
Output is correct |
92 |
Correct |
85 ms |
18672 KB |
Output is correct |
93 |
Correct |
17 ms |
12416 KB |
Output is correct |
94 |
Correct |
12 ms |
12160 KB |
Output is correct |
95 |
Correct |
120 ms |
19812 KB |
Output is correct |
96 |
Correct |
171 ms |
24308 KB |
Output is correct |
97 |
Correct |
15 ms |
12424 KB |
Output is correct |
98 |
Correct |
12 ms |
12160 KB |
Output is correct |
99 |
Correct |
100 ms |
19480 KB |
Output is correct |
100 |
Correct |
15 ms |
12288 KB |
Output is correct |
101 |
Correct |
167 ms |
18824 KB |
Output is correct |
102 |
Correct |
153 ms |
22648 KB |
Output is correct |
103 |
Correct |
15 ms |
12416 KB |
Output is correct |
104 |
Correct |
15 ms |
12416 KB |
Output is correct |
105 |
Correct |
20 ms |
12288 KB |
Output is correct |
106 |
Correct |
16 ms |
12416 KB |
Output is correct |
107 |
Correct |
16 ms |
12424 KB |
Output is correct |
108 |
Correct |
952 ms |
47720 KB |
Output is correct |
109 |
Correct |
1294 ms |
64328 KB |
Output is correct |
110 |
Correct |
1303 ms |
77532 KB |
Output is correct |
111 |
Correct |
1438 ms |
76268 KB |
Output is correct |
112 |
Correct |
1389 ms |
64536 KB |
Output is correct |
113 |
Correct |
939 ms |
49652 KB |
Output is correct |
114 |
Correct |
1228 ms |
42516 KB |
Output is correct |
115 |
Correct |
1201 ms |
42512 KB |
Output is correct |
116 |
Correct |
1369 ms |
45096 KB |
Output is correct |
117 |
Correct |
1400 ms |
48476 KB |
Output is correct |
118 |
Correct |
1077 ms |
43444 KB |
Output is correct |
119 |
Correct |
1425 ms |
48528 KB |
Output is correct |
120 |
Correct |
1585 ms |
65100 KB |
Output is correct |
121 |
Correct |
1361 ms |
48216 KB |
Output is correct |
122 |
Correct |
1317 ms |
46668 KB |
Output is correct |
123 |
Correct |
978 ms |
49764 KB |
Output is correct |
124 |
Correct |
1316 ms |
47552 KB |
Output is correct |