# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
154956 |
2019-09-25T16:55:53 Z |
karma |
Mergers (JOI19_mergers) |
C++14 |
|
3000 ms |
76052 KB |
#include<bits/stdc++.h>
#define Task "test"
#define ll long long
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
const int N = int(5e5) + 5;
typedef pair<int, int> pii;
vector<int> a[N], g;
int sz[N], n, k, u, v, nstate, full, res = 0;
int cnt[N], s[N], orgcnt[N], cc;
int in[N], out[N], Time;
bool big[N], choose[N];
vector<pii> vertex;
void DFS(int u, int par) {
sz[u] = 1; in[u] = ++Time;
for(int i = 0; i < int(a[u].size()); ++i) {
if(a[u][i] == par) swap(a[u][i], a[u].back());
if(a[u][i] != par) DFS(a[u][i], u), sz[u] += a[u][i];
}
if(u != 1) a[u].pop_back();
out[u] = Time;
}
void Add(int u, int val) {
if(cnt[s[u]] == 0) ++nstate;
if(cnt[s[u]] == orgcnt[s[u]]) --full;
cnt[s[u]] += val;
if(cnt[s[u]] == orgcnt[s[u]]) ++full;
if(cnt[s[u]] == 0) --nstate;
for(int v: a[u])
if(!big[v]) Add(v, val);
}
void DSU(int u, bool keep) {
int Big = 0;
for(int v: a[u])
if(sz[v] > sz[Big]) Big = v;
for(int v: a[u])
if(v != Big) DSU(v, 0);
if(Big) DSU(Big, 1), big[Big] = 1;
Add(u, 1);
if(full == nstate) choose[u] = 1, ++cc;
if(Big) big[Big] = 0;
if(!keep) Add(u, -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
cin >> n >> k;
for(int i = 1; i < n; ++i) {
cin >> u >> v;
a[u].pb(v), a[v].pb(u);
}
for(int i = 1; i <= n; ++i) cin >> s[i], ++orgcnt[s[i]];
DFS(1, 1); DSU(1, 1);
if(cc == 1) return cout << 0, 0;
for(int i = 1; i <= n; ++i) {
a[i].clear();
if(choose[i]) vertex.pb(mp(in[i], i));
}
sort(vertex.begin(), vertex.end());
for(pii p: vertex) {
while(!g.empty() && out[g.back()] < p.fi) g.pop_back();
if(g.size()) a[g.back()].pb(p.se);
g.pb(p.se);
}
for(int i = 1; i <= n; ++i) if(a[i].empty() && choose[i]) ++res;
if(a[1].size() == 1) ++res;
cout << (res + 1) / 2;
}
Compilation message
mergers.cpp: In function 'int main()':
mergers.cpp:58:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen(Task".out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12156 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12156 KB |
Output is correct |
14 |
Correct |
13 ms |
12124 KB |
Output is correct |
15 |
Correct |
13 ms |
12156 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12152 KB |
Output is correct |
20 |
Correct |
13 ms |
12152 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
14 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12156 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12156 KB |
Output is correct |
14 |
Correct |
13 ms |
12124 KB |
Output is correct |
15 |
Correct |
13 ms |
12156 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12152 KB |
Output is correct |
20 |
Correct |
13 ms |
12152 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
14 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
13 ms |
12280 KB |
Output is correct |
26 |
Correct |
16 ms |
12408 KB |
Output is correct |
27 |
Correct |
15 ms |
12280 KB |
Output is correct |
28 |
Correct |
15 ms |
12408 KB |
Output is correct |
29 |
Correct |
16 ms |
12408 KB |
Output is correct |
30 |
Correct |
15 ms |
12408 KB |
Output is correct |
31 |
Correct |
13 ms |
12152 KB |
Output is correct |
32 |
Correct |
15 ms |
12536 KB |
Output is correct |
33 |
Correct |
13 ms |
12152 KB |
Output is correct |
34 |
Correct |
14 ms |
12408 KB |
Output is correct |
35 |
Correct |
16 ms |
12408 KB |
Output is correct |
36 |
Correct |
16 ms |
12284 KB |
Output is correct |
37 |
Correct |
17 ms |
12280 KB |
Output is correct |
38 |
Correct |
16 ms |
12152 KB |
Output is correct |
39 |
Correct |
29 ms |
12408 KB |
Output is correct |
40 |
Correct |
16 ms |
12280 KB |
Output is correct |
41 |
Correct |
15 ms |
12280 KB |
Output is correct |
42 |
Correct |
18 ms |
12408 KB |
Output is correct |
43 |
Correct |
15 ms |
12536 KB |
Output is correct |
44 |
Correct |
13 ms |
12152 KB |
Output is correct |
45 |
Correct |
16 ms |
12408 KB |
Output is correct |
46 |
Correct |
17 ms |
12408 KB |
Output is correct |
47 |
Correct |
13 ms |
12152 KB |
Output is correct |
48 |
Correct |
16 ms |
12408 KB |
Output is correct |
49 |
Correct |
15 ms |
12408 KB |
Output is correct |
50 |
Correct |
16 ms |
12536 KB |
Output is correct |
51 |
Correct |
16 ms |
12408 KB |
Output is correct |
52 |
Correct |
15 ms |
12408 KB |
Output is correct |
53 |
Correct |
15 ms |
12408 KB |
Output is correct |
54 |
Correct |
15 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12156 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12156 KB |
Output is correct |
14 |
Correct |
13 ms |
12124 KB |
Output is correct |
15 |
Correct |
13 ms |
12156 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12152 KB |
Output is correct |
20 |
Correct |
13 ms |
12152 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
14 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
13 ms |
12152 KB |
Output is correct |
26 |
Correct |
76 ms |
18740 KB |
Output is correct |
27 |
Correct |
181 ms |
18424 KB |
Output is correct |
28 |
Correct |
16 ms |
12280 KB |
Output is correct |
29 |
Correct |
13 ms |
12280 KB |
Output is correct |
30 |
Correct |
13 ms |
12152 KB |
Output is correct |
31 |
Correct |
415 ms |
18824 KB |
Output is correct |
32 |
Correct |
17 ms |
12284 KB |
Output is correct |
33 |
Correct |
179 ms |
24320 KB |
Output is correct |
34 |
Correct |
261 ms |
18504 KB |
Output is correct |
35 |
Correct |
17 ms |
12280 KB |
Output is correct |
36 |
Correct |
252 ms |
19320 KB |
Output is correct |
37 |
Correct |
15 ms |
12280 KB |
Output is correct |
38 |
Correct |
16 ms |
12280 KB |
Output is correct |
39 |
Correct |
88 ms |
18544 KB |
Output is correct |
40 |
Correct |
15 ms |
12536 KB |
Output is correct |
41 |
Correct |
200 ms |
18488 KB |
Output is correct |
42 |
Correct |
159 ms |
20356 KB |
Output is correct |
43 |
Correct |
14 ms |
12152 KB |
Output is correct |
44 |
Correct |
164 ms |
24312 KB |
Output is correct |
45 |
Correct |
119 ms |
21496 KB |
Output is correct |
46 |
Correct |
15 ms |
12408 KB |
Output is correct |
47 |
Correct |
16 ms |
12280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
78 ms |
18668 KB |
Output is correct |
2 |
Correct |
98 ms |
20972 KB |
Output is correct |
3 |
Correct |
16 ms |
12408 KB |
Output is correct |
4 |
Correct |
15 ms |
12280 KB |
Output is correct |
5 |
Correct |
13 ms |
12152 KB |
Output is correct |
6 |
Correct |
14 ms |
12152 KB |
Output is correct |
7 |
Correct |
15 ms |
12280 KB |
Output is correct |
8 |
Correct |
624 ms |
19416 KB |
Output is correct |
9 |
Correct |
17 ms |
12280 KB |
Output is correct |
10 |
Correct |
476 ms |
18808 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
457 ms |
18680 KB |
Output is correct |
13 |
Correct |
386 ms |
19576 KB |
Output is correct |
14 |
Correct |
285 ms |
20844 KB |
Output is correct |
15 |
Correct |
92 ms |
18544 KB |
Output is correct |
16 |
Correct |
15 ms |
12280 KB |
Output is correct |
17 |
Correct |
14 ms |
12100 KB |
Output is correct |
18 |
Correct |
107 ms |
20972 KB |
Output is correct |
19 |
Correct |
167 ms |
24092 KB |
Output is correct |
20 |
Correct |
16 ms |
12280 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
90 ms |
19692 KB |
Output is correct |
23 |
Correct |
18 ms |
12408 KB |
Output is correct |
24 |
Correct |
390 ms |
19036 KB |
Output is correct |
25 |
Correct |
161 ms |
23024 KB |
Output is correct |
26 |
Correct |
15 ms |
12408 KB |
Output is correct |
27 |
Correct |
18 ms |
12664 KB |
Output is correct |
28 |
Correct |
18 ms |
12280 KB |
Output is correct |
29 |
Correct |
15 ms |
12380 KB |
Output is correct |
30 |
Correct |
15 ms |
12408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
12152 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12152 KB |
Output is correct |
4 |
Correct |
13 ms |
12152 KB |
Output is correct |
5 |
Correct |
13 ms |
12156 KB |
Output is correct |
6 |
Correct |
13 ms |
12152 KB |
Output is correct |
7 |
Correct |
13 ms |
12152 KB |
Output is correct |
8 |
Correct |
14 ms |
12152 KB |
Output is correct |
9 |
Correct |
13 ms |
12152 KB |
Output is correct |
10 |
Correct |
13 ms |
12152 KB |
Output is correct |
11 |
Correct |
13 ms |
12152 KB |
Output is correct |
12 |
Correct |
13 ms |
12152 KB |
Output is correct |
13 |
Correct |
13 ms |
12156 KB |
Output is correct |
14 |
Correct |
13 ms |
12124 KB |
Output is correct |
15 |
Correct |
13 ms |
12156 KB |
Output is correct |
16 |
Correct |
13 ms |
12152 KB |
Output is correct |
17 |
Correct |
13 ms |
12152 KB |
Output is correct |
18 |
Correct |
13 ms |
12152 KB |
Output is correct |
19 |
Correct |
13 ms |
12152 KB |
Output is correct |
20 |
Correct |
13 ms |
12152 KB |
Output is correct |
21 |
Correct |
13 ms |
12152 KB |
Output is correct |
22 |
Correct |
13 ms |
12152 KB |
Output is correct |
23 |
Correct |
14 ms |
12152 KB |
Output is correct |
24 |
Correct |
13 ms |
12152 KB |
Output is correct |
25 |
Correct |
13 ms |
12280 KB |
Output is correct |
26 |
Correct |
16 ms |
12408 KB |
Output is correct |
27 |
Correct |
15 ms |
12280 KB |
Output is correct |
28 |
Correct |
15 ms |
12408 KB |
Output is correct |
29 |
Correct |
16 ms |
12408 KB |
Output is correct |
30 |
Correct |
15 ms |
12408 KB |
Output is correct |
31 |
Correct |
13 ms |
12152 KB |
Output is correct |
32 |
Correct |
15 ms |
12536 KB |
Output is correct |
33 |
Correct |
13 ms |
12152 KB |
Output is correct |
34 |
Correct |
14 ms |
12408 KB |
Output is correct |
35 |
Correct |
16 ms |
12408 KB |
Output is correct |
36 |
Correct |
16 ms |
12284 KB |
Output is correct |
37 |
Correct |
17 ms |
12280 KB |
Output is correct |
38 |
Correct |
16 ms |
12152 KB |
Output is correct |
39 |
Correct |
29 ms |
12408 KB |
Output is correct |
40 |
Correct |
16 ms |
12280 KB |
Output is correct |
41 |
Correct |
15 ms |
12280 KB |
Output is correct |
42 |
Correct |
18 ms |
12408 KB |
Output is correct |
43 |
Correct |
15 ms |
12536 KB |
Output is correct |
44 |
Correct |
13 ms |
12152 KB |
Output is correct |
45 |
Correct |
16 ms |
12408 KB |
Output is correct |
46 |
Correct |
17 ms |
12408 KB |
Output is correct |
47 |
Correct |
13 ms |
12152 KB |
Output is correct |
48 |
Correct |
16 ms |
12408 KB |
Output is correct |
49 |
Correct |
15 ms |
12408 KB |
Output is correct |
50 |
Correct |
16 ms |
12536 KB |
Output is correct |
51 |
Correct |
16 ms |
12408 KB |
Output is correct |
52 |
Correct |
15 ms |
12408 KB |
Output is correct |
53 |
Correct |
15 ms |
12408 KB |
Output is correct |
54 |
Correct |
15 ms |
12280 KB |
Output is correct |
55 |
Correct |
13 ms |
12152 KB |
Output is correct |
56 |
Correct |
76 ms |
18740 KB |
Output is correct |
57 |
Correct |
181 ms |
18424 KB |
Output is correct |
58 |
Correct |
16 ms |
12280 KB |
Output is correct |
59 |
Correct |
13 ms |
12280 KB |
Output is correct |
60 |
Correct |
13 ms |
12152 KB |
Output is correct |
61 |
Correct |
415 ms |
18824 KB |
Output is correct |
62 |
Correct |
17 ms |
12284 KB |
Output is correct |
63 |
Correct |
179 ms |
24320 KB |
Output is correct |
64 |
Correct |
261 ms |
18504 KB |
Output is correct |
65 |
Correct |
17 ms |
12280 KB |
Output is correct |
66 |
Correct |
252 ms |
19320 KB |
Output is correct |
67 |
Correct |
15 ms |
12280 KB |
Output is correct |
68 |
Correct |
16 ms |
12280 KB |
Output is correct |
69 |
Correct |
88 ms |
18544 KB |
Output is correct |
70 |
Correct |
15 ms |
12536 KB |
Output is correct |
71 |
Correct |
200 ms |
18488 KB |
Output is correct |
72 |
Correct |
159 ms |
20356 KB |
Output is correct |
73 |
Correct |
14 ms |
12152 KB |
Output is correct |
74 |
Correct |
164 ms |
24312 KB |
Output is correct |
75 |
Correct |
119 ms |
21496 KB |
Output is correct |
76 |
Correct |
15 ms |
12408 KB |
Output is correct |
77 |
Correct |
16 ms |
12280 KB |
Output is correct |
78 |
Correct |
78 ms |
18668 KB |
Output is correct |
79 |
Correct |
98 ms |
20972 KB |
Output is correct |
80 |
Correct |
16 ms |
12408 KB |
Output is correct |
81 |
Correct |
15 ms |
12280 KB |
Output is correct |
82 |
Correct |
13 ms |
12152 KB |
Output is correct |
83 |
Correct |
14 ms |
12152 KB |
Output is correct |
84 |
Correct |
15 ms |
12280 KB |
Output is correct |
85 |
Correct |
624 ms |
19416 KB |
Output is correct |
86 |
Correct |
17 ms |
12280 KB |
Output is correct |
87 |
Correct |
476 ms |
18808 KB |
Output is correct |
88 |
Correct |
13 ms |
12152 KB |
Output is correct |
89 |
Correct |
457 ms |
18680 KB |
Output is correct |
90 |
Correct |
386 ms |
19576 KB |
Output is correct |
91 |
Correct |
285 ms |
20844 KB |
Output is correct |
92 |
Correct |
92 ms |
18544 KB |
Output is correct |
93 |
Correct |
15 ms |
12280 KB |
Output is correct |
94 |
Correct |
14 ms |
12100 KB |
Output is correct |
95 |
Correct |
107 ms |
20972 KB |
Output is correct |
96 |
Correct |
167 ms |
24092 KB |
Output is correct |
97 |
Correct |
16 ms |
12280 KB |
Output is correct |
98 |
Correct |
13 ms |
12152 KB |
Output is correct |
99 |
Correct |
90 ms |
19692 KB |
Output is correct |
100 |
Correct |
18 ms |
12408 KB |
Output is correct |
101 |
Correct |
390 ms |
19036 KB |
Output is correct |
102 |
Correct |
161 ms |
23024 KB |
Output is correct |
103 |
Correct |
15 ms |
12408 KB |
Output is correct |
104 |
Correct |
18 ms |
12664 KB |
Output is correct |
105 |
Correct |
18 ms |
12280 KB |
Output is correct |
106 |
Correct |
15 ms |
12380 KB |
Output is correct |
107 |
Correct |
15 ms |
12408 KB |
Output is correct |
108 |
Correct |
802 ms |
50392 KB |
Output is correct |
109 |
Correct |
1540 ms |
58616 KB |
Output is correct |
110 |
Correct |
1050 ms |
67516 KB |
Output is correct |
111 |
Correct |
1362 ms |
76052 KB |
Output is correct |
112 |
Correct |
1230 ms |
65768 KB |
Output is correct |
113 |
Correct |
775 ms |
56020 KB |
Output is correct |
114 |
Correct |
2529 ms |
44280 KB |
Output is correct |
115 |
Correct |
2048 ms |
44268 KB |
Output is correct |
116 |
Execution timed out |
3053 ms |
47472 KB |
Time limit exceeded |
117 |
Halted |
0 ms |
0 KB |
- |