# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
105454 |
2019-04-12T12:02:56 Z |
reality |
Mergers (JOI19_mergers) |
C++17 |
|
1890 ms |
201984 KB |
#include "bits/stdc++.h"
using namespace std;
#define fi first
#define se second
#define ll long long
#define dbg(v) cerr<<#v<<" = "<<v<<'\n'
#define vi vector<int>
#define vl vector <ll>
#define pii pair<int,int>
#define vii vector < pii >
#define mp make_pair
#define db long double
#define pb push_back
#define all(s) s.begin(),s.end()
template < class P , class Q > ostream& operator<<(ostream& stream, pair < P , Q > v){ stream << "(" << v.fi << ',' << v.se << ")"; return stream;}
template < class T > ostream& operator<<(ostream& stream, const vector<T> v){ stream << "[ "; for (int i=0; i<(int)v.size(); i++) stream << v[i] << " "; stream << "]"; return stream;}
template < class T > T smin(T &a,T b) {if (a > b) a = b;return a;}
template < class T > T smax(T &a,T b) {if (a < b) a = b;return a;}
#define pp pair < pair < int , vi > , map < int , int > >
const int N = 1e6 + 5;
int cnt[N];
void comb(pp &a,pp & b) {
if (a.se.size() < b.se.size())
swap(a,b);
if (a.fi.se.size() < b.fi.se.size())
swap(a.fi.se,b.fi.se);
for (auto it : b.fi.se)
a.fi.se.pb(it);
a.fi.fi += b.fi.fi;
for (auto it : b.se) {
a.se[it.fi] += it.se;
a.fi.fi += a.se[it.fi] == cnt[it.fi] && it.se < cnt[it.fi];
}
b.fi.fi = 0;
b.fi.se.clear();
b.se.clear();
}
vi g[N];
int v[N];
int flag[N];
int deg[N];
pp M[N];
int R = 0;
void dfs(int node,int prev) {
M[node].fi.fi = cnt[v[node]] == 1;
M[node].fi.se.pb(node);
M[node].se[v[node]] = 1;
for (auto it : g[node]) {
if (it == prev)
continue;
dfs(it,node);
comb(M[node],M[it]);
}
if (M[node].fi.fi == M[node].se.size()) {
++R;
for (auto it : M[node].fi.se)
flag[it] = R;
M[node].fi.fi = 0;
M[node].fi.se.clear();
M[node].se.clear();
}
}
int main(void) {
int n,k;
cin.tie(0);
ios_base :: sync_with_stdio(0);
cin>>n>>k;
vii e;
for (int i = 1;i < n;++i) {
int a,b;
cin>>a>>b;
e.pb(mp(a,b));
g[a].pb(b);
g[b].pb(a);
}
for (int i = 1;i <= n;++i) {
cin>>v[i];
cnt[v[i]] += 1;
}
dfs(1,0);
if (R == 1) {
puts("0");
return 0;
}
if (R == 2) {
puts("1");
return 0;
}
int ans = 0;
for (auto it : e)
if (flag[it.fi] != flag[it.se])
++deg[flag[it.fi]],++deg[flag[it.se]];
for (int i = 1;i <= R;++i)
ans += deg[i] == 1;
cout << ((ans + 1) / 2) << '\n';
return 0;
}
Compilation message
mergers.cpp: In function 'void dfs(int, int)':
mergers.cpp:54:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (M[node].fi.fi == M[node].se.size()) {
~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
102136 KB |
Output is correct |
2 |
Correct |
87 ms |
102136 KB |
Output is correct |
3 |
Correct |
86 ms |
102136 KB |
Output is correct |
4 |
Correct |
94 ms |
102152 KB |
Output is correct |
5 |
Correct |
91 ms |
102136 KB |
Output is correct |
6 |
Correct |
112 ms |
102264 KB |
Output is correct |
7 |
Correct |
98 ms |
102232 KB |
Output is correct |
8 |
Correct |
103 ms |
102172 KB |
Output is correct |
9 |
Correct |
107 ms |
102168 KB |
Output is correct |
10 |
Correct |
106 ms |
102264 KB |
Output is correct |
11 |
Correct |
92 ms |
102136 KB |
Output is correct |
12 |
Correct |
115 ms |
102136 KB |
Output is correct |
13 |
Correct |
103 ms |
102136 KB |
Output is correct |
14 |
Correct |
88 ms |
102164 KB |
Output is correct |
15 |
Correct |
107 ms |
102136 KB |
Output is correct |
16 |
Correct |
91 ms |
102136 KB |
Output is correct |
17 |
Correct |
90 ms |
102088 KB |
Output is correct |
18 |
Correct |
100 ms |
102136 KB |
Output is correct |
19 |
Correct |
92 ms |
102108 KB |
Output is correct |
20 |
Correct |
108 ms |
102200 KB |
Output is correct |
21 |
Correct |
90 ms |
102136 KB |
Output is correct |
22 |
Correct |
92 ms |
102264 KB |
Output is correct |
23 |
Correct |
102 ms |
102136 KB |
Output is correct |
24 |
Correct |
95 ms |
102264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
102136 KB |
Output is correct |
2 |
Correct |
87 ms |
102136 KB |
Output is correct |
3 |
Correct |
86 ms |
102136 KB |
Output is correct |
4 |
Correct |
94 ms |
102152 KB |
Output is correct |
5 |
Correct |
91 ms |
102136 KB |
Output is correct |
6 |
Correct |
112 ms |
102264 KB |
Output is correct |
7 |
Correct |
98 ms |
102232 KB |
Output is correct |
8 |
Correct |
103 ms |
102172 KB |
Output is correct |
9 |
Correct |
107 ms |
102168 KB |
Output is correct |
10 |
Correct |
106 ms |
102264 KB |
Output is correct |
11 |
Correct |
92 ms |
102136 KB |
Output is correct |
12 |
Correct |
115 ms |
102136 KB |
Output is correct |
13 |
Correct |
103 ms |
102136 KB |
Output is correct |
14 |
Correct |
88 ms |
102164 KB |
Output is correct |
15 |
Correct |
107 ms |
102136 KB |
Output is correct |
16 |
Correct |
91 ms |
102136 KB |
Output is correct |
17 |
Correct |
90 ms |
102088 KB |
Output is correct |
18 |
Correct |
100 ms |
102136 KB |
Output is correct |
19 |
Correct |
92 ms |
102108 KB |
Output is correct |
20 |
Correct |
108 ms |
102200 KB |
Output is correct |
21 |
Correct |
90 ms |
102136 KB |
Output is correct |
22 |
Correct |
92 ms |
102264 KB |
Output is correct |
23 |
Correct |
102 ms |
102136 KB |
Output is correct |
24 |
Correct |
95 ms |
102264 KB |
Output is correct |
25 |
Correct |
91 ms |
102136 KB |
Output is correct |
26 |
Correct |
95 ms |
102392 KB |
Output is correct |
27 |
Correct |
101 ms |
102352 KB |
Output is correct |
28 |
Correct |
109 ms |
102520 KB |
Output is correct |
29 |
Correct |
96 ms |
102492 KB |
Output is correct |
30 |
Correct |
94 ms |
102628 KB |
Output is correct |
31 |
Correct |
90 ms |
102136 KB |
Output is correct |
32 |
Correct |
114 ms |
102900 KB |
Output is correct |
33 |
Correct |
109 ms |
102140 KB |
Output is correct |
34 |
Correct |
108 ms |
102520 KB |
Output is correct |
35 |
Correct |
116 ms |
102392 KB |
Output is correct |
36 |
Correct |
88 ms |
102520 KB |
Output is correct |
37 |
Correct |
107 ms |
102520 KB |
Output is correct |
38 |
Correct |
87 ms |
102204 KB |
Output is correct |
39 |
Correct |
117 ms |
102656 KB |
Output is correct |
40 |
Correct |
98 ms |
102524 KB |
Output is correct |
41 |
Correct |
92 ms |
102492 KB |
Output is correct |
42 |
Correct |
91 ms |
102648 KB |
Output is correct |
43 |
Correct |
170 ms |
102776 KB |
Output is correct |
44 |
Correct |
95 ms |
102108 KB |
Output is correct |
45 |
Correct |
101 ms |
102648 KB |
Output is correct |
46 |
Correct |
107 ms |
102392 KB |
Output is correct |
47 |
Correct |
108 ms |
102136 KB |
Output is correct |
48 |
Correct |
104 ms |
102396 KB |
Output is correct |
49 |
Correct |
96 ms |
102440 KB |
Output is correct |
50 |
Correct |
104 ms |
102520 KB |
Output is correct |
51 |
Correct |
109 ms |
102472 KB |
Output is correct |
52 |
Correct |
89 ms |
102492 KB |
Output is correct |
53 |
Correct |
98 ms |
102504 KB |
Output is correct |
54 |
Correct |
102 ms |
102520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
102136 KB |
Output is correct |
2 |
Correct |
87 ms |
102136 KB |
Output is correct |
3 |
Correct |
86 ms |
102136 KB |
Output is correct |
4 |
Correct |
94 ms |
102152 KB |
Output is correct |
5 |
Correct |
91 ms |
102136 KB |
Output is correct |
6 |
Correct |
112 ms |
102264 KB |
Output is correct |
7 |
Correct |
98 ms |
102232 KB |
Output is correct |
8 |
Correct |
103 ms |
102172 KB |
Output is correct |
9 |
Correct |
107 ms |
102168 KB |
Output is correct |
10 |
Correct |
106 ms |
102264 KB |
Output is correct |
11 |
Correct |
92 ms |
102136 KB |
Output is correct |
12 |
Correct |
115 ms |
102136 KB |
Output is correct |
13 |
Correct |
103 ms |
102136 KB |
Output is correct |
14 |
Correct |
88 ms |
102164 KB |
Output is correct |
15 |
Correct |
107 ms |
102136 KB |
Output is correct |
16 |
Correct |
91 ms |
102136 KB |
Output is correct |
17 |
Correct |
90 ms |
102088 KB |
Output is correct |
18 |
Correct |
100 ms |
102136 KB |
Output is correct |
19 |
Correct |
92 ms |
102108 KB |
Output is correct |
20 |
Correct |
108 ms |
102200 KB |
Output is correct |
21 |
Correct |
90 ms |
102136 KB |
Output is correct |
22 |
Correct |
92 ms |
102264 KB |
Output is correct |
23 |
Correct |
102 ms |
102136 KB |
Output is correct |
24 |
Correct |
95 ms |
102264 KB |
Output is correct |
25 |
Correct |
113 ms |
102108 KB |
Output is correct |
26 |
Correct |
212 ms |
112204 KB |
Output is correct |
27 |
Correct |
277 ms |
113940 KB |
Output is correct |
28 |
Correct |
107 ms |
102412 KB |
Output is correct |
29 |
Correct |
91 ms |
102108 KB |
Output is correct |
30 |
Correct |
87 ms |
102084 KB |
Output is correct |
31 |
Correct |
232 ms |
113004 KB |
Output is correct |
32 |
Correct |
98 ms |
102392 KB |
Output is correct |
33 |
Correct |
282 ms |
124136 KB |
Output is correct |
34 |
Correct |
249 ms |
113900 KB |
Output is correct |
35 |
Correct |
90 ms |
102392 KB |
Output is correct |
36 |
Correct |
235 ms |
113692 KB |
Output is correct |
37 |
Correct |
93 ms |
102520 KB |
Output is correct |
38 |
Correct |
98 ms |
102392 KB |
Output is correct |
39 |
Correct |
207 ms |
112364 KB |
Output is correct |
40 |
Correct |
99 ms |
102776 KB |
Output is correct |
41 |
Correct |
266 ms |
113680 KB |
Output is correct |
42 |
Correct |
283 ms |
116712 KB |
Output is correct |
43 |
Correct |
88 ms |
102264 KB |
Output is correct |
44 |
Correct |
312 ms |
124468 KB |
Output is correct |
45 |
Correct |
317 ms |
118748 KB |
Output is correct |
46 |
Correct |
111 ms |
102520 KB |
Output is correct |
47 |
Correct |
115 ms |
102456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
215 ms |
110832 KB |
Output is correct |
2 |
Correct |
205 ms |
111212 KB |
Output is correct |
3 |
Correct |
101 ms |
102540 KB |
Output is correct |
4 |
Correct |
103 ms |
102392 KB |
Output is correct |
5 |
Correct |
103 ms |
102300 KB |
Output is correct |
6 |
Correct |
103 ms |
102308 KB |
Output is correct |
7 |
Correct |
97 ms |
102452 KB |
Output is correct |
8 |
Correct |
285 ms |
112876 KB |
Output is correct |
9 |
Correct |
92 ms |
102392 KB |
Output is correct |
10 |
Correct |
304 ms |
112492 KB |
Output is correct |
11 |
Correct |
98 ms |
102140 KB |
Output is correct |
12 |
Correct |
259 ms |
112208 KB |
Output is correct |
13 |
Correct |
272 ms |
112504 KB |
Output is correct |
14 |
Correct |
273 ms |
112616 KB |
Output is correct |
15 |
Correct |
244 ms |
112492 KB |
Output is correct |
16 |
Correct |
114 ms |
102520 KB |
Output is correct |
17 |
Correct |
99 ms |
102236 KB |
Output is correct |
18 |
Correct |
280 ms |
113328 KB |
Output is correct |
19 |
Correct |
288 ms |
119404 KB |
Output is correct |
20 |
Correct |
116 ms |
102392 KB |
Output is correct |
21 |
Correct |
116 ms |
102124 KB |
Output is correct |
22 |
Correct |
318 ms |
114156 KB |
Output is correct |
23 |
Correct |
108 ms |
102520 KB |
Output is correct |
24 |
Correct |
257 ms |
112144 KB |
Output is correct |
25 |
Correct |
322 ms |
116196 KB |
Output is correct |
26 |
Correct |
101 ms |
102396 KB |
Output is correct |
27 |
Correct |
98 ms |
102540 KB |
Output is correct |
28 |
Correct |
98 ms |
102464 KB |
Output is correct |
29 |
Correct |
116 ms |
102520 KB |
Output is correct |
30 |
Correct |
93 ms |
102520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
102136 KB |
Output is correct |
2 |
Correct |
87 ms |
102136 KB |
Output is correct |
3 |
Correct |
86 ms |
102136 KB |
Output is correct |
4 |
Correct |
94 ms |
102152 KB |
Output is correct |
5 |
Correct |
91 ms |
102136 KB |
Output is correct |
6 |
Correct |
112 ms |
102264 KB |
Output is correct |
7 |
Correct |
98 ms |
102232 KB |
Output is correct |
8 |
Correct |
103 ms |
102172 KB |
Output is correct |
9 |
Correct |
107 ms |
102168 KB |
Output is correct |
10 |
Correct |
106 ms |
102264 KB |
Output is correct |
11 |
Correct |
92 ms |
102136 KB |
Output is correct |
12 |
Correct |
115 ms |
102136 KB |
Output is correct |
13 |
Correct |
103 ms |
102136 KB |
Output is correct |
14 |
Correct |
88 ms |
102164 KB |
Output is correct |
15 |
Correct |
107 ms |
102136 KB |
Output is correct |
16 |
Correct |
91 ms |
102136 KB |
Output is correct |
17 |
Correct |
90 ms |
102088 KB |
Output is correct |
18 |
Correct |
100 ms |
102136 KB |
Output is correct |
19 |
Correct |
92 ms |
102108 KB |
Output is correct |
20 |
Correct |
108 ms |
102200 KB |
Output is correct |
21 |
Correct |
90 ms |
102136 KB |
Output is correct |
22 |
Correct |
92 ms |
102264 KB |
Output is correct |
23 |
Correct |
102 ms |
102136 KB |
Output is correct |
24 |
Correct |
95 ms |
102264 KB |
Output is correct |
25 |
Correct |
91 ms |
102136 KB |
Output is correct |
26 |
Correct |
95 ms |
102392 KB |
Output is correct |
27 |
Correct |
101 ms |
102352 KB |
Output is correct |
28 |
Correct |
109 ms |
102520 KB |
Output is correct |
29 |
Correct |
96 ms |
102492 KB |
Output is correct |
30 |
Correct |
94 ms |
102628 KB |
Output is correct |
31 |
Correct |
90 ms |
102136 KB |
Output is correct |
32 |
Correct |
114 ms |
102900 KB |
Output is correct |
33 |
Correct |
109 ms |
102140 KB |
Output is correct |
34 |
Correct |
108 ms |
102520 KB |
Output is correct |
35 |
Correct |
116 ms |
102392 KB |
Output is correct |
36 |
Correct |
88 ms |
102520 KB |
Output is correct |
37 |
Correct |
107 ms |
102520 KB |
Output is correct |
38 |
Correct |
87 ms |
102204 KB |
Output is correct |
39 |
Correct |
117 ms |
102656 KB |
Output is correct |
40 |
Correct |
98 ms |
102524 KB |
Output is correct |
41 |
Correct |
92 ms |
102492 KB |
Output is correct |
42 |
Correct |
91 ms |
102648 KB |
Output is correct |
43 |
Correct |
170 ms |
102776 KB |
Output is correct |
44 |
Correct |
95 ms |
102108 KB |
Output is correct |
45 |
Correct |
101 ms |
102648 KB |
Output is correct |
46 |
Correct |
107 ms |
102392 KB |
Output is correct |
47 |
Correct |
108 ms |
102136 KB |
Output is correct |
48 |
Correct |
104 ms |
102396 KB |
Output is correct |
49 |
Correct |
96 ms |
102440 KB |
Output is correct |
50 |
Correct |
104 ms |
102520 KB |
Output is correct |
51 |
Correct |
109 ms |
102472 KB |
Output is correct |
52 |
Correct |
89 ms |
102492 KB |
Output is correct |
53 |
Correct |
98 ms |
102504 KB |
Output is correct |
54 |
Correct |
102 ms |
102520 KB |
Output is correct |
55 |
Correct |
113 ms |
102108 KB |
Output is correct |
56 |
Correct |
212 ms |
112204 KB |
Output is correct |
57 |
Correct |
277 ms |
113940 KB |
Output is correct |
58 |
Correct |
107 ms |
102412 KB |
Output is correct |
59 |
Correct |
91 ms |
102108 KB |
Output is correct |
60 |
Correct |
87 ms |
102084 KB |
Output is correct |
61 |
Correct |
232 ms |
113004 KB |
Output is correct |
62 |
Correct |
98 ms |
102392 KB |
Output is correct |
63 |
Correct |
282 ms |
124136 KB |
Output is correct |
64 |
Correct |
249 ms |
113900 KB |
Output is correct |
65 |
Correct |
90 ms |
102392 KB |
Output is correct |
66 |
Correct |
235 ms |
113692 KB |
Output is correct |
67 |
Correct |
93 ms |
102520 KB |
Output is correct |
68 |
Correct |
98 ms |
102392 KB |
Output is correct |
69 |
Correct |
207 ms |
112364 KB |
Output is correct |
70 |
Correct |
99 ms |
102776 KB |
Output is correct |
71 |
Correct |
266 ms |
113680 KB |
Output is correct |
72 |
Correct |
283 ms |
116712 KB |
Output is correct |
73 |
Correct |
88 ms |
102264 KB |
Output is correct |
74 |
Correct |
312 ms |
124468 KB |
Output is correct |
75 |
Correct |
317 ms |
118748 KB |
Output is correct |
76 |
Correct |
111 ms |
102520 KB |
Output is correct |
77 |
Correct |
115 ms |
102456 KB |
Output is correct |
78 |
Correct |
215 ms |
110832 KB |
Output is correct |
79 |
Correct |
205 ms |
111212 KB |
Output is correct |
80 |
Correct |
101 ms |
102540 KB |
Output is correct |
81 |
Correct |
103 ms |
102392 KB |
Output is correct |
82 |
Correct |
103 ms |
102300 KB |
Output is correct |
83 |
Correct |
103 ms |
102308 KB |
Output is correct |
84 |
Correct |
97 ms |
102452 KB |
Output is correct |
85 |
Correct |
285 ms |
112876 KB |
Output is correct |
86 |
Correct |
92 ms |
102392 KB |
Output is correct |
87 |
Correct |
304 ms |
112492 KB |
Output is correct |
88 |
Correct |
98 ms |
102140 KB |
Output is correct |
89 |
Correct |
259 ms |
112208 KB |
Output is correct |
90 |
Correct |
272 ms |
112504 KB |
Output is correct |
91 |
Correct |
273 ms |
112616 KB |
Output is correct |
92 |
Correct |
244 ms |
112492 KB |
Output is correct |
93 |
Correct |
114 ms |
102520 KB |
Output is correct |
94 |
Correct |
99 ms |
102236 KB |
Output is correct |
95 |
Correct |
280 ms |
113328 KB |
Output is correct |
96 |
Correct |
288 ms |
119404 KB |
Output is correct |
97 |
Correct |
116 ms |
102392 KB |
Output is correct |
98 |
Correct |
116 ms |
102124 KB |
Output is correct |
99 |
Correct |
318 ms |
114156 KB |
Output is correct |
100 |
Correct |
108 ms |
102520 KB |
Output is correct |
101 |
Correct |
257 ms |
112144 KB |
Output is correct |
102 |
Correct |
322 ms |
116196 KB |
Output is correct |
103 |
Correct |
101 ms |
102396 KB |
Output is correct |
104 |
Correct |
98 ms |
102540 KB |
Output is correct |
105 |
Correct |
98 ms |
102464 KB |
Output is correct |
106 |
Correct |
116 ms |
102520 KB |
Output is correct |
107 |
Correct |
93 ms |
102520 KB |
Output is correct |
108 |
Correct |
1730 ms |
163276 KB |
Output is correct |
109 |
Correct |
1568 ms |
182680 KB |
Output is correct |
110 |
Correct |
1549 ms |
201984 KB |
Output is correct |
111 |
Correct |
1541 ms |
191964 KB |
Output is correct |
112 |
Correct |
1885 ms |
183824 KB |
Output is correct |
113 |
Correct |
1226 ms |
159308 KB |
Output is correct |
114 |
Correct |
1222 ms |
161788 KB |
Output is correct |
115 |
Correct |
1153 ms |
162012 KB |
Output is correct |
116 |
Correct |
1730 ms |
161684 KB |
Output is correct |
117 |
Correct |
1179 ms |
155744 KB |
Output is correct |
118 |
Correct |
1064 ms |
156268 KB |
Output is correct |
119 |
Correct |
1178 ms |
155836 KB |
Output is correct |
120 |
Correct |
1429 ms |
174132 KB |
Output is correct |
121 |
Correct |
1252 ms |
155872 KB |
Output is correct |
122 |
Correct |
1890 ms |
175544 KB |
Output is correct |
123 |
Correct |
1061 ms |
157148 KB |
Output is correct |
124 |
Correct |
1351 ms |
157404 KB |
Output is correct |