# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1097385 |
2024-10-07T07:07:58 Z |
Alihan_8 |
Mergers (JOI19_mergers) |
C++17 |
|
567 ms |
77400 KB |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ar array
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k; cin >> n >> k;
vector <vector<int>> adj(n);
for ( int i = 0; i + 1 < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
adj[u].pb(v);
adj[v].pb(u);
}
vector <int> s(n);
for ( auto &x: s ){
cin >> x; --x;
}
vector <int> tin(n), tout(n);
vector <ar<int,2>> f(k, {n, -1});
int ct = 0;
auto trav = [&](auto trav, int u, int p) -> void{
tin[u] = ++ct;
if ( f[s[u]][0] == n ) f[s[u]][0] = ct;
for ( auto &v: adj[u] ){
if ( v != p ) trav(trav, v, u);
} tout[u] = ct;
f[s[u]][1] = ct;
};
trav(trav, 0, -1);
vector <ar<int,2>> q(n);
vector <int> bad(n);
auto dfs = [&](auto dfs, int u, int p) -> void{
q[u] = f[s[u]];
for ( auto &v: adj[u] ){
if ( v != p ){
dfs(dfs, v, u);
q[u][0] = min(q[u][0], q[v][0]);
q[u][1] = max(q[u][1], q[v][1]);
}
}
if ( tin[u] <= q[u][0] && tout[u] >= q[u][1] ){
bad[u] = 1;
}
};
dfs(dfs, 0, -1);
vector <int> sub(n);
bad[0] = 0;
auto cals = [&](auto cals, int u, int p) -> void{
sub[u] = bad[u];
for ( auto &v: adj[u] ){
if ( v != p ){
cals(cals, v, u);
sub[u] += sub[v];
}
}
};
cals(cals, 0, -1);
int num = 0;
for ( int u = 1; u < n; u++ ){
if ( !bad[u] ) continue;
if ( sub[u] == sub[0] || sub[u] == 1 ){
num += 1;
}
}
cout << (num + 1) / 2;
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
656 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
616 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
2 ms |
668 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
2 ms |
728 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
2 ms |
676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
28 ms |
10408 KB |
Output is correct |
27 |
Correct |
38 ms |
10076 KB |
Output is correct |
28 |
Correct |
2 ms |
728 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
45 ms |
10076 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
55 ms |
16836 KB |
Output is correct |
34 |
Correct |
47 ms |
10064 KB |
Output is correct |
35 |
Correct |
2 ms |
600 KB |
Output is correct |
36 |
Correct |
46 ms |
10836 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
27 ms |
10264 KB |
Output is correct |
40 |
Correct |
2 ms |
856 KB |
Output is correct |
41 |
Correct |
38 ms |
10068 KB |
Output is correct |
42 |
Correct |
47 ms |
12112 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
47 ms |
16916 KB |
Output is correct |
45 |
Correct |
41 ms |
13672 KB |
Output is correct |
46 |
Correct |
1 ms |
608 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
10452 KB |
Output is correct |
2 |
Correct |
32 ms |
11484 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
452 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
40 ms |
10836 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
36 ms |
10264 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
41 ms |
10084 KB |
Output is correct |
13 |
Correct |
41 ms |
10836 KB |
Output is correct |
14 |
Correct |
39 ms |
11092 KB |
Output is correct |
15 |
Correct |
33 ms |
10324 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
28 ms |
11412 KB |
Output is correct |
19 |
Correct |
45 ms |
14932 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
27 ms |
10964 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
40 ms |
10268 KB |
Output is correct |
25 |
Correct |
47 ms |
13652 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
760 KB |
Output is correct |
28 |
Correct |
1 ms |
600 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
452 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
2 ms |
656 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
604 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
860 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
616 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
2 ms |
668 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
2 ms |
728 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
2 ms |
676 KB |
Output is correct |
55 |
Correct |
1 ms |
344 KB |
Output is correct |
56 |
Correct |
28 ms |
10408 KB |
Output is correct |
57 |
Correct |
38 ms |
10076 KB |
Output is correct |
58 |
Correct |
2 ms |
728 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
45 ms |
10076 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
55 ms |
16836 KB |
Output is correct |
64 |
Correct |
47 ms |
10064 KB |
Output is correct |
65 |
Correct |
2 ms |
600 KB |
Output is correct |
66 |
Correct |
46 ms |
10836 KB |
Output is correct |
67 |
Correct |
1 ms |
600 KB |
Output is correct |
68 |
Correct |
1 ms |
604 KB |
Output is correct |
69 |
Correct |
27 ms |
10264 KB |
Output is correct |
70 |
Correct |
2 ms |
856 KB |
Output is correct |
71 |
Correct |
38 ms |
10068 KB |
Output is correct |
72 |
Correct |
47 ms |
12112 KB |
Output is correct |
73 |
Correct |
0 ms |
348 KB |
Output is correct |
74 |
Correct |
47 ms |
16916 KB |
Output is correct |
75 |
Correct |
41 ms |
13672 KB |
Output is correct |
76 |
Correct |
1 ms |
608 KB |
Output is correct |
77 |
Correct |
1 ms |
604 KB |
Output is correct |
78 |
Correct |
29 ms |
10452 KB |
Output is correct |
79 |
Correct |
32 ms |
11484 KB |
Output is correct |
80 |
Correct |
1 ms |
600 KB |
Output is correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
0 ms |
452 KB |
Output is correct |
83 |
Correct |
0 ms |
348 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
40 ms |
10836 KB |
Output is correct |
86 |
Correct |
2 ms |
604 KB |
Output is correct |
87 |
Correct |
36 ms |
10264 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
41 ms |
10084 KB |
Output is correct |
90 |
Correct |
41 ms |
10836 KB |
Output is correct |
91 |
Correct |
39 ms |
11092 KB |
Output is correct |
92 |
Correct |
33 ms |
10324 KB |
Output is correct |
93 |
Correct |
1 ms |
600 KB |
Output is correct |
94 |
Correct |
0 ms |
348 KB |
Output is correct |
95 |
Correct |
28 ms |
11412 KB |
Output is correct |
96 |
Correct |
45 ms |
14932 KB |
Output is correct |
97 |
Correct |
1 ms |
600 KB |
Output is correct |
98 |
Correct |
0 ms |
348 KB |
Output is correct |
99 |
Correct |
27 ms |
10964 KB |
Output is correct |
100 |
Correct |
1 ms |
600 KB |
Output is correct |
101 |
Correct |
40 ms |
10268 KB |
Output is correct |
102 |
Correct |
47 ms |
13652 KB |
Output is correct |
103 |
Correct |
1 ms |
604 KB |
Output is correct |
104 |
Correct |
1 ms |
760 KB |
Output is correct |
105 |
Correct |
1 ms |
600 KB |
Output is correct |
106 |
Correct |
1 ms |
604 KB |
Output is correct |
107 |
Correct |
1 ms |
600 KB |
Output is correct |
108 |
Correct |
198 ms |
54980 KB |
Output is correct |
109 |
Correct |
494 ms |
66384 KB |
Output is correct |
110 |
Correct |
500 ms |
76772 KB |
Output is correct |
111 |
Correct |
567 ms |
77400 KB |
Output is correct |
112 |
Correct |
477 ms |
68328 KB |
Output is correct |
113 |
Correct |
218 ms |
56888 KB |
Output is correct |
114 |
Correct |
360 ms |
49568 KB |
Output is correct |
115 |
Correct |
374 ms |
49492 KB |
Output is correct |
116 |
Correct |
385 ms |
52296 KB |
Output is correct |
117 |
Correct |
356 ms |
56152 KB |
Output is correct |
118 |
Correct |
295 ms |
49616 KB |
Output is correct |
119 |
Correct |
355 ms |
56036 KB |
Output is correct |
120 |
Correct |
400 ms |
68784 KB |
Output is correct |
121 |
Correct |
363 ms |
55888 KB |
Output is correct |
122 |
Correct |
368 ms |
54172 KB |
Output is correct |
123 |
Correct |
229 ms |
57544 KB |
Output is correct |
124 |
Correct |
484 ms |
54584 KB |
Output is correct |