# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
110004 |
2019-05-08T16:01:31 Z |
luciocf |
Mergers (JOI19_mergers) |
C++14 |
|
1736 ms |
84980 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:8: 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:9: 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:9: 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 |
15 ms |
12160 KB |
Output is correct |
2 |
Correct |
13 ms |
12160 KB |
Output is correct |
3 |
Correct |
14 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
16 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
15 ms |
12092 KB |
Output is correct |
10 |
Correct |
15 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
15 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
14 ms |
12160 KB |
Output is correct |
15 |
Correct |
13 ms |
12160 KB |
Output is correct |
16 |
Correct |
13 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12160 KB |
Output is correct |
18 |
Correct |
13 ms |
12160 KB |
Output is correct |
19 |
Correct |
18 ms |
12160 KB |
Output is correct |
20 |
Correct |
14 ms |
12160 KB |
Output is correct |
21 |
Correct |
15 ms |
12288 KB |
Output is correct |
22 |
Correct |
14 ms |
12160 KB |
Output is correct |
23 |
Correct |
13 ms |
12160 KB |
Output is correct |
24 |
Correct |
16 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12160 KB |
Output is correct |
2 |
Correct |
13 ms |
12160 KB |
Output is correct |
3 |
Correct |
14 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
16 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
15 ms |
12092 KB |
Output is correct |
10 |
Correct |
15 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
15 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
14 ms |
12160 KB |
Output is correct |
15 |
Correct |
13 ms |
12160 KB |
Output is correct |
16 |
Correct |
13 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12160 KB |
Output is correct |
18 |
Correct |
13 ms |
12160 KB |
Output is correct |
19 |
Correct |
18 ms |
12160 KB |
Output is correct |
20 |
Correct |
14 ms |
12160 KB |
Output is correct |
21 |
Correct |
15 ms |
12288 KB |
Output is correct |
22 |
Correct |
14 ms |
12160 KB |
Output is correct |
23 |
Correct |
13 ms |
12160 KB |
Output is correct |
24 |
Correct |
16 ms |
12288 KB |
Output is correct |
25 |
Correct |
13 ms |
12160 KB |
Output is correct |
26 |
Correct |
15 ms |
12416 KB |
Output is correct |
27 |
Correct |
16 ms |
12288 KB |
Output is correct |
28 |
Correct |
18 ms |
12544 KB |
Output is correct |
29 |
Correct |
15 ms |
12416 KB |
Output is correct |
30 |
Correct |
16 ms |
12416 KB |
Output is correct |
31 |
Correct |
16 ms |
12080 KB |
Output is correct |
32 |
Correct |
20 ms |
12544 KB |
Output is correct |
33 |
Correct |
15 ms |
12160 KB |
Output is correct |
34 |
Correct |
19 ms |
12416 KB |
Output is correct |
35 |
Correct |
17 ms |
12416 KB |
Output is correct |
36 |
Correct |
17 ms |
12416 KB |
Output is correct |
37 |
Correct |
20 ms |
12288 KB |
Output is correct |
38 |
Correct |
13 ms |
12160 KB |
Output is correct |
39 |
Correct |
19 ms |
12344 KB |
Output is correct |
40 |
Correct |
18 ms |
12288 KB |
Output is correct |
41 |
Correct |
21 ms |
12416 KB |
Output is correct |
42 |
Correct |
16 ms |
12416 KB |
Output is correct |
43 |
Correct |
17 ms |
12544 KB |
Output is correct |
44 |
Correct |
15 ms |
12160 KB |
Output is correct |
45 |
Correct |
16 ms |
12416 KB |
Output is correct |
46 |
Correct |
15 ms |
12416 KB |
Output is correct |
47 |
Correct |
13 ms |
12160 KB |
Output is correct |
48 |
Correct |
19 ms |
12416 KB |
Output is correct |
49 |
Correct |
16 ms |
12416 KB |
Output is correct |
50 |
Correct |
18 ms |
12544 KB |
Output is correct |
51 |
Correct |
16 ms |
12408 KB |
Output is correct |
52 |
Correct |
16 ms |
12280 KB |
Output is correct |
53 |
Correct |
16 ms |
12416 KB |
Output is correct |
54 |
Correct |
17 ms |
12288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12160 KB |
Output is correct |
2 |
Correct |
13 ms |
12160 KB |
Output is correct |
3 |
Correct |
14 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
16 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
15 ms |
12092 KB |
Output is correct |
10 |
Correct |
15 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
15 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
14 ms |
12160 KB |
Output is correct |
15 |
Correct |
13 ms |
12160 KB |
Output is correct |
16 |
Correct |
13 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12160 KB |
Output is correct |
18 |
Correct |
13 ms |
12160 KB |
Output is correct |
19 |
Correct |
18 ms |
12160 KB |
Output is correct |
20 |
Correct |
14 ms |
12160 KB |
Output is correct |
21 |
Correct |
15 ms |
12288 KB |
Output is correct |
22 |
Correct |
14 ms |
12160 KB |
Output is correct |
23 |
Correct |
13 ms |
12160 KB |
Output is correct |
24 |
Correct |
16 ms |
12288 KB |
Output is correct |
25 |
Correct |
13 ms |
12160 KB |
Output is correct |
26 |
Correct |
120 ms |
18596 KB |
Output is correct |
27 |
Correct |
200 ms |
18204 KB |
Output is correct |
28 |
Correct |
18 ms |
12416 KB |
Output is correct |
29 |
Correct |
16 ms |
12160 KB |
Output is correct |
30 |
Correct |
14 ms |
12160 KB |
Output is correct |
31 |
Correct |
156 ms |
18424 KB |
Output is correct |
32 |
Correct |
18 ms |
12544 KB |
Output is correct |
33 |
Correct |
190 ms |
26872 KB |
Output is correct |
34 |
Correct |
157 ms |
18296 KB |
Output is correct |
35 |
Correct |
17 ms |
12416 KB |
Output is correct |
36 |
Correct |
181 ms |
19292 KB |
Output is correct |
37 |
Correct |
18 ms |
12288 KB |
Output is correct |
38 |
Correct |
17 ms |
12328 KB |
Output is correct |
39 |
Correct |
132 ms |
18744 KB |
Output is correct |
40 |
Correct |
17 ms |
12672 KB |
Output is correct |
41 |
Correct |
189 ms |
18268 KB |
Output is correct |
42 |
Correct |
187 ms |
21152 KB |
Output is correct |
43 |
Correct |
14 ms |
12160 KB |
Output is correct |
44 |
Correct |
179 ms |
27256 KB |
Output is correct |
45 |
Correct |
159 ms |
22776 KB |
Output is correct |
46 |
Correct |
18 ms |
12408 KB |
Output is correct |
47 |
Correct |
17 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
18612 KB |
Output is correct |
2 |
Correct |
113 ms |
19736 KB |
Output is correct |
3 |
Correct |
18 ms |
12416 KB |
Output is correct |
4 |
Correct |
17 ms |
12416 KB |
Output is correct |
5 |
Correct |
16 ms |
12160 KB |
Output is correct |
6 |
Correct |
14 ms |
12160 KB |
Output is correct |
7 |
Correct |
16 ms |
12288 KB |
Output is correct |
8 |
Correct |
205 ms |
19116 KB |
Output is correct |
9 |
Correct |
19 ms |
12416 KB |
Output is correct |
10 |
Correct |
191 ms |
18652 KB |
Output is correct |
11 |
Correct |
13 ms |
12160 KB |
Output is correct |
12 |
Correct |
168 ms |
18764 KB |
Output is correct |
13 |
Correct |
190 ms |
18936 KB |
Output is correct |
14 |
Correct |
167 ms |
19448 KB |
Output is correct |
15 |
Correct |
114 ms |
18544 KB |
Output is correct |
16 |
Correct |
16 ms |
12416 KB |
Output is correct |
17 |
Correct |
17 ms |
12208 KB |
Output is correct |
18 |
Correct |
121 ms |
19696 KB |
Output is correct |
19 |
Correct |
185 ms |
24080 KB |
Output is correct |
20 |
Correct |
16 ms |
12416 KB |
Output is correct |
21 |
Correct |
14 ms |
12160 KB |
Output is correct |
22 |
Correct |
155 ms |
19384 KB |
Output is correct |
23 |
Correct |
18 ms |
12544 KB |
Output is correct |
24 |
Correct |
179 ms |
18556 KB |
Output is correct |
25 |
Correct |
179 ms |
22520 KB |
Output is correct |
26 |
Correct |
17 ms |
12416 KB |
Output is correct |
27 |
Correct |
15 ms |
12416 KB |
Output is correct |
28 |
Correct |
15 ms |
12416 KB |
Output is correct |
29 |
Correct |
19 ms |
12416 KB |
Output is correct |
30 |
Correct |
16 ms |
12416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12160 KB |
Output is correct |
2 |
Correct |
13 ms |
12160 KB |
Output is correct |
3 |
Correct |
14 ms |
12160 KB |
Output is correct |
4 |
Correct |
14 ms |
12160 KB |
Output is correct |
5 |
Correct |
12 ms |
12160 KB |
Output is correct |
6 |
Correct |
16 ms |
12160 KB |
Output is correct |
7 |
Correct |
12 ms |
12160 KB |
Output is correct |
8 |
Correct |
13 ms |
12160 KB |
Output is correct |
9 |
Correct |
15 ms |
12092 KB |
Output is correct |
10 |
Correct |
15 ms |
12160 KB |
Output is correct |
11 |
Correct |
11 ms |
12160 KB |
Output is correct |
12 |
Correct |
15 ms |
12160 KB |
Output is correct |
13 |
Correct |
14 ms |
12160 KB |
Output is correct |
14 |
Correct |
14 ms |
12160 KB |
Output is correct |
15 |
Correct |
13 ms |
12160 KB |
Output is correct |
16 |
Correct |
13 ms |
12160 KB |
Output is correct |
17 |
Correct |
14 ms |
12160 KB |
Output is correct |
18 |
Correct |
13 ms |
12160 KB |
Output is correct |
19 |
Correct |
18 ms |
12160 KB |
Output is correct |
20 |
Correct |
14 ms |
12160 KB |
Output is correct |
21 |
Correct |
15 ms |
12288 KB |
Output is correct |
22 |
Correct |
14 ms |
12160 KB |
Output is correct |
23 |
Correct |
13 ms |
12160 KB |
Output is correct |
24 |
Correct |
16 ms |
12288 KB |
Output is correct |
25 |
Correct |
13 ms |
12160 KB |
Output is correct |
26 |
Correct |
15 ms |
12416 KB |
Output is correct |
27 |
Correct |
16 ms |
12288 KB |
Output is correct |
28 |
Correct |
18 ms |
12544 KB |
Output is correct |
29 |
Correct |
15 ms |
12416 KB |
Output is correct |
30 |
Correct |
16 ms |
12416 KB |
Output is correct |
31 |
Correct |
16 ms |
12080 KB |
Output is correct |
32 |
Correct |
20 ms |
12544 KB |
Output is correct |
33 |
Correct |
15 ms |
12160 KB |
Output is correct |
34 |
Correct |
19 ms |
12416 KB |
Output is correct |
35 |
Correct |
17 ms |
12416 KB |
Output is correct |
36 |
Correct |
17 ms |
12416 KB |
Output is correct |
37 |
Correct |
20 ms |
12288 KB |
Output is correct |
38 |
Correct |
13 ms |
12160 KB |
Output is correct |
39 |
Correct |
19 ms |
12344 KB |
Output is correct |
40 |
Correct |
18 ms |
12288 KB |
Output is correct |
41 |
Correct |
21 ms |
12416 KB |
Output is correct |
42 |
Correct |
16 ms |
12416 KB |
Output is correct |
43 |
Correct |
17 ms |
12544 KB |
Output is correct |
44 |
Correct |
15 ms |
12160 KB |
Output is correct |
45 |
Correct |
16 ms |
12416 KB |
Output is correct |
46 |
Correct |
15 ms |
12416 KB |
Output is correct |
47 |
Correct |
13 ms |
12160 KB |
Output is correct |
48 |
Correct |
19 ms |
12416 KB |
Output is correct |
49 |
Correct |
16 ms |
12416 KB |
Output is correct |
50 |
Correct |
18 ms |
12544 KB |
Output is correct |
51 |
Correct |
16 ms |
12408 KB |
Output is correct |
52 |
Correct |
16 ms |
12280 KB |
Output is correct |
53 |
Correct |
16 ms |
12416 KB |
Output is correct |
54 |
Correct |
17 ms |
12288 KB |
Output is correct |
55 |
Correct |
13 ms |
12160 KB |
Output is correct |
56 |
Correct |
120 ms |
18596 KB |
Output is correct |
57 |
Correct |
200 ms |
18204 KB |
Output is correct |
58 |
Correct |
18 ms |
12416 KB |
Output is correct |
59 |
Correct |
16 ms |
12160 KB |
Output is correct |
60 |
Correct |
14 ms |
12160 KB |
Output is correct |
61 |
Correct |
156 ms |
18424 KB |
Output is correct |
62 |
Correct |
18 ms |
12544 KB |
Output is correct |
63 |
Correct |
190 ms |
26872 KB |
Output is correct |
64 |
Correct |
157 ms |
18296 KB |
Output is correct |
65 |
Correct |
17 ms |
12416 KB |
Output is correct |
66 |
Correct |
181 ms |
19292 KB |
Output is correct |
67 |
Correct |
18 ms |
12288 KB |
Output is correct |
68 |
Correct |
17 ms |
12328 KB |
Output is correct |
69 |
Correct |
132 ms |
18744 KB |
Output is correct |
70 |
Correct |
17 ms |
12672 KB |
Output is correct |
71 |
Correct |
189 ms |
18268 KB |
Output is correct |
72 |
Correct |
187 ms |
21152 KB |
Output is correct |
73 |
Correct |
14 ms |
12160 KB |
Output is correct |
74 |
Correct |
179 ms |
27256 KB |
Output is correct |
75 |
Correct |
159 ms |
22776 KB |
Output is correct |
76 |
Correct |
18 ms |
12408 KB |
Output is correct |
77 |
Correct |
17 ms |
12416 KB |
Output is correct |
78 |
Correct |
104 ms |
18612 KB |
Output is correct |
79 |
Correct |
113 ms |
19736 KB |
Output is correct |
80 |
Correct |
18 ms |
12416 KB |
Output is correct |
81 |
Correct |
17 ms |
12416 KB |
Output is correct |
82 |
Correct |
16 ms |
12160 KB |
Output is correct |
83 |
Correct |
14 ms |
12160 KB |
Output is correct |
84 |
Correct |
16 ms |
12288 KB |
Output is correct |
85 |
Correct |
205 ms |
19116 KB |
Output is correct |
86 |
Correct |
19 ms |
12416 KB |
Output is correct |
87 |
Correct |
191 ms |
18652 KB |
Output is correct |
88 |
Correct |
13 ms |
12160 KB |
Output is correct |
89 |
Correct |
168 ms |
18764 KB |
Output is correct |
90 |
Correct |
190 ms |
18936 KB |
Output is correct |
91 |
Correct |
167 ms |
19448 KB |
Output is correct |
92 |
Correct |
114 ms |
18544 KB |
Output is correct |
93 |
Correct |
16 ms |
12416 KB |
Output is correct |
94 |
Correct |
17 ms |
12208 KB |
Output is correct |
95 |
Correct |
121 ms |
19696 KB |
Output is correct |
96 |
Correct |
185 ms |
24080 KB |
Output is correct |
97 |
Correct |
16 ms |
12416 KB |
Output is correct |
98 |
Correct |
14 ms |
12160 KB |
Output is correct |
99 |
Correct |
155 ms |
19384 KB |
Output is correct |
100 |
Correct |
18 ms |
12544 KB |
Output is correct |
101 |
Correct |
179 ms |
18556 KB |
Output is correct |
102 |
Correct |
179 ms |
22520 KB |
Output is correct |
103 |
Correct |
17 ms |
12416 KB |
Output is correct |
104 |
Correct |
15 ms |
12416 KB |
Output is correct |
105 |
Correct |
15 ms |
12416 KB |
Output is correct |
106 |
Correct |
19 ms |
12416 KB |
Output is correct |
107 |
Correct |
16 ms |
12416 KB |
Output is correct |
108 |
Correct |
979 ms |
48256 KB |
Output is correct |
109 |
Correct |
1283 ms |
71292 KB |
Output is correct |
110 |
Correct |
1317 ms |
84980 KB |
Output is correct |
111 |
Correct |
1596 ms |
84884 KB |
Output is correct |
112 |
Correct |
1468 ms |
73192 KB |
Output is correct |
113 |
Correct |
894 ms |
58272 KB |
Output is correct |
114 |
Correct |
1185 ms |
49860 KB |
Output is correct |
115 |
Correct |
1184 ms |
49676 KB |
Output is correct |
116 |
Correct |
1166 ms |
53744 KB |
Output is correct |
117 |
Correct |
1504 ms |
57212 KB |
Output is correct |
118 |
Correct |
1258 ms |
51192 KB |
Output is correct |
119 |
Correct |
1505 ms |
57296 KB |
Output is correct |
120 |
Correct |
1736 ms |
74052 KB |
Output is correct |
121 |
Correct |
1329 ms |
57592 KB |
Output is correct |
122 |
Correct |
1297 ms |
55836 KB |
Output is correct |
123 |
Correct |
819 ms |
58696 KB |
Output is correct |
124 |
Correct |
1453 ms |
56612 KB |
Output is correct |