# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
604445 | 2022-07-25T06:37:37 Z | temporary_juggernaut | Mergers (JOI19_mergers) | C++14 | 1696 ms | 176640 KB |
#include<bits/stdc++.h> #define fr first #define sc second using namespace std; typedef long long ll; typedef long double ld; template<class T>void umax(T &a,T b){if(a<b)a=b;} template<class T>void umin(T &a,T b){if(b<a)a=b;} #ifdef juggernaut #define printl(args...) printf(args) #else #define printl(args...) 0 #endif int n,k; vector<int>g[500005]; int sz[500005]; int gp[500005]; set<int>st[500005]; int pivot[500005]; int sub[500005]; void add(int a,int b){ if(st[a].size()<st[b].size()){ swap(st[a],st[b]); swap(pivot[a],pivot[b]); } for(int to:st[b]){ if(!st[a].count(to)){ st[a].insert(to); pivot[a]+=sz[to]; } } st[b].clear(); } int tot; int sweet[500005]; map<int,int>mp[500005]; void dfs(int v,int p){ pivot[v]=sz[gp[v]]; sub[v]=1; st[v].insert(gp[v]); for(int to:g[v])if(to^p){ dfs(to,v); if(sub[to]==pivot[to]){ mp[v][to]=true; sweet[v]++; tot++; } sweet[v]+=sweet[to]; add(v,to); sub[v]+=sub[to]; } } int ans; void go(int v,int p){ for(int to:g[v])if(to^p){ go(to,v); if(mp[v][to]){ if(sweet[to]>0&&tot-sweet[to]-1>0){ ans--; } } } } int main(){ scanf("%d%d",&n,&k); for(int i=1;i^n;i++){ int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } for(int i=1;i<=n;i++){ scanf("%d",&gp[i]); sz[gp[i]]++; } dfs(1,1); ans=tot; go(1,1); cout<<(ans+1>>1); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 26 ms | 58980 KB | Output is correct |
2 | Correct | 27 ms | 58964 KB | Output is correct |
3 | Correct | 28 ms | 59016 KB | Output is correct |
4 | Correct | 27 ms | 59012 KB | Output is correct |
5 | Correct | 27 ms | 59000 KB | Output is correct |
6 | Correct | 28 ms | 58952 KB | Output is correct |
7 | Correct | 29 ms | 58956 KB | Output is correct |
8 | Correct | 27 ms | 58964 KB | Output is correct |
9 | Correct | 30 ms | 59016 KB | Output is correct |
10 | Correct | 27 ms | 58948 KB | Output is correct |
11 | Correct | 31 ms | 58996 KB | Output is correct |
12 | Correct | 28 ms | 58988 KB | Output is correct |
13 | Correct | 26 ms | 58968 KB | Output is correct |
14 | Correct | 33 ms | 59036 KB | Output is correct |
15 | Correct | 32 ms | 59048 KB | Output is correct |
16 | Correct | 30 ms | 58936 KB | Output is correct |
17 | Correct | 28 ms | 58952 KB | Output is correct |
18 | Correct | 26 ms | 58964 KB | Output is correct |
19 | Correct | 26 ms | 58964 KB | Output is correct |
20 | Correct | 27 ms | 59020 KB | Output is correct |
21 | Correct | 26 ms | 58964 KB | Output is correct |
22 | Correct | 26 ms | 59004 KB | Output is correct |
23 | Correct | 27 ms | 58956 KB | Output is correct |
24 | Correct | 30 ms | 58952 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 26 ms | 58980 KB | Output is correct |
2 | Correct | 27 ms | 58964 KB | Output is correct |
3 | Correct | 28 ms | 59016 KB | Output is correct |
4 | Correct | 27 ms | 59012 KB | Output is correct |
5 | Correct | 27 ms | 59000 KB | Output is correct |
6 | Correct | 28 ms | 58952 KB | Output is correct |
7 | Correct | 29 ms | 58956 KB | Output is correct |
8 | Correct | 27 ms | 58964 KB | Output is correct |
9 | Correct | 30 ms | 59016 KB | Output is correct |
10 | Correct | 27 ms | 58948 KB | Output is correct |
11 | Correct | 31 ms | 58996 KB | Output is correct |
12 | Correct | 28 ms | 58988 KB | Output is correct |
13 | Correct | 26 ms | 58968 KB | Output is correct |
14 | Correct | 33 ms | 59036 KB | Output is correct |
15 | Correct | 32 ms | 59048 KB | Output is correct |
16 | Correct | 30 ms | 58936 KB | Output is correct |
17 | Correct | 28 ms | 58952 KB | Output is correct |
18 | Correct | 26 ms | 58964 KB | Output is correct |
19 | Correct | 26 ms | 58964 KB | Output is correct |
20 | Correct | 27 ms | 59020 KB | Output is correct |
21 | Correct | 26 ms | 58964 KB | Output is correct |
22 | Correct | 26 ms | 59004 KB | Output is correct |
23 | Correct | 27 ms | 58956 KB | Output is correct |
24 | Correct | 30 ms | 58952 KB | Output is correct |
25 | Correct | 33 ms | 58964 KB | Output is correct |
26 | Correct | 33 ms | 59388 KB | Output is correct |
27 | Correct | 29 ms | 59348 KB | Output is correct |
28 | Correct | 29 ms | 59568 KB | Output is correct |
29 | Correct | 30 ms | 59476 KB | Output is correct |
30 | Correct | 31 ms | 59248 KB | Output is correct |
31 | Correct | 28 ms | 59036 KB | Output is correct |
32 | Correct | 30 ms | 59704 KB | Output is correct |
33 | Correct | 31 ms | 59048 KB | Output is correct |
34 | Correct | 28 ms | 59348 KB | Output is correct |
35 | Correct | 34 ms | 59508 KB | Output is correct |
36 | Correct | 29 ms | 59220 KB | Output is correct |
37 | Correct | 29 ms | 59400 KB | Output is correct |
38 | Correct | 27 ms | 58944 KB | Output is correct |
39 | Correct | 30 ms | 59348 KB | Output is correct |
40 | Correct | 32 ms | 59296 KB | Output is correct |
41 | Correct | 34 ms | 59276 KB | Output is correct |
42 | Correct | 30 ms | 59320 KB | Output is correct |
43 | Correct | 30 ms | 59656 KB | Output is correct |
44 | Correct | 27 ms | 58968 KB | Output is correct |
45 | Correct | 30 ms | 59520 KB | Output is correct |
46 | Correct | 29 ms | 59376 KB | Output is correct |
47 | Correct | 26 ms | 59012 KB | Output is correct |
48 | Correct | 28 ms | 59364 KB | Output is correct |
49 | Correct | 29 ms | 59476 KB | Output is correct |
50 | Correct | 29 ms | 59608 KB | Output is correct |
51 | Correct | 30 ms | 59348 KB | Output is correct |
52 | Correct | 32 ms | 59320 KB | Output is correct |
53 | Correct | 31 ms | 59368 KB | Output is correct |
54 | Correct | 28 ms | 59236 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 26 ms | 58980 KB | Output is correct |
2 | Correct | 27 ms | 58964 KB | Output is correct |
3 | Correct | 28 ms | 59016 KB | Output is correct |
4 | Correct | 27 ms | 59012 KB | Output is correct |
5 | Correct | 27 ms | 59000 KB | Output is correct |
6 | Correct | 28 ms | 58952 KB | Output is correct |
7 | Correct | 29 ms | 58956 KB | Output is correct |
8 | Correct | 27 ms | 58964 KB | Output is correct |
9 | Correct | 30 ms | 59016 KB | Output is correct |
10 | Correct | 27 ms | 58948 KB | Output is correct |
11 | Correct | 31 ms | 58996 KB | Output is correct |
12 | Correct | 28 ms | 58988 KB | Output is correct |
13 | Correct | 26 ms | 58968 KB | Output is correct |
14 | Correct | 33 ms | 59036 KB | Output is correct |
15 | Correct | 32 ms | 59048 KB | Output is correct |
16 | Correct | 30 ms | 58936 KB | Output is correct |
17 | Correct | 28 ms | 58952 KB | Output is correct |
18 | Correct | 26 ms | 58964 KB | Output is correct |
19 | Correct | 26 ms | 58964 KB | Output is correct |
20 | Correct | 27 ms | 59020 KB | Output is correct |
21 | Correct | 26 ms | 58964 KB | Output is correct |
22 | Correct | 26 ms | 59004 KB | Output is correct |
23 | Correct | 27 ms | 58956 KB | Output is correct |
24 | Correct | 30 ms | 58952 KB | Output is correct |
25 | Correct | 35 ms | 58976 KB | Output is correct |
26 | Correct | 118 ms | 68336 KB | Output is correct |
27 | Correct | 124 ms | 68536 KB | Output is correct |
28 | Correct | 28 ms | 59348 KB | Output is correct |
29 | Correct | 27 ms | 59044 KB | Output is correct |
30 | Correct | 30 ms | 59024 KB | Output is correct |
31 | Correct | 121 ms | 68560 KB | Output is correct |
32 | Correct | 28 ms | 59280 KB | Output is correct |
33 | Correct | 157 ms | 79992 KB | Output is correct |
34 | Correct | 130 ms | 68532 KB | Output is correct |
35 | Correct | 27 ms | 59220 KB | Output is correct |
36 | Correct | 125 ms | 69776 KB | Output is correct |
37 | Correct | 29 ms | 59288 KB | Output is correct |
38 | Correct | 33 ms | 59244 KB | Output is correct |
39 | Correct | 107 ms | 68408 KB | Output is correct |
40 | Correct | 28 ms | 59648 KB | Output is correct |
41 | Correct | 112 ms | 68492 KB | Output is correct |
42 | Correct | 163 ms | 72304 KB | Output is correct |
43 | Correct | 29 ms | 59016 KB | Output is correct |
44 | Correct | 128 ms | 80464 KB | Output is correct |
45 | Correct | 132 ms | 74460 KB | Output is correct |
46 | Correct | 28 ms | 59332 KB | Output is correct |
47 | Correct | 31 ms | 59308 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 113 ms | 68368 KB | Output is correct |
2 | Correct | 201 ms | 73512 KB | Output is correct |
3 | Correct | 42 ms | 59356 KB | Output is correct |
4 | Correct | 29 ms | 59232 KB | Output is correct |
5 | Correct | 26 ms | 58976 KB | Output is correct |
6 | Correct | 34 ms | 58972 KB | Output is correct |
7 | Correct | 30 ms | 59220 KB | Output is correct |
8 | Correct | 181 ms | 70984 KB | Output is correct |
9 | Correct | 29 ms | 59276 KB | Output is correct |
10 | Correct | 123 ms | 68588 KB | Output is correct |
11 | Correct | 30 ms | 58944 KB | Output is correct |
12 | Correct | 186 ms | 68484 KB | Output is correct |
13 | Correct | 230 ms | 71028 KB | Output is correct |
14 | Correct | 214 ms | 74520 KB | Output is correct |
15 | Correct | 121 ms | 68448 KB | Output is correct |
16 | Correct | 31 ms | 59348 KB | Output is correct |
17 | Correct | 29 ms | 58964 KB | Output is correct |
18 | Correct | 254 ms | 72900 KB | Output is correct |
19 | Correct | 192 ms | 82236 KB | Output is correct |
20 | Correct | 35 ms | 59532 KB | Output is correct |
21 | Correct | 30 ms | 59060 KB | Output is correct |
22 | Correct | 217 ms | 70880 KB | Output is correct |
23 | Correct | 29 ms | 59340 KB | Output is correct |
24 | Correct | 145 ms | 68948 KB | Output is correct |
25 | Correct | 260 ms | 79244 KB | Output is correct |
26 | Correct | 36 ms | 59412 KB | Output is correct |
27 | Correct | 32 ms | 59648 KB | Output is correct |
28 | Correct | 31 ms | 59248 KB | Output is correct |
29 | Correct | 30 ms | 59328 KB | Output is correct |
30 | Correct | 37 ms | 59384 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 26 ms | 58980 KB | Output is correct |
2 | Correct | 27 ms | 58964 KB | Output is correct |
3 | Correct | 28 ms | 59016 KB | Output is correct |
4 | Correct | 27 ms | 59012 KB | Output is correct |
5 | Correct | 27 ms | 59000 KB | Output is correct |
6 | Correct | 28 ms | 58952 KB | Output is correct |
7 | Correct | 29 ms | 58956 KB | Output is correct |
8 | Correct | 27 ms | 58964 KB | Output is correct |
9 | Correct | 30 ms | 59016 KB | Output is correct |
10 | Correct | 27 ms | 58948 KB | Output is correct |
11 | Correct | 31 ms | 58996 KB | Output is correct |
12 | Correct | 28 ms | 58988 KB | Output is correct |
13 | Correct | 26 ms | 58968 KB | Output is correct |
14 | Correct | 33 ms | 59036 KB | Output is correct |
15 | Correct | 32 ms | 59048 KB | Output is correct |
16 | Correct | 30 ms | 58936 KB | Output is correct |
17 | Correct | 28 ms | 58952 KB | Output is correct |
18 | Correct | 26 ms | 58964 KB | Output is correct |
19 | Correct | 26 ms | 58964 KB | Output is correct |
20 | Correct | 27 ms | 59020 KB | Output is correct |
21 | Correct | 26 ms | 58964 KB | Output is correct |
22 | Correct | 26 ms | 59004 KB | Output is correct |
23 | Correct | 27 ms | 58956 KB | Output is correct |
24 | Correct | 30 ms | 58952 KB | Output is correct |
25 | Correct | 33 ms | 58964 KB | Output is correct |
26 | Correct | 33 ms | 59388 KB | Output is correct |
27 | Correct | 29 ms | 59348 KB | Output is correct |
28 | Correct | 29 ms | 59568 KB | Output is correct |
29 | Correct | 30 ms | 59476 KB | Output is correct |
30 | Correct | 31 ms | 59248 KB | Output is correct |
31 | Correct | 28 ms | 59036 KB | Output is correct |
32 | Correct | 30 ms | 59704 KB | Output is correct |
33 | Correct | 31 ms | 59048 KB | Output is correct |
34 | Correct | 28 ms | 59348 KB | Output is correct |
35 | Correct | 34 ms | 59508 KB | Output is correct |
36 | Correct | 29 ms | 59220 KB | Output is correct |
37 | Correct | 29 ms | 59400 KB | Output is correct |
38 | Correct | 27 ms | 58944 KB | Output is correct |
39 | Correct | 30 ms | 59348 KB | Output is correct |
40 | Correct | 32 ms | 59296 KB | Output is correct |
41 | Correct | 34 ms | 59276 KB | Output is correct |
42 | Correct | 30 ms | 59320 KB | Output is correct |
43 | Correct | 30 ms | 59656 KB | Output is correct |
44 | Correct | 27 ms | 58968 KB | Output is correct |
45 | Correct | 30 ms | 59520 KB | Output is correct |
46 | Correct | 29 ms | 59376 KB | Output is correct |
47 | Correct | 26 ms | 59012 KB | Output is correct |
48 | Correct | 28 ms | 59364 KB | Output is correct |
49 | Correct | 29 ms | 59476 KB | Output is correct |
50 | Correct | 29 ms | 59608 KB | Output is correct |
51 | Correct | 30 ms | 59348 KB | Output is correct |
52 | Correct | 32 ms | 59320 KB | Output is correct |
53 | Correct | 31 ms | 59368 KB | Output is correct |
54 | Correct | 28 ms | 59236 KB | Output is correct |
55 | Correct | 35 ms | 58976 KB | Output is correct |
56 | Correct | 118 ms | 68336 KB | Output is correct |
57 | Correct | 124 ms | 68536 KB | Output is correct |
58 | Correct | 28 ms | 59348 KB | Output is correct |
59 | Correct | 27 ms | 59044 KB | Output is correct |
60 | Correct | 30 ms | 59024 KB | Output is correct |
61 | Correct | 121 ms | 68560 KB | Output is correct |
62 | Correct | 28 ms | 59280 KB | Output is correct |
63 | Correct | 157 ms | 79992 KB | Output is correct |
64 | Correct | 130 ms | 68532 KB | Output is correct |
65 | Correct | 27 ms | 59220 KB | Output is correct |
66 | Correct | 125 ms | 69776 KB | Output is correct |
67 | Correct | 29 ms | 59288 KB | Output is correct |
68 | Correct | 33 ms | 59244 KB | Output is correct |
69 | Correct | 107 ms | 68408 KB | Output is correct |
70 | Correct | 28 ms | 59648 KB | Output is correct |
71 | Correct | 112 ms | 68492 KB | Output is correct |
72 | Correct | 163 ms | 72304 KB | Output is correct |
73 | Correct | 29 ms | 59016 KB | Output is correct |
74 | Correct | 128 ms | 80464 KB | Output is correct |
75 | Correct | 132 ms | 74460 KB | Output is correct |
76 | Correct | 28 ms | 59332 KB | Output is correct |
77 | Correct | 31 ms | 59308 KB | Output is correct |
78 | Correct | 113 ms | 68368 KB | Output is correct |
79 | Correct | 201 ms | 73512 KB | Output is correct |
80 | Correct | 42 ms | 59356 KB | Output is correct |
81 | Correct | 29 ms | 59232 KB | Output is correct |
82 | Correct | 26 ms | 58976 KB | Output is correct |
83 | Correct | 34 ms | 58972 KB | Output is correct |
84 | Correct | 30 ms | 59220 KB | Output is correct |
85 | Correct | 181 ms | 70984 KB | Output is correct |
86 | Correct | 29 ms | 59276 KB | Output is correct |
87 | Correct | 123 ms | 68588 KB | Output is correct |
88 | Correct | 30 ms | 58944 KB | Output is correct |
89 | Correct | 186 ms | 68484 KB | Output is correct |
90 | Correct | 230 ms | 71028 KB | Output is correct |
91 | Correct | 214 ms | 74520 KB | Output is correct |
92 | Correct | 121 ms | 68448 KB | Output is correct |
93 | Correct | 31 ms | 59348 KB | Output is correct |
94 | Correct | 29 ms | 58964 KB | Output is correct |
95 | Correct | 254 ms | 72900 KB | Output is correct |
96 | Correct | 192 ms | 82236 KB | Output is correct |
97 | Correct | 35 ms | 59532 KB | Output is correct |
98 | Correct | 30 ms | 59060 KB | Output is correct |
99 | Correct | 217 ms | 70880 KB | Output is correct |
100 | Correct | 29 ms | 59340 KB | Output is correct |
101 | Correct | 145 ms | 68948 KB | Output is correct |
102 | Correct | 260 ms | 79244 KB | Output is correct |
103 | Correct | 36 ms | 59412 KB | Output is correct |
104 | Correct | 32 ms | 59648 KB | Output is correct |
105 | Correct | 31 ms | 59248 KB | Output is correct |
106 | Correct | 30 ms | 59328 KB | Output is correct |
107 | Correct | 37 ms | 59384 KB | Output is correct |
108 | Correct | 1173 ms | 116116 KB | Output is correct |
109 | Correct | 900 ms | 135672 KB | Output is correct |
110 | Correct | 879 ms | 153700 KB | Output is correct |
111 | Correct | 1397 ms | 176640 KB | Output is correct |
112 | Correct | 1349 ms | 153620 KB | Output is correct |
113 | Correct | 1507 ms | 128780 KB | Output is correct |
114 | Correct | 680 ms | 106600 KB | Output is correct |
115 | Correct | 694 ms | 106496 KB | Output is correct |
116 | Correct | 966 ms | 111600 KB | Output is correct |
117 | Correct | 1372 ms | 133920 KB | Output is correct |
118 | Correct | 810 ms | 105632 KB | Output is correct |
119 | Correct | 1367 ms | 132280 KB | Output is correct |
120 | Correct | 1367 ms | 160644 KB | Output is correct |
121 | Correct | 1373 ms | 132712 KB | Output is correct |
122 | Correct | 1274 ms | 121676 KB | Output is correct |
123 | Correct | 1696 ms | 131456 KB | Output is correct |
124 | Correct | 1272 ms | 130536 KB | Output is correct |