# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
366218 |
2021-02-13T14:20:44 Z |
vaaven |
Mergers (JOI19_mergers) |
C++14 |
|
1293 ms |
139756 KB |
//#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <set>
#include <stack>
#include <iomanip>
#include <bitset>
#include <map>
#include <cassert>
#include <array>
#include <queue>
#include <cstring>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
// #define endl '\n'
#define all(x) x.begin(), x.end()
//#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
const int inf = 1e9;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-14;
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("inputik.txt", "r", stdin);
// freopen("outputik.txt", "w", stdout);
}
const int maxn = 5e5 + 228;
vi g[maxn];
int up[maxn][20], a[maxn], tin[maxn], tout[maxn], link[maxn], sizex[maxn];
int timer = 0;
void unite(int a, int b);
void dfs(int v, int p = 0){
up[v][0] = p;
tin[v] = timer++;
for(int i=1; i<20; i++){
up[v][i] = up[up[v][i-1]][i-1];
}
for(int i:g[v]){
if(i != p){
dfs(i, v);
}
}
tout[v] = ++timer;
}
void dfs2(int v, int p = -1){
for(int i:g[v]){
if(i != p){
dfs2(i, v);
a[v] += a[i];
}
}
if(a[v])
unite(v, p);
}
bool upper(int a, int b){
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca(int a, int b){
if(upper(a, b))
return a;
if(upper(b, a))
return b;
int cur = a;
for(int i=19; i>=0; i--){
if(!upper(up[cur][i], b)){
cur = up[cur][i];
}
}
cur = up[cur][0];
return cur;
}
int find(int a){
return (link[a] == a ? a : link[a] = find(link[a]));
}
void unite(int a, int b){
// cout << 1 << endl;
a = find(a);
b = find(b);
if(a == b)
return;
if(sizex[a] > sizex[b])
swap(a, b);
link[a] = b;
sizex[b] += sizex[a];
}
void solve(){
int n, k;
cin >> n >> k;
vvi col(k);
for(int i=0; i+1<n; i++){
int a, b;
cin >> a >> b;
a--, b--;
g[a].pb(b);
g[b].pb(a);
}
for(int i=0; i<n; i++){
int a;
cin >> a;
col[a-1].pb(i);
}
dfs(0);
for(int i=0; i<k; i++){
int cur = col[i][0];
for(int j=1; j<col[i].size(); j++){
cur = lca(cur, col[i][j]);
}
// cout << i << " " << cur << endl;
for(int j:col[i]){
a[j]++;
a[cur]--;
}
}
for(int i=0; i<maxn; i++){
link[i] = i;
sizex[i] = 1;
}
dfs2(0);
int ans = 0;
vi cnt(n, 0);
for(int i=0; i<n; i++){
for(int j:g[i]){
if(find(i) != find(j)){
cnt[find(i)]++;
cnt[find(j)]++;
}
}
}
for(int i=0; i<n; i++){
if(cnt[i] == 2 && find(i) == i){
// cout << 1 << endl;
ans++;
}
}
// cout << endl;
// cout << ans << endl;
cout << max((ans+1)/2, 0) << endl;
}
/*
5 4
1 2
2 3
3 4
3 5
1
2
1
3
4
=======
5 4
1 2
2 3
3 4
4 5
1
2
3
4
1
=======
2 2
1 2
1
2
*/
signed main(){
fast_io();
// srand(time(NULL));
cout << fixed << setprecision(10);
int q = 1;
// cin >> q;
while(q--)
solve();
}
Compilation message
mergers.cpp: In function 'void solve()':
mergers.cpp:138:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
138 | for(int j=1; j<col[i].size(); j++){
| ~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
12 ms |
15980 KB |
Output is correct |
8 |
Correct |
11 ms |
15980 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
13 ms |
15980 KB |
Output is correct |
11 |
Correct |
11 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
12 ms |
15980 KB |
Output is correct |
15 |
Correct |
12 ms |
15980 KB |
Output is correct |
16 |
Correct |
11 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
16128 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
12 ms |
15980 KB |
Output is correct |
8 |
Correct |
11 ms |
15980 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
13 ms |
15980 KB |
Output is correct |
11 |
Correct |
11 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
12 ms |
15980 KB |
Output is correct |
15 |
Correct |
12 ms |
15980 KB |
Output is correct |
16 |
Correct |
11 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
16128 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
25 |
Correct |
11 ms |
15980 KB |
Output is correct |
26 |
Correct |
13 ms |
16620 KB |
Output is correct |
27 |
Correct |
13 ms |
16492 KB |
Output is correct |
28 |
Correct |
14 ms |
16748 KB |
Output is correct |
29 |
Correct |
14 ms |
16620 KB |
Output is correct |
30 |
Correct |
13 ms |
16512 KB |
Output is correct |
31 |
Correct |
11 ms |
15980 KB |
Output is correct |
32 |
Correct |
14 ms |
16748 KB |
Output is correct |
33 |
Correct |
11 ms |
15980 KB |
Output is correct |
34 |
Correct |
13 ms |
16512 KB |
Output is correct |
35 |
Correct |
14 ms |
16620 KB |
Output is correct |
36 |
Correct |
14 ms |
16620 KB |
Output is correct |
37 |
Correct |
14 ms |
16492 KB |
Output is correct |
38 |
Correct |
12 ms |
16108 KB |
Output is correct |
39 |
Correct |
14 ms |
16492 KB |
Output is correct |
40 |
Correct |
13 ms |
16492 KB |
Output is correct |
41 |
Correct |
13 ms |
16492 KB |
Output is correct |
42 |
Correct |
13 ms |
16492 KB |
Output is correct |
43 |
Correct |
13 ms |
16748 KB |
Output is correct |
44 |
Correct |
11 ms |
16108 KB |
Output is correct |
45 |
Correct |
13 ms |
16620 KB |
Output is correct |
46 |
Correct |
13 ms |
16492 KB |
Output is correct |
47 |
Correct |
11 ms |
16108 KB |
Output is correct |
48 |
Correct |
14 ms |
16492 KB |
Output is correct |
49 |
Correct |
13 ms |
16620 KB |
Output is correct |
50 |
Correct |
13 ms |
16620 KB |
Output is correct |
51 |
Correct |
13 ms |
16492 KB |
Output is correct |
52 |
Correct |
13 ms |
16492 KB |
Output is correct |
53 |
Correct |
13 ms |
16492 KB |
Output is correct |
54 |
Correct |
13 ms |
16492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
12 ms |
15980 KB |
Output is correct |
8 |
Correct |
11 ms |
15980 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
13 ms |
15980 KB |
Output is correct |
11 |
Correct |
11 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
12 ms |
15980 KB |
Output is correct |
15 |
Correct |
12 ms |
15980 KB |
Output is correct |
16 |
Correct |
11 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
16128 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
25 |
Correct |
13 ms |
15980 KB |
Output is correct |
26 |
Correct |
75 ms |
30948 KB |
Output is correct |
27 |
Correct |
113 ms |
30700 KB |
Output is correct |
28 |
Correct |
15 ms |
16492 KB |
Output is correct |
29 |
Correct |
13 ms |
15980 KB |
Output is correct |
30 |
Correct |
13 ms |
15980 KB |
Output is correct |
31 |
Correct |
120 ms |
30660 KB |
Output is correct |
32 |
Correct |
13 ms |
16492 KB |
Output is correct |
33 |
Correct |
147 ms |
37740 KB |
Output is correct |
34 |
Correct |
130 ms |
30956 KB |
Output is correct |
35 |
Correct |
13 ms |
16492 KB |
Output is correct |
36 |
Correct |
126 ms |
31340 KB |
Output is correct |
37 |
Correct |
13 ms |
16512 KB |
Output is correct |
38 |
Correct |
15 ms |
16492 KB |
Output is correct |
39 |
Correct |
83 ms |
30948 KB |
Output is correct |
40 |
Correct |
15 ms |
16748 KB |
Output is correct |
41 |
Correct |
110 ms |
30748 KB |
Output is correct |
42 |
Correct |
136 ms |
33004 KB |
Output is correct |
43 |
Correct |
11 ms |
16108 KB |
Output is correct |
44 |
Correct |
163 ms |
37988 KB |
Output is correct |
45 |
Correct |
164 ms |
34412 KB |
Output is correct |
46 |
Correct |
13 ms |
16492 KB |
Output is correct |
47 |
Correct |
13 ms |
16492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
30948 KB |
Output is correct |
2 |
Correct |
108 ms |
36196 KB |
Output is correct |
3 |
Correct |
14 ms |
16620 KB |
Output is correct |
4 |
Correct |
15 ms |
16492 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
13 ms |
16492 KB |
Output is correct |
8 |
Correct |
147 ms |
33288 KB |
Output is correct |
9 |
Correct |
13 ms |
16492 KB |
Output is correct |
10 |
Correct |
130 ms |
30828 KB |
Output is correct |
11 |
Correct |
11 ms |
16108 KB |
Output is correct |
12 |
Correct |
121 ms |
30828 KB |
Output is correct |
13 |
Correct |
155 ms |
33132 KB |
Output is correct |
14 |
Correct |
141 ms |
36076 KB |
Output is correct |
15 |
Correct |
77 ms |
30948 KB |
Output is correct |
16 |
Correct |
13 ms |
16492 KB |
Output is correct |
17 |
Correct |
12 ms |
16108 KB |
Output is correct |
18 |
Correct |
101 ms |
35684 KB |
Output is correct |
19 |
Correct |
181 ms |
40000 KB |
Output is correct |
20 |
Correct |
14 ms |
16492 KB |
Output is correct |
21 |
Correct |
11 ms |
16108 KB |
Output is correct |
22 |
Correct |
96 ms |
33380 KB |
Output is correct |
23 |
Correct |
13 ms |
16492 KB |
Output is correct |
24 |
Correct |
143 ms |
31340 KB |
Output is correct |
25 |
Correct |
165 ms |
38508 KB |
Output is correct |
26 |
Correct |
13 ms |
16620 KB |
Output is correct |
27 |
Correct |
14 ms |
16620 KB |
Output is correct |
28 |
Correct |
14 ms |
16492 KB |
Output is correct |
29 |
Correct |
13 ms |
16492 KB |
Output is correct |
30 |
Correct |
13 ms |
16512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15980 KB |
Output is correct |
2 |
Correct |
11 ms |
15980 KB |
Output is correct |
3 |
Correct |
11 ms |
15980 KB |
Output is correct |
4 |
Correct |
11 ms |
15980 KB |
Output is correct |
5 |
Correct |
11 ms |
15980 KB |
Output is correct |
6 |
Correct |
11 ms |
15980 KB |
Output is correct |
7 |
Correct |
12 ms |
15980 KB |
Output is correct |
8 |
Correct |
11 ms |
15980 KB |
Output is correct |
9 |
Correct |
11 ms |
15980 KB |
Output is correct |
10 |
Correct |
13 ms |
15980 KB |
Output is correct |
11 |
Correct |
11 ms |
15980 KB |
Output is correct |
12 |
Correct |
11 ms |
15980 KB |
Output is correct |
13 |
Correct |
11 ms |
15980 KB |
Output is correct |
14 |
Correct |
12 ms |
15980 KB |
Output is correct |
15 |
Correct |
12 ms |
15980 KB |
Output is correct |
16 |
Correct |
11 ms |
15980 KB |
Output is correct |
17 |
Correct |
11 ms |
15980 KB |
Output is correct |
18 |
Correct |
11 ms |
15980 KB |
Output is correct |
19 |
Correct |
11 ms |
15980 KB |
Output is correct |
20 |
Correct |
11 ms |
16128 KB |
Output is correct |
21 |
Correct |
11 ms |
15980 KB |
Output is correct |
22 |
Correct |
11 ms |
15980 KB |
Output is correct |
23 |
Correct |
11 ms |
15980 KB |
Output is correct |
24 |
Correct |
11 ms |
15980 KB |
Output is correct |
25 |
Correct |
11 ms |
15980 KB |
Output is correct |
26 |
Correct |
13 ms |
16620 KB |
Output is correct |
27 |
Correct |
13 ms |
16492 KB |
Output is correct |
28 |
Correct |
14 ms |
16748 KB |
Output is correct |
29 |
Correct |
14 ms |
16620 KB |
Output is correct |
30 |
Correct |
13 ms |
16512 KB |
Output is correct |
31 |
Correct |
11 ms |
15980 KB |
Output is correct |
32 |
Correct |
14 ms |
16748 KB |
Output is correct |
33 |
Correct |
11 ms |
15980 KB |
Output is correct |
34 |
Correct |
13 ms |
16512 KB |
Output is correct |
35 |
Correct |
14 ms |
16620 KB |
Output is correct |
36 |
Correct |
14 ms |
16620 KB |
Output is correct |
37 |
Correct |
14 ms |
16492 KB |
Output is correct |
38 |
Correct |
12 ms |
16108 KB |
Output is correct |
39 |
Correct |
14 ms |
16492 KB |
Output is correct |
40 |
Correct |
13 ms |
16492 KB |
Output is correct |
41 |
Correct |
13 ms |
16492 KB |
Output is correct |
42 |
Correct |
13 ms |
16492 KB |
Output is correct |
43 |
Correct |
13 ms |
16748 KB |
Output is correct |
44 |
Correct |
11 ms |
16108 KB |
Output is correct |
45 |
Correct |
13 ms |
16620 KB |
Output is correct |
46 |
Correct |
13 ms |
16492 KB |
Output is correct |
47 |
Correct |
11 ms |
16108 KB |
Output is correct |
48 |
Correct |
14 ms |
16492 KB |
Output is correct |
49 |
Correct |
13 ms |
16620 KB |
Output is correct |
50 |
Correct |
13 ms |
16620 KB |
Output is correct |
51 |
Correct |
13 ms |
16492 KB |
Output is correct |
52 |
Correct |
13 ms |
16492 KB |
Output is correct |
53 |
Correct |
13 ms |
16492 KB |
Output is correct |
54 |
Correct |
13 ms |
16492 KB |
Output is correct |
55 |
Correct |
13 ms |
15980 KB |
Output is correct |
56 |
Correct |
75 ms |
30948 KB |
Output is correct |
57 |
Correct |
113 ms |
30700 KB |
Output is correct |
58 |
Correct |
15 ms |
16492 KB |
Output is correct |
59 |
Correct |
13 ms |
15980 KB |
Output is correct |
60 |
Correct |
13 ms |
15980 KB |
Output is correct |
61 |
Correct |
120 ms |
30660 KB |
Output is correct |
62 |
Correct |
13 ms |
16492 KB |
Output is correct |
63 |
Correct |
147 ms |
37740 KB |
Output is correct |
64 |
Correct |
130 ms |
30956 KB |
Output is correct |
65 |
Correct |
13 ms |
16492 KB |
Output is correct |
66 |
Correct |
126 ms |
31340 KB |
Output is correct |
67 |
Correct |
13 ms |
16512 KB |
Output is correct |
68 |
Correct |
15 ms |
16492 KB |
Output is correct |
69 |
Correct |
83 ms |
30948 KB |
Output is correct |
70 |
Correct |
15 ms |
16748 KB |
Output is correct |
71 |
Correct |
110 ms |
30748 KB |
Output is correct |
72 |
Correct |
136 ms |
33004 KB |
Output is correct |
73 |
Correct |
11 ms |
16108 KB |
Output is correct |
74 |
Correct |
163 ms |
37988 KB |
Output is correct |
75 |
Correct |
164 ms |
34412 KB |
Output is correct |
76 |
Correct |
13 ms |
16492 KB |
Output is correct |
77 |
Correct |
13 ms |
16492 KB |
Output is correct |
78 |
Correct |
76 ms |
30948 KB |
Output is correct |
79 |
Correct |
108 ms |
36196 KB |
Output is correct |
80 |
Correct |
14 ms |
16620 KB |
Output is correct |
81 |
Correct |
15 ms |
16492 KB |
Output is correct |
82 |
Correct |
11 ms |
15980 KB |
Output is correct |
83 |
Correct |
11 ms |
15980 KB |
Output is correct |
84 |
Correct |
13 ms |
16492 KB |
Output is correct |
85 |
Correct |
147 ms |
33288 KB |
Output is correct |
86 |
Correct |
13 ms |
16492 KB |
Output is correct |
87 |
Correct |
130 ms |
30828 KB |
Output is correct |
88 |
Correct |
11 ms |
16108 KB |
Output is correct |
89 |
Correct |
121 ms |
30828 KB |
Output is correct |
90 |
Correct |
155 ms |
33132 KB |
Output is correct |
91 |
Correct |
141 ms |
36076 KB |
Output is correct |
92 |
Correct |
77 ms |
30948 KB |
Output is correct |
93 |
Correct |
13 ms |
16492 KB |
Output is correct |
94 |
Correct |
12 ms |
16108 KB |
Output is correct |
95 |
Correct |
101 ms |
35684 KB |
Output is correct |
96 |
Correct |
181 ms |
40000 KB |
Output is correct |
97 |
Correct |
14 ms |
16492 KB |
Output is correct |
98 |
Correct |
11 ms |
16108 KB |
Output is correct |
99 |
Correct |
96 ms |
33380 KB |
Output is correct |
100 |
Correct |
13 ms |
16492 KB |
Output is correct |
101 |
Correct |
143 ms |
31340 KB |
Output is correct |
102 |
Correct |
165 ms |
38508 KB |
Output is correct |
103 |
Correct |
13 ms |
16620 KB |
Output is correct |
104 |
Correct |
14 ms |
16620 KB |
Output is correct |
105 |
Correct |
14 ms |
16492 KB |
Output is correct |
106 |
Correct |
13 ms |
16492 KB |
Output is correct |
107 |
Correct |
13 ms |
16512 KB |
Output is correct |
108 |
Correct |
673 ms |
101784 KB |
Output is correct |
109 |
Correct |
1066 ms |
107012 KB |
Output is correct |
110 |
Correct |
1067 ms |
118236 KB |
Output is correct |
111 |
Correct |
1293 ms |
139756 KB |
Output is correct |
112 |
Correct |
1231 ms |
130008 KB |
Output is correct |
113 |
Correct |
718 ms |
115540 KB |
Output is correct |
114 |
Correct |
765 ms |
88968 KB |
Output is correct |
115 |
Correct |
783 ms |
88924 KB |
Output is correct |
116 |
Correct |
970 ms |
95992 KB |
Output is correct |
117 |
Correct |
998 ms |
116588 KB |
Output is correct |
118 |
Correct |
778 ms |
89452 KB |
Output is correct |
119 |
Correct |
992 ms |
116588 KB |
Output is correct |
120 |
Correct |
1228 ms |
130796 KB |
Output is correct |
121 |
Correct |
989 ms |
116588 KB |
Output is correct |
122 |
Correct |
1049 ms |
105836 KB |
Output is correct |
123 |
Correct |
710 ms |
118080 KB |
Output is correct |
124 |
Correct |
1084 ms |
114540 KB |
Output is correct |