# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
185399 | 2020-01-11T12:45:09 Z | koosaga(C15-#575) | Mergers (JOI19_mergers) | C++17 | 2719 ms | 128556 KB |
#include <bits/stdc++.h> using namespace std; const int MAXN = 500050; using lint = long long; using pi = pair<int, int>; struct disj{ int pa[MAXN]; void init(int n){ iota(pa, pa + n + 1, 0); } int find(int x){ return pa[x] = (pa[x] == x ? x : find(pa[x])); } bool uni(int p, int q){ p = find(p); q = find(q); if(p == q) return 0; pa[q] = p; return 1; } }disj; int n, k; vector<int> gph[MAXN]; vector<int> grp[MAXN]; vector<int> dfn; int par[19][MAXN], dep[MAXN], din[MAXN]; int cnt[MAXN], dx[MAXN], piv; int lca(int x, int y){ if(dep[x] > dep[y]) swap(x, y); int dx = dep[y] - dep[x]; for(int i=0; i<19; i++){ if((dx >> i) & 1) y = par[i][y]; } for(int i=18; i>=0; i--){ if(par[i][x] != par[i][y]){ x = par[i][x]; y = par[i][y]; } } if(x != y) return par[0][x]; return x; } void dfs(int x, int p){ dfn.push_back(x); din[x] = ++piv; for(auto &i : gph[x]){ if(i != p){ par[0][i] = x; dep[i] = dep[x] + 1; dfs(i, x); } } } int main(){ scanf("%d %d",&n,&k); for(int i=0; i<n-1; i++){ int s, e; scanf("%d %d",&s,&e); gph[s].push_back(e); gph[e].push_back(s); } for(int i=1; i<=n; i++){ int x; scanf("%d",&x); grp[x].push_back(i); } dfs(1, 0); for(int i=1; i<19; i++){ for(int j=1; j<=n; j++){ par[i][j] = par[i-1][par[i-1][j]]; } } for(int i=1; i<=k; i++){ sort(grp[i].begin(), grp[i].end(), [&](const int &a, const int &b){ return din[a] < din[b]; }); for(int j=0; j+1<grp[i].size(); j++){ int ps = grp[i][j]; int pe = grp[i][j+1]; dx[pe]++; dx[ps]++; dx[lca(ps, pe)] -= 2; } } disj.init(n); for(int i=dfn.size()-1; i>=0; i--){ int x = dfn[i]; for(auto &j : gph[x]){ if(j != par[0][x]) dx[x] += dx[j]; } if(dx[x] > 0) disj.uni(par[0][x], x); } for(int i=2; i<=n; i++){ int l = disj.find(par[0][i]); int r = disj.find(i); if(l != r) cnt[l]++, cnt[r]++; } int sum = count(cnt + 1, cnt + n + 1, 1); printf("%d\n", (sum + 1) / 2); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 23928 KB | Output is correct |
2 | Correct | 29 ms | 23928 KB | Output is correct |
3 | Correct | 29 ms | 24028 KB | Output is correct |
4 | Correct | 34 ms | 24028 KB | Output is correct |
5 | Correct | 30 ms | 23928 KB | Output is correct |
6 | Correct | 31 ms | 23928 KB | Output is correct |
7 | Correct | 29 ms | 23956 KB | Output is correct |
8 | Correct | 30 ms | 23928 KB | Output is correct |
9 | Correct | 33 ms | 24056 KB | Output is correct |
10 | Correct | 25 ms | 23928 KB | Output is correct |
11 | Correct | 19 ms | 23928 KB | Output is correct |
12 | Correct | 25 ms | 24056 KB | Output is correct |
13 | Correct | 25 ms | 24056 KB | Output is correct |
14 | Correct | 25 ms | 24060 KB | Output is correct |
15 | Correct | 24 ms | 23972 KB | Output is correct |
16 | Correct | 23 ms | 24056 KB | Output is correct |
17 | Correct | 24 ms | 23928 KB | Output is correct |
18 | Correct | 25 ms | 24028 KB | Output is correct |
19 | Correct | 24 ms | 23928 KB | Output is correct |
20 | Correct | 25 ms | 24060 KB | Output is correct |
21 | Correct | 25 ms | 24056 KB | Output is correct |
22 | Correct | 26 ms | 24056 KB | Output is correct |
23 | Correct | 24 ms | 24056 KB | Output is correct |
24 | Correct | 30 ms | 24056 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 23928 KB | Output is correct |
2 | Correct | 29 ms | 23928 KB | Output is correct |
3 | Correct | 29 ms | 24028 KB | Output is correct |
4 | Correct | 34 ms | 24028 KB | Output is correct |
5 | Correct | 30 ms | 23928 KB | Output is correct |
6 | Correct | 31 ms | 23928 KB | Output is correct |
7 | Correct | 29 ms | 23956 KB | Output is correct |
8 | Correct | 30 ms | 23928 KB | Output is correct |
9 | Correct | 33 ms | 24056 KB | Output is correct |
10 | Correct | 25 ms | 23928 KB | Output is correct |
11 | Correct | 19 ms | 23928 KB | Output is correct |
12 | Correct | 25 ms | 24056 KB | Output is correct |
13 | Correct | 25 ms | 24056 KB | Output is correct |
14 | Correct | 25 ms | 24060 KB | Output is correct |
15 | Correct | 24 ms | 23972 KB | Output is correct |
16 | Correct | 23 ms | 24056 KB | Output is correct |
17 | Correct | 24 ms | 23928 KB | Output is correct |
18 | Correct | 25 ms | 24028 KB | Output is correct |
19 | Correct | 24 ms | 23928 KB | Output is correct |
20 | Correct | 25 ms | 24060 KB | Output is correct |
21 | Correct | 25 ms | 24056 KB | Output is correct |
22 | Correct | 26 ms | 24056 KB | Output is correct |
23 | Correct | 24 ms | 24056 KB | Output is correct |
24 | Correct | 30 ms | 24056 KB | Output is correct |
25 | Correct | 30 ms | 24056 KB | Output is correct |
26 | Correct | 34 ms | 24508 KB | Output is correct |
27 | Correct | 26 ms | 24440 KB | Output is correct |
28 | Correct | 30 ms | 24640 KB | Output is correct |
29 | Correct | 32 ms | 24568 KB | Output is correct |
30 | Correct | 33 ms | 24696 KB | Output is correct |
31 | Correct | 25 ms | 23928 KB | Output is correct |
32 | Correct | 27 ms | 24696 KB | Output is correct |
33 | Correct | 23 ms | 23928 KB | Output is correct |
34 | Correct | 29 ms | 24396 KB | Output is correct |
35 | Correct | 29 ms | 24568 KB | Output is correct |
36 | Correct | 29 ms | 24568 KB | Output is correct |
37 | Correct | 27 ms | 24568 KB | Output is correct |
38 | Correct | 20 ms | 24028 KB | Output is correct |
39 | Correct | 29 ms | 24568 KB | Output is correct |
40 | Correct | 28 ms | 24440 KB | Output is correct |
41 | Correct | 28 ms | 24536 KB | Output is correct |
42 | Correct | 33 ms | 24476 KB | Output is correct |
43 | Correct | 29 ms | 24568 KB | Output is correct |
44 | Correct | 31 ms | 24056 KB | Output is correct |
45 | Correct | 34 ms | 24568 KB | Output is correct |
46 | Correct | 32 ms | 24608 KB | Output is correct |
47 | Correct | 23 ms | 24056 KB | Output is correct |
48 | Correct | 34 ms | 24440 KB | Output is correct |
49 | Correct | 33 ms | 24556 KB | Output is correct |
50 | Correct | 33 ms | 24568 KB | Output is correct |
51 | Correct | 34 ms | 24440 KB | Output is correct |
52 | Correct | 25 ms | 24312 KB | Output is correct |
53 | Correct | 33 ms | 24440 KB | Output is correct |
54 | Correct | 34 ms | 24440 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 23928 KB | Output is correct |
2 | Correct | 29 ms | 23928 KB | Output is correct |
3 | Correct | 29 ms | 24028 KB | Output is correct |
4 | Correct | 34 ms | 24028 KB | Output is correct |
5 | Correct | 30 ms | 23928 KB | Output is correct |
6 | Correct | 31 ms | 23928 KB | Output is correct |
7 | Correct | 29 ms | 23956 KB | Output is correct |
8 | Correct | 30 ms | 23928 KB | Output is correct |
9 | Correct | 33 ms | 24056 KB | Output is correct |
10 | Correct | 25 ms | 23928 KB | Output is correct |
11 | Correct | 19 ms | 23928 KB | Output is correct |
12 | Correct | 25 ms | 24056 KB | Output is correct |
13 | Correct | 25 ms | 24056 KB | Output is correct |
14 | Correct | 25 ms | 24060 KB | Output is correct |
15 | Correct | 24 ms | 23972 KB | Output is correct |
16 | Correct | 23 ms | 24056 KB | Output is correct |
17 | Correct | 24 ms | 23928 KB | Output is correct |
18 | Correct | 25 ms | 24028 KB | Output is correct |
19 | Correct | 24 ms | 23928 KB | Output is correct |
20 | Correct | 25 ms | 24060 KB | Output is correct |
21 | Correct | 25 ms | 24056 KB | Output is correct |
22 | Correct | 26 ms | 24056 KB | Output is correct |
23 | Correct | 24 ms | 24056 KB | Output is correct |
24 | Correct | 30 ms | 24056 KB | Output is correct |
25 | Correct | 28 ms | 24056 KB | Output is correct |
26 | Correct | 298 ms | 38968 KB | Output is correct |
27 | Correct | 528 ms | 38720 KB | Output is correct |
28 | Correct | 27 ms | 24444 KB | Output is correct |
29 | Correct | 33 ms | 23928 KB | Output is correct |
30 | Correct | 31 ms | 24184 KB | Output is correct |
31 | Correct | 300 ms | 38796 KB | Output is correct |
32 | Correct | 27 ms | 24440 KB | Output is correct |
33 | Correct | 498 ms | 43012 KB | Output is correct |
34 | Correct | 412 ms | 38904 KB | Output is correct |
35 | Correct | 28 ms | 24440 KB | Output is correct |
36 | Correct | 519 ms | 39252 KB | Output is correct |
37 | Correct | 34 ms | 24440 KB | Output is correct |
38 | Correct | 39 ms | 24568 KB | Output is correct |
39 | Correct | 441 ms | 38908 KB | Output is correct |
40 | Correct | 34 ms | 24568 KB | Output is correct |
41 | Correct | 433 ms | 38640 KB | Output is correct |
42 | Correct | 537 ms | 40048 KB | Output is correct |
43 | Correct | 30 ms | 23928 KB | Output is correct |
44 | Correct | 472 ms | 43184 KB | Output is correct |
45 | Correct | 361 ms | 41004 KB | Output is correct |
46 | Correct | 28 ms | 24440 KB | Output is correct |
47 | Correct | 26 ms | 24440 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 250 ms | 39164 KB | Output is correct |
2 | Correct | 123 ms | 41836 KB | Output is correct |
3 | Correct | 27 ms | 24568 KB | Output is correct |
4 | Correct | 33 ms | 24384 KB | Output is correct |
5 | Correct | 25 ms | 24028 KB | Output is correct |
6 | Correct | 31 ms | 23952 KB | Output is correct |
7 | Correct | 32 ms | 24440 KB | Output is correct |
8 | Correct | 403 ms | 40540 KB | Output is correct |
9 | Correct | 33 ms | 24440 KB | Output is correct |
10 | Correct | 436 ms | 39380 KB | Output is correct |
11 | Correct | 29 ms | 24056 KB | Output is correct |
12 | Correct | 407 ms | 39244 KB | Output is correct |
13 | Correct | 265 ms | 40436 KB | Output is correct |
14 | Correct | 156 ms | 41968 KB | Output is correct |
15 | Correct | 275 ms | 39028 KB | Output is correct |
16 | Correct | 27 ms | 24444 KB | Output is correct |
17 | Correct | 25 ms | 24028 KB | Output is correct |
18 | Correct | 149 ms | 42144 KB | Output is correct |
19 | Correct | 174 ms | 44388 KB | Output is correct |
20 | Correct | 28 ms | 24540 KB | Output is correct |
21 | Correct | 24 ms | 23928 KB | Output is correct |
22 | Correct | 231 ms | 40744 KB | Output is correct |
23 | Correct | 29 ms | 24440 KB | Output is correct |
24 | Correct | 315 ms | 39768 KB | Output is correct |
25 | Correct | 230 ms | 43504 KB | Output is correct |
26 | Correct | 32 ms | 24568 KB | Output is correct |
27 | Correct | 34 ms | 24568 KB | Output is correct |
28 | Correct | 32 ms | 24568 KB | Output is correct |
29 | Correct | 28 ms | 24440 KB | Output is correct |
30 | Correct | 33 ms | 24440 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 25 ms | 23928 KB | Output is correct |
2 | Correct | 29 ms | 23928 KB | Output is correct |
3 | Correct | 29 ms | 24028 KB | Output is correct |
4 | Correct | 34 ms | 24028 KB | Output is correct |
5 | Correct | 30 ms | 23928 KB | Output is correct |
6 | Correct | 31 ms | 23928 KB | Output is correct |
7 | Correct | 29 ms | 23956 KB | Output is correct |
8 | Correct | 30 ms | 23928 KB | Output is correct |
9 | Correct | 33 ms | 24056 KB | Output is correct |
10 | Correct | 25 ms | 23928 KB | Output is correct |
11 | Correct | 19 ms | 23928 KB | Output is correct |
12 | Correct | 25 ms | 24056 KB | Output is correct |
13 | Correct | 25 ms | 24056 KB | Output is correct |
14 | Correct | 25 ms | 24060 KB | Output is correct |
15 | Correct | 24 ms | 23972 KB | Output is correct |
16 | Correct | 23 ms | 24056 KB | Output is correct |
17 | Correct | 24 ms | 23928 KB | Output is correct |
18 | Correct | 25 ms | 24028 KB | Output is correct |
19 | Correct | 24 ms | 23928 KB | Output is correct |
20 | Correct | 25 ms | 24060 KB | Output is correct |
21 | Correct | 25 ms | 24056 KB | Output is correct |
22 | Correct | 26 ms | 24056 KB | Output is correct |
23 | Correct | 24 ms | 24056 KB | Output is correct |
24 | Correct | 30 ms | 24056 KB | Output is correct |
25 | Correct | 30 ms | 24056 KB | Output is correct |
26 | Correct | 34 ms | 24508 KB | Output is correct |
27 | Correct | 26 ms | 24440 KB | Output is correct |
28 | Correct | 30 ms | 24640 KB | Output is correct |
29 | Correct | 32 ms | 24568 KB | Output is correct |
30 | Correct | 33 ms | 24696 KB | Output is correct |
31 | Correct | 25 ms | 23928 KB | Output is correct |
32 | Correct | 27 ms | 24696 KB | Output is correct |
33 | Correct | 23 ms | 23928 KB | Output is correct |
34 | Correct | 29 ms | 24396 KB | Output is correct |
35 | Correct | 29 ms | 24568 KB | Output is correct |
36 | Correct | 29 ms | 24568 KB | Output is correct |
37 | Correct | 27 ms | 24568 KB | Output is correct |
38 | Correct | 20 ms | 24028 KB | Output is correct |
39 | Correct | 29 ms | 24568 KB | Output is correct |
40 | Correct | 28 ms | 24440 KB | Output is correct |
41 | Correct | 28 ms | 24536 KB | Output is correct |
42 | Correct | 33 ms | 24476 KB | Output is correct |
43 | Correct | 29 ms | 24568 KB | Output is correct |
44 | Correct | 31 ms | 24056 KB | Output is correct |
45 | Correct | 34 ms | 24568 KB | Output is correct |
46 | Correct | 32 ms | 24608 KB | Output is correct |
47 | Correct | 23 ms | 24056 KB | Output is correct |
48 | Correct | 34 ms | 24440 KB | Output is correct |
49 | Correct | 33 ms | 24556 KB | Output is correct |
50 | Correct | 33 ms | 24568 KB | Output is correct |
51 | Correct | 34 ms | 24440 KB | Output is correct |
52 | Correct | 25 ms | 24312 KB | Output is correct |
53 | Correct | 33 ms | 24440 KB | Output is correct |
54 | Correct | 34 ms | 24440 KB | Output is correct |
55 | Correct | 28 ms | 24056 KB | Output is correct |
56 | Correct | 298 ms | 38968 KB | Output is correct |
57 | Correct | 528 ms | 38720 KB | Output is correct |
58 | Correct | 27 ms | 24444 KB | Output is correct |
59 | Correct | 33 ms | 23928 KB | Output is correct |
60 | Correct | 31 ms | 24184 KB | Output is correct |
61 | Correct | 300 ms | 38796 KB | Output is correct |
62 | Correct | 27 ms | 24440 KB | Output is correct |
63 | Correct | 498 ms | 43012 KB | Output is correct |
64 | Correct | 412 ms | 38904 KB | Output is correct |
65 | Correct | 28 ms | 24440 KB | Output is correct |
66 | Correct | 519 ms | 39252 KB | Output is correct |
67 | Correct | 34 ms | 24440 KB | Output is correct |
68 | Correct | 39 ms | 24568 KB | Output is correct |
69 | Correct | 441 ms | 38908 KB | Output is correct |
70 | Correct | 34 ms | 24568 KB | Output is correct |
71 | Correct | 433 ms | 38640 KB | Output is correct |
72 | Correct | 537 ms | 40048 KB | Output is correct |
73 | Correct | 30 ms | 23928 KB | Output is correct |
74 | Correct | 472 ms | 43184 KB | Output is correct |
75 | Correct | 361 ms | 41004 KB | Output is correct |
76 | Correct | 28 ms | 24440 KB | Output is correct |
77 | Correct | 26 ms | 24440 KB | Output is correct |
78 | Correct | 250 ms | 39164 KB | Output is correct |
79 | Correct | 123 ms | 41836 KB | Output is correct |
80 | Correct | 27 ms | 24568 KB | Output is correct |
81 | Correct | 33 ms | 24384 KB | Output is correct |
82 | Correct | 25 ms | 24028 KB | Output is correct |
83 | Correct | 31 ms | 23952 KB | Output is correct |
84 | Correct | 32 ms | 24440 KB | Output is correct |
85 | Correct | 403 ms | 40540 KB | Output is correct |
86 | Correct | 33 ms | 24440 KB | Output is correct |
87 | Correct | 436 ms | 39380 KB | Output is correct |
88 | Correct | 29 ms | 24056 KB | Output is correct |
89 | Correct | 407 ms | 39244 KB | Output is correct |
90 | Correct | 265 ms | 40436 KB | Output is correct |
91 | Correct | 156 ms | 41968 KB | Output is correct |
92 | Correct | 275 ms | 39028 KB | Output is correct |
93 | Correct | 27 ms | 24444 KB | Output is correct |
94 | Correct | 25 ms | 24028 KB | Output is correct |
95 | Correct | 149 ms | 42144 KB | Output is correct |
96 | Correct | 174 ms | 44388 KB | Output is correct |
97 | Correct | 28 ms | 24540 KB | Output is correct |
98 | Correct | 24 ms | 23928 KB | Output is correct |
99 | Correct | 231 ms | 40744 KB | Output is correct |
100 | Correct | 29 ms | 24440 KB | Output is correct |
101 | Correct | 315 ms | 39768 KB | Output is correct |
102 | Correct | 230 ms | 43504 KB | Output is correct |
103 | Correct | 32 ms | 24568 KB | Output is correct |
104 | Correct | 34 ms | 24568 KB | Output is correct |
105 | Correct | 32 ms | 24568 KB | Output is correct |
106 | Correct | 28 ms | 24440 KB | Output is correct |
107 | Correct | 33 ms | 24440 KB | Output is correct |
108 | Correct | 2145 ms | 106976 KB | Output is correct |
109 | Correct | 2300 ms | 107324 KB | Output is correct |
110 | Correct | 2719 ms | 114036 KB | Output is correct |
111 | Correct | 1056 ms | 128556 KB | Output is correct |
112 | Correct | 974 ms | 122596 KB | Output is correct |
113 | Correct | 841 ms | 114780 KB | Output is correct |
114 | Correct | 2133 ms | 96876 KB | Output is correct |
115 | Correct | 1970 ms | 96712 KB | Output is correct |
116 | Correct | 2092 ms | 103468 KB | Output is correct |
117 | Correct | 905 ms | 114872 KB | Output is correct |
118 | Correct | 1736 ms | 98848 KB | Output is correct |
119 | Correct | 986 ms | 114892 KB | Output is correct |
120 | Correct | 959 ms | 122912 KB | Output is correct |
121 | Correct | 994 ms | 114836 KB | Output is correct |
122 | Correct | 1632 ms | 108580 KB | Output is correct |
123 | Correct | 704 ms | 114404 KB | Output is correct |
124 | Correct | 950 ms | 112748 KB | Output is correct |