# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
799782 |
2023-08-01T02:21:43 Z |
Alish |
Mergers (JOI19_mergers) |
C++14 |
|
911 ms |
136088 KB |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define Mp make_pair
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
ll mod = 998244353;
ll power(ll a, ll b)
{
return (!b ? 1 : (b & 1 ? a * power(a * a % mod, b / 2) % mod : power(a * a % mod, b / 2) % mod));
}
const int N = 5e5+23, L =23 ;
vector<int>g[N], buc[N];
int st[N], h[N], par[N][L], sz[N], sum[N], is[N];
int ans;
int n , k ;
int a[N];
int tim;
void dfs(int v, int p=0){
sz[v]=1;
st[v]=++tim;
for (int u: g[v]) {
if(u==p)continue;
h[u]=h[v]+1;
par[u][0]=v;
dfs(u,v);
sz[v]+=sz[u];
}
}
void DFS(int v, int p=0){
int temp=0;
for (int u: g[v]){
if(u==p) continue;
DFS(u,v);
is[v]+=is[u];
sum[v]+=sum[u];
}
if(sum[v]==sz[v]){
if(is[v]==0 && v!=1)ans++;
if(is[v]==1 && v==1) ans++;
is[v]=1;
}
}
int LCA(int v, int u){
if(h[v]>h[u])swap(u,v);
int d =h[u]-h[v];
for (int i=L-1; i>=0; i--) {
if(d>>i&1) u=par[u][i];
}
if(u==v) return v;
for (int i=L-1; i>=0; i--){
if(par[u][i]!=par[v][i]){
v=par[v][i];
u=par[u][i];
}
}
return par[v][0];
}
int main()
{
fast_io
cin>>n>>k;
for (int i=0; i<n-1; i++){
int u , v; cin>>v>>u;
g[v].pb(u);
g[u].pb(v);
}
for (int i=1; i<=n; i++){
cin>>a[i];
buc[a[i]].pb(i);
}
dfs(1);
for (int j=1; j<L; j++){
for (int i=1; i<=n; i++){
par[i][j]=par[par[i][j-1]][j-1];
}
}
for (int w=1; w<=k; w++){
vector<pii> vec;
for (int i: buc[w]) vec.pb({st[i], i});
sort(all(vec));
int lca=LCA(vec[0].S, vec.back().S);
sum[lca]+=buc[w].size();
}
DFS(1);
//cout<<ans<<" ";
cout<<(ans+1)/2<<endl;
}
Compilation message
mergers.cpp: In function 'void DFS(int, int)':
mergers.cpp:53:9: warning: unused variable 'temp' [-Wunused-variable]
53 | int temp=0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23740 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23820 KB |
Output is correct |
9 |
Correct |
11 ms |
23804 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23876 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
11 ms |
23792 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
12 ms |
23740 KB |
Output is correct |
17 |
Correct |
12 ms |
23836 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23816 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23740 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23820 KB |
Output is correct |
9 |
Correct |
11 ms |
23804 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23876 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
11 ms |
23792 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
12 ms |
23740 KB |
Output is correct |
17 |
Correct |
12 ms |
23836 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23816 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
10 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
24300 KB |
Output is correct |
27 |
Correct |
12 ms |
24276 KB |
Output is correct |
28 |
Correct |
13 ms |
24468 KB |
Output is correct |
29 |
Correct |
13 ms |
24532 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
13 ms |
23764 KB |
Output is correct |
32 |
Correct |
13 ms |
24600 KB |
Output is correct |
33 |
Correct |
12 ms |
23776 KB |
Output is correct |
34 |
Correct |
12 ms |
24276 KB |
Output is correct |
35 |
Correct |
13 ms |
24404 KB |
Output is correct |
36 |
Correct |
12 ms |
24276 KB |
Output is correct |
37 |
Correct |
13 ms |
24276 KB |
Output is correct |
38 |
Correct |
11 ms |
23764 KB |
Output is correct |
39 |
Correct |
13 ms |
24372 KB |
Output is correct |
40 |
Correct |
12 ms |
24404 KB |
Output is correct |
41 |
Correct |
13 ms |
24276 KB |
Output is correct |
42 |
Correct |
13 ms |
24372 KB |
Output is correct |
43 |
Correct |
14 ms |
24532 KB |
Output is correct |
44 |
Correct |
12 ms |
23812 KB |
Output is correct |
45 |
Correct |
13 ms |
24404 KB |
Output is correct |
46 |
Correct |
14 ms |
24276 KB |
Output is correct |
47 |
Correct |
11 ms |
23764 KB |
Output is correct |
48 |
Correct |
13 ms |
24340 KB |
Output is correct |
49 |
Correct |
13 ms |
24404 KB |
Output is correct |
50 |
Correct |
13 ms |
24472 KB |
Output is correct |
51 |
Correct |
14 ms |
24296 KB |
Output is correct |
52 |
Correct |
12 ms |
24276 KB |
Output is correct |
53 |
Correct |
13 ms |
24304 KB |
Output is correct |
54 |
Correct |
12 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23740 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23820 KB |
Output is correct |
9 |
Correct |
11 ms |
23804 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23876 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
11 ms |
23792 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
12 ms |
23740 KB |
Output is correct |
17 |
Correct |
12 ms |
23836 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23816 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
10 ms |
23764 KB |
Output is correct |
26 |
Correct |
55 ms |
39236 KB |
Output is correct |
27 |
Correct |
66 ms |
39740 KB |
Output is correct |
28 |
Correct |
14 ms |
24276 KB |
Output is correct |
29 |
Correct |
13 ms |
23812 KB |
Output is correct |
30 |
Correct |
14 ms |
23816 KB |
Output is correct |
31 |
Correct |
71 ms |
39708 KB |
Output is correct |
32 |
Correct |
15 ms |
24332 KB |
Output is correct |
33 |
Correct |
103 ms |
46880 KB |
Output is correct |
34 |
Correct |
66 ms |
39928 KB |
Output is correct |
35 |
Correct |
13 ms |
24292 KB |
Output is correct |
36 |
Correct |
76 ms |
40408 KB |
Output is correct |
37 |
Correct |
14 ms |
24304 KB |
Output is correct |
38 |
Correct |
14 ms |
24352 KB |
Output is correct |
39 |
Correct |
66 ms |
39240 KB |
Output is correct |
40 |
Correct |
16 ms |
24588 KB |
Output is correct |
41 |
Correct |
71 ms |
39940 KB |
Output is correct |
42 |
Correct |
83 ms |
42072 KB |
Output is correct |
43 |
Correct |
12 ms |
23832 KB |
Output is correct |
44 |
Correct |
84 ms |
47392 KB |
Output is correct |
45 |
Correct |
80 ms |
43432 KB |
Output is correct |
46 |
Correct |
13 ms |
24276 KB |
Output is correct |
47 |
Correct |
13 ms |
24356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
38560 KB |
Output is correct |
2 |
Correct |
90 ms |
41988 KB |
Output is correct |
3 |
Correct |
14 ms |
24276 KB |
Output is correct |
4 |
Correct |
14 ms |
24276 KB |
Output is correct |
5 |
Correct |
12 ms |
23796 KB |
Output is correct |
6 |
Correct |
11 ms |
23860 KB |
Output is correct |
7 |
Correct |
13 ms |
24276 KB |
Output is correct |
8 |
Correct |
77 ms |
40764 KB |
Output is correct |
9 |
Correct |
12 ms |
24316 KB |
Output is correct |
10 |
Correct |
64 ms |
39796 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
71 ms |
39708 KB |
Output is correct |
13 |
Correct |
82 ms |
40700 KB |
Output is correct |
14 |
Correct |
82 ms |
42252 KB |
Output is correct |
15 |
Correct |
70 ms |
39276 KB |
Output is correct |
16 |
Correct |
13 ms |
24340 KB |
Output is correct |
17 |
Correct |
12 ms |
23764 KB |
Output is correct |
18 |
Correct |
72 ms |
42296 KB |
Output is correct |
19 |
Correct |
93 ms |
46372 KB |
Output is correct |
20 |
Correct |
14 ms |
24288 KB |
Output is correct |
21 |
Correct |
24 ms |
23828 KB |
Output is correct |
22 |
Correct |
82 ms |
41060 KB |
Output is correct |
23 |
Correct |
14 ms |
24360 KB |
Output is correct |
24 |
Correct |
77 ms |
40016 KB |
Output is correct |
25 |
Correct |
84 ms |
44868 KB |
Output is correct |
26 |
Correct |
13 ms |
24404 KB |
Output is correct |
27 |
Correct |
12 ms |
24404 KB |
Output is correct |
28 |
Correct |
13 ms |
24336 KB |
Output is correct |
29 |
Correct |
13 ms |
24340 KB |
Output is correct |
30 |
Correct |
13 ms |
24336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23804 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23820 KB |
Output is correct |
4 |
Correct |
11 ms |
23740 KB |
Output is correct |
5 |
Correct |
12 ms |
23816 KB |
Output is correct |
6 |
Correct |
13 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23812 KB |
Output is correct |
8 |
Correct |
12 ms |
23820 KB |
Output is correct |
9 |
Correct |
11 ms |
23804 KB |
Output is correct |
10 |
Correct |
12 ms |
23764 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23876 KB |
Output is correct |
13 |
Correct |
11 ms |
23816 KB |
Output is correct |
14 |
Correct |
11 ms |
23792 KB |
Output is correct |
15 |
Correct |
12 ms |
23816 KB |
Output is correct |
16 |
Correct |
12 ms |
23740 KB |
Output is correct |
17 |
Correct |
12 ms |
23836 KB |
Output is correct |
18 |
Correct |
12 ms |
23764 KB |
Output is correct |
19 |
Correct |
12 ms |
23764 KB |
Output is correct |
20 |
Correct |
12 ms |
23816 KB |
Output is correct |
21 |
Correct |
12 ms |
23764 KB |
Output is correct |
22 |
Correct |
14 ms |
23892 KB |
Output is correct |
23 |
Correct |
13 ms |
23844 KB |
Output is correct |
24 |
Correct |
12 ms |
23812 KB |
Output is correct |
25 |
Correct |
10 ms |
23764 KB |
Output is correct |
26 |
Correct |
13 ms |
24300 KB |
Output is correct |
27 |
Correct |
12 ms |
24276 KB |
Output is correct |
28 |
Correct |
13 ms |
24468 KB |
Output is correct |
29 |
Correct |
13 ms |
24532 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
13 ms |
23764 KB |
Output is correct |
32 |
Correct |
13 ms |
24600 KB |
Output is correct |
33 |
Correct |
12 ms |
23776 KB |
Output is correct |
34 |
Correct |
12 ms |
24276 KB |
Output is correct |
35 |
Correct |
13 ms |
24404 KB |
Output is correct |
36 |
Correct |
12 ms |
24276 KB |
Output is correct |
37 |
Correct |
13 ms |
24276 KB |
Output is correct |
38 |
Correct |
11 ms |
23764 KB |
Output is correct |
39 |
Correct |
13 ms |
24372 KB |
Output is correct |
40 |
Correct |
12 ms |
24404 KB |
Output is correct |
41 |
Correct |
13 ms |
24276 KB |
Output is correct |
42 |
Correct |
13 ms |
24372 KB |
Output is correct |
43 |
Correct |
14 ms |
24532 KB |
Output is correct |
44 |
Correct |
12 ms |
23812 KB |
Output is correct |
45 |
Correct |
13 ms |
24404 KB |
Output is correct |
46 |
Correct |
14 ms |
24276 KB |
Output is correct |
47 |
Correct |
11 ms |
23764 KB |
Output is correct |
48 |
Correct |
13 ms |
24340 KB |
Output is correct |
49 |
Correct |
13 ms |
24404 KB |
Output is correct |
50 |
Correct |
13 ms |
24472 KB |
Output is correct |
51 |
Correct |
14 ms |
24296 KB |
Output is correct |
52 |
Correct |
12 ms |
24276 KB |
Output is correct |
53 |
Correct |
13 ms |
24304 KB |
Output is correct |
54 |
Correct |
12 ms |
24276 KB |
Output is correct |
55 |
Correct |
10 ms |
23764 KB |
Output is correct |
56 |
Correct |
55 ms |
39236 KB |
Output is correct |
57 |
Correct |
66 ms |
39740 KB |
Output is correct |
58 |
Correct |
14 ms |
24276 KB |
Output is correct |
59 |
Correct |
13 ms |
23812 KB |
Output is correct |
60 |
Correct |
14 ms |
23816 KB |
Output is correct |
61 |
Correct |
71 ms |
39708 KB |
Output is correct |
62 |
Correct |
15 ms |
24332 KB |
Output is correct |
63 |
Correct |
103 ms |
46880 KB |
Output is correct |
64 |
Correct |
66 ms |
39928 KB |
Output is correct |
65 |
Correct |
13 ms |
24292 KB |
Output is correct |
66 |
Correct |
76 ms |
40408 KB |
Output is correct |
67 |
Correct |
14 ms |
24304 KB |
Output is correct |
68 |
Correct |
14 ms |
24352 KB |
Output is correct |
69 |
Correct |
66 ms |
39240 KB |
Output is correct |
70 |
Correct |
16 ms |
24588 KB |
Output is correct |
71 |
Correct |
71 ms |
39940 KB |
Output is correct |
72 |
Correct |
83 ms |
42072 KB |
Output is correct |
73 |
Correct |
12 ms |
23832 KB |
Output is correct |
74 |
Correct |
84 ms |
47392 KB |
Output is correct |
75 |
Correct |
80 ms |
43432 KB |
Output is correct |
76 |
Correct |
13 ms |
24276 KB |
Output is correct |
77 |
Correct |
13 ms |
24356 KB |
Output is correct |
78 |
Correct |
53 ms |
38560 KB |
Output is correct |
79 |
Correct |
90 ms |
41988 KB |
Output is correct |
80 |
Correct |
14 ms |
24276 KB |
Output is correct |
81 |
Correct |
14 ms |
24276 KB |
Output is correct |
82 |
Correct |
12 ms |
23796 KB |
Output is correct |
83 |
Correct |
11 ms |
23860 KB |
Output is correct |
84 |
Correct |
13 ms |
24276 KB |
Output is correct |
85 |
Correct |
77 ms |
40764 KB |
Output is correct |
86 |
Correct |
12 ms |
24316 KB |
Output is correct |
87 |
Correct |
64 ms |
39796 KB |
Output is correct |
88 |
Correct |
12 ms |
23764 KB |
Output is correct |
89 |
Correct |
71 ms |
39708 KB |
Output is correct |
90 |
Correct |
82 ms |
40700 KB |
Output is correct |
91 |
Correct |
82 ms |
42252 KB |
Output is correct |
92 |
Correct |
70 ms |
39276 KB |
Output is correct |
93 |
Correct |
13 ms |
24340 KB |
Output is correct |
94 |
Correct |
12 ms |
23764 KB |
Output is correct |
95 |
Correct |
72 ms |
42296 KB |
Output is correct |
96 |
Correct |
93 ms |
46372 KB |
Output is correct |
97 |
Correct |
14 ms |
24288 KB |
Output is correct |
98 |
Correct |
24 ms |
23828 KB |
Output is correct |
99 |
Correct |
82 ms |
41060 KB |
Output is correct |
100 |
Correct |
14 ms |
24360 KB |
Output is correct |
101 |
Correct |
77 ms |
40016 KB |
Output is correct |
102 |
Correct |
84 ms |
44868 KB |
Output is correct |
103 |
Correct |
13 ms |
24404 KB |
Output is correct |
104 |
Correct |
12 ms |
24404 KB |
Output is correct |
105 |
Correct |
13 ms |
24336 KB |
Output is correct |
106 |
Correct |
13 ms |
24340 KB |
Output is correct |
107 |
Correct |
13 ms |
24336 KB |
Output is correct |
108 |
Correct |
464 ms |
105292 KB |
Output is correct |
109 |
Correct |
724 ms |
117608 KB |
Output is correct |
110 |
Correct |
719 ms |
128972 KB |
Output is correct |
111 |
Correct |
911 ms |
136088 KB |
Output is correct |
112 |
Correct |
759 ms |
126432 KB |
Output is correct |
113 |
Correct |
515 ms |
112956 KB |
Output is correct |
114 |
Correct |
533 ms |
99584 KB |
Output is correct |
115 |
Correct |
518 ms |
99772 KB |
Output is correct |
116 |
Correct |
606 ms |
102408 KB |
Output is correct |
117 |
Correct |
633 ms |
112968 KB |
Output is correct |
118 |
Correct |
521 ms |
99140 KB |
Output is correct |
119 |
Correct |
661 ms |
113024 KB |
Output is correct |
120 |
Correct |
782 ms |
126832 KB |
Output is correct |
121 |
Correct |
672 ms |
112956 KB |
Output is correct |
122 |
Correct |
709 ms |
106700 KB |
Output is correct |
123 |
Correct |
457 ms |
114144 KB |
Output is correct |
124 |
Correct |
767 ms |
110920 KB |
Output is correct |