# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
564816 |
2022-05-19T17:49:28 Z |
urosk |
Mergers (JOI19_mergers) |
C++14 |
|
2796 ms |
250316 KB |
// __builtin_popcount(x)
// __builtin_popcountll(x)
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
using namespace std;
#define maxn 500005
ll n,k;
vector<ll> g[maxn];
vector<ll> v;
ll a[maxn];
ll in[maxn];
ll out[maxn];
ll dp[maxn][3];
ll b[maxn][3];
ll it = 1;
void dfs(ll u,ll par){
in[u] = it++;
for(ll s : g[u]){
if(s==par) continue;
dfs(s,u);
}
out[u] = it-1;
}
bool intree(ll x,ll i){
return x>=in[i]&&out[i]>=x;
}
bool bio[maxn];
ll ans = 0;
set<ll> rg[maxn];
ll dsu[maxn];
ll root(ll x){
while(x!=dsu[x]){
dsu[x] = dsu[dsu[x]];
x = dsu[x];
}
return x;
}
void upd(ll x,ll y){
x = root(x);
y = root(y);
dsu[x] = y;
}
map<pll,bool> mp;
void dfs2(ll u,ll par){
dp[u][1] = b[a[u]][1];
dp[u][2] = b[a[u]][2];
for(ll s : g[u]){
if(s==par) continue;
dfs2(s,u);
dp[u][1] = min(dp[u][1],dp[s][1]);
dp[u][2] = max(dp[u][2],dp[s][2]);
bio[u]|=bio[s];
}
bool da = 0;
if(u!=par){
if(intree(dp[u][1],u)&&intree(dp[u][2],u)) da = 1;
}
//cerr<<"u: "<<u<<" "<<in[u]<< " "<<out[u]<<" "<<dp[u][1]<< " "<<dp[u][2]<<endl;
if(da){
mp[{u,par}] = mp[{par,u}] = 1;
}
}
bool vis[maxn];
void dfs3(ll u,ll par){
vis[u] = 1;
for(ll s : g[u]){
if(s==par) continue;
if(mp[{s,u}]) continue;
upd(u,s);
dfs3(s,u);
}
}
vector<pll> e;
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> k;
iota(dsu+1,dsu+n+1,1);
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
e.pb({x,y});
}
for(ll i = 1;i<=n;i++) cin >> a[i];
dfs(1,1);
for(ll i = 1;i<=k;i++){
b[i][1] = llinf;
b[i][2] = 0;
}
for(ll i = 1;i<=n;i++){
ll col = a[i];
b[col][1] = min(b[col][1],in[i]);
b[col][2] = max(b[col][2],in[i]);
}
dfs2(1,1);
for(ll i = 1;i<=n;i++){
if(vis[i]) continue;
dfs3(i,i);
}
for(pll p : e){
ll x = p.fi;
ll y = p.sc;
x = root(x);
y = root(y);
if(x==y) continue;
rg[x].insert(y);
rg[y].insert(x);
}
for(ll i = 1;i<=n;i++) if(sz(rg[i])==1) ans++;
ans = (ans+1)/2;
cout<<ans<<endl;
return 0;
}
/*
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
5 5
1 2
2 3
3 4
4 5
1
2
3
4
5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
18 ms |
35604 KB |
Output is correct |
4 |
Correct |
16 ms |
35576 KB |
Output is correct |
5 |
Correct |
17 ms |
35508 KB |
Output is correct |
6 |
Correct |
16 ms |
35540 KB |
Output is correct |
7 |
Correct |
17 ms |
35600 KB |
Output is correct |
8 |
Correct |
17 ms |
35552 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35540 KB |
Output is correct |
11 |
Correct |
17 ms |
35612 KB |
Output is correct |
12 |
Correct |
18 ms |
35516 KB |
Output is correct |
13 |
Correct |
19 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
20 ms |
35536 KB |
Output is correct |
16 |
Correct |
16 ms |
35492 KB |
Output is correct |
17 |
Correct |
17 ms |
35560 KB |
Output is correct |
18 |
Correct |
16 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35564 KB |
Output is correct |
20 |
Correct |
16 ms |
35580 KB |
Output is correct |
21 |
Correct |
17 ms |
35560 KB |
Output is correct |
22 |
Correct |
17 ms |
35524 KB |
Output is correct |
23 |
Correct |
21 ms |
35564 KB |
Output is correct |
24 |
Correct |
18 ms |
35564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
18 ms |
35604 KB |
Output is correct |
4 |
Correct |
16 ms |
35576 KB |
Output is correct |
5 |
Correct |
17 ms |
35508 KB |
Output is correct |
6 |
Correct |
16 ms |
35540 KB |
Output is correct |
7 |
Correct |
17 ms |
35600 KB |
Output is correct |
8 |
Correct |
17 ms |
35552 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35540 KB |
Output is correct |
11 |
Correct |
17 ms |
35612 KB |
Output is correct |
12 |
Correct |
18 ms |
35516 KB |
Output is correct |
13 |
Correct |
19 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
20 ms |
35536 KB |
Output is correct |
16 |
Correct |
16 ms |
35492 KB |
Output is correct |
17 |
Correct |
17 ms |
35560 KB |
Output is correct |
18 |
Correct |
16 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35564 KB |
Output is correct |
20 |
Correct |
16 ms |
35580 KB |
Output is correct |
21 |
Correct |
17 ms |
35560 KB |
Output is correct |
22 |
Correct |
17 ms |
35524 KB |
Output is correct |
23 |
Correct |
21 ms |
35564 KB |
Output is correct |
24 |
Correct |
18 ms |
35564 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
20 ms |
36348 KB |
Output is correct |
27 |
Correct |
21 ms |
36176 KB |
Output is correct |
28 |
Correct |
22 ms |
36600 KB |
Output is correct |
29 |
Correct |
22 ms |
36636 KB |
Output is correct |
30 |
Correct |
19 ms |
36088 KB |
Output is correct |
31 |
Correct |
17 ms |
35620 KB |
Output is correct |
32 |
Correct |
24 ms |
36564 KB |
Output is correct |
33 |
Correct |
17 ms |
35540 KB |
Output is correct |
34 |
Correct |
19 ms |
36056 KB |
Output is correct |
35 |
Correct |
27 ms |
36576 KB |
Output is correct |
36 |
Correct |
25 ms |
36124 KB |
Output is correct |
37 |
Correct |
24 ms |
36184 KB |
Output is correct |
38 |
Correct |
17 ms |
35540 KB |
Output is correct |
39 |
Correct |
20 ms |
36140 KB |
Output is correct |
40 |
Correct |
19 ms |
36104 KB |
Output is correct |
41 |
Correct |
18 ms |
36076 KB |
Output is correct |
42 |
Correct |
19 ms |
36220 KB |
Output is correct |
43 |
Correct |
19 ms |
36436 KB |
Output is correct |
44 |
Correct |
19 ms |
35568 KB |
Output is correct |
45 |
Correct |
23 ms |
36308 KB |
Output is correct |
46 |
Correct |
21 ms |
36308 KB |
Output is correct |
47 |
Correct |
18 ms |
35540 KB |
Output is correct |
48 |
Correct |
19 ms |
36148 KB |
Output is correct |
49 |
Correct |
22 ms |
36672 KB |
Output is correct |
50 |
Correct |
22 ms |
36712 KB |
Output is correct |
51 |
Correct |
27 ms |
36160 KB |
Output is correct |
52 |
Correct |
21 ms |
36092 KB |
Output is correct |
53 |
Correct |
21 ms |
36276 KB |
Output is correct |
54 |
Correct |
20 ms |
36156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
18 ms |
35604 KB |
Output is correct |
4 |
Correct |
16 ms |
35576 KB |
Output is correct |
5 |
Correct |
17 ms |
35508 KB |
Output is correct |
6 |
Correct |
16 ms |
35540 KB |
Output is correct |
7 |
Correct |
17 ms |
35600 KB |
Output is correct |
8 |
Correct |
17 ms |
35552 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35540 KB |
Output is correct |
11 |
Correct |
17 ms |
35612 KB |
Output is correct |
12 |
Correct |
18 ms |
35516 KB |
Output is correct |
13 |
Correct |
19 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
20 ms |
35536 KB |
Output is correct |
16 |
Correct |
16 ms |
35492 KB |
Output is correct |
17 |
Correct |
17 ms |
35560 KB |
Output is correct |
18 |
Correct |
16 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35564 KB |
Output is correct |
20 |
Correct |
16 ms |
35580 KB |
Output is correct |
21 |
Correct |
17 ms |
35560 KB |
Output is correct |
22 |
Correct |
17 ms |
35524 KB |
Output is correct |
23 |
Correct |
21 ms |
35564 KB |
Output is correct |
24 |
Correct |
18 ms |
35564 KB |
Output is correct |
25 |
Correct |
18 ms |
35512 KB |
Output is correct |
26 |
Correct |
117 ms |
54284 KB |
Output is correct |
27 |
Correct |
171 ms |
54212 KB |
Output is correct |
28 |
Correct |
22 ms |
36308 KB |
Output is correct |
29 |
Correct |
18 ms |
35540 KB |
Output is correct |
30 |
Correct |
17 ms |
35540 KB |
Output is correct |
31 |
Correct |
145 ms |
54268 KB |
Output is correct |
32 |
Correct |
23 ms |
36052 KB |
Output is correct |
33 |
Correct |
202 ms |
65252 KB |
Output is correct |
34 |
Correct |
146 ms |
54164 KB |
Output is correct |
35 |
Correct |
19 ms |
36128 KB |
Output is correct |
36 |
Correct |
173 ms |
54928 KB |
Output is correct |
37 |
Correct |
20 ms |
36092 KB |
Output is correct |
38 |
Correct |
22 ms |
36036 KB |
Output is correct |
39 |
Correct |
125 ms |
54312 KB |
Output is correct |
40 |
Correct |
23 ms |
36496 KB |
Output is correct |
41 |
Correct |
135 ms |
54236 KB |
Output is correct |
42 |
Correct |
190 ms |
58220 KB |
Output is correct |
43 |
Correct |
18 ms |
35616 KB |
Output is correct |
44 |
Correct |
180 ms |
65608 KB |
Output is correct |
45 |
Correct |
162 ms |
60900 KB |
Output is correct |
46 |
Correct |
20 ms |
36184 KB |
Output is correct |
47 |
Correct |
19 ms |
36092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
52856 KB |
Output is correct |
2 |
Correct |
326 ms |
70808 KB |
Output is correct |
3 |
Correct |
21 ms |
36440 KB |
Output is correct |
4 |
Correct |
20 ms |
36084 KB |
Output is correct |
5 |
Correct |
17 ms |
35540 KB |
Output is correct |
6 |
Correct |
20 ms |
35552 KB |
Output is correct |
7 |
Correct |
23 ms |
36056 KB |
Output is correct |
8 |
Correct |
215 ms |
57384 KB |
Output is correct |
9 |
Correct |
22 ms |
36052 KB |
Output is correct |
10 |
Correct |
135 ms |
54524 KB |
Output is correct |
11 |
Correct |
17 ms |
35532 KB |
Output is correct |
12 |
Correct |
187 ms |
54048 KB |
Output is correct |
13 |
Correct |
180 ms |
58828 KB |
Output is correct |
14 |
Correct |
365 ms |
72444 KB |
Output is correct |
15 |
Correct |
111 ms |
54240 KB |
Output is correct |
16 |
Correct |
21 ms |
36304 KB |
Output is correct |
17 |
Correct |
17 ms |
35568 KB |
Output is correct |
18 |
Correct |
298 ms |
69344 KB |
Output is correct |
19 |
Correct |
336 ms |
78280 KB |
Output is correct |
20 |
Correct |
20 ms |
36160 KB |
Output is correct |
21 |
Correct |
18 ms |
35540 KB |
Output is correct |
22 |
Correct |
160 ms |
58604 KB |
Output is correct |
23 |
Correct |
22 ms |
36188 KB |
Output is correct |
24 |
Correct |
183 ms |
55900 KB |
Output is correct |
25 |
Correct |
335 ms |
77412 KB |
Output is correct |
26 |
Correct |
23 ms |
36636 KB |
Output is correct |
27 |
Correct |
24 ms |
36692 KB |
Output is correct |
28 |
Correct |
20 ms |
36152 KB |
Output is correct |
29 |
Correct |
19 ms |
36152 KB |
Output is correct |
30 |
Correct |
20 ms |
36264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
17 ms |
35540 KB |
Output is correct |
3 |
Correct |
18 ms |
35604 KB |
Output is correct |
4 |
Correct |
16 ms |
35576 KB |
Output is correct |
5 |
Correct |
17 ms |
35508 KB |
Output is correct |
6 |
Correct |
16 ms |
35540 KB |
Output is correct |
7 |
Correct |
17 ms |
35600 KB |
Output is correct |
8 |
Correct |
17 ms |
35552 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
18 ms |
35540 KB |
Output is correct |
11 |
Correct |
17 ms |
35612 KB |
Output is correct |
12 |
Correct |
18 ms |
35516 KB |
Output is correct |
13 |
Correct |
19 ms |
35540 KB |
Output is correct |
14 |
Correct |
18 ms |
35540 KB |
Output is correct |
15 |
Correct |
20 ms |
35536 KB |
Output is correct |
16 |
Correct |
16 ms |
35492 KB |
Output is correct |
17 |
Correct |
17 ms |
35560 KB |
Output is correct |
18 |
Correct |
16 ms |
35540 KB |
Output is correct |
19 |
Correct |
18 ms |
35564 KB |
Output is correct |
20 |
Correct |
16 ms |
35580 KB |
Output is correct |
21 |
Correct |
17 ms |
35560 KB |
Output is correct |
22 |
Correct |
17 ms |
35524 KB |
Output is correct |
23 |
Correct |
21 ms |
35564 KB |
Output is correct |
24 |
Correct |
18 ms |
35564 KB |
Output is correct |
25 |
Correct |
17 ms |
35540 KB |
Output is correct |
26 |
Correct |
20 ms |
36348 KB |
Output is correct |
27 |
Correct |
21 ms |
36176 KB |
Output is correct |
28 |
Correct |
22 ms |
36600 KB |
Output is correct |
29 |
Correct |
22 ms |
36636 KB |
Output is correct |
30 |
Correct |
19 ms |
36088 KB |
Output is correct |
31 |
Correct |
17 ms |
35620 KB |
Output is correct |
32 |
Correct |
24 ms |
36564 KB |
Output is correct |
33 |
Correct |
17 ms |
35540 KB |
Output is correct |
34 |
Correct |
19 ms |
36056 KB |
Output is correct |
35 |
Correct |
27 ms |
36576 KB |
Output is correct |
36 |
Correct |
25 ms |
36124 KB |
Output is correct |
37 |
Correct |
24 ms |
36184 KB |
Output is correct |
38 |
Correct |
17 ms |
35540 KB |
Output is correct |
39 |
Correct |
20 ms |
36140 KB |
Output is correct |
40 |
Correct |
19 ms |
36104 KB |
Output is correct |
41 |
Correct |
18 ms |
36076 KB |
Output is correct |
42 |
Correct |
19 ms |
36220 KB |
Output is correct |
43 |
Correct |
19 ms |
36436 KB |
Output is correct |
44 |
Correct |
19 ms |
35568 KB |
Output is correct |
45 |
Correct |
23 ms |
36308 KB |
Output is correct |
46 |
Correct |
21 ms |
36308 KB |
Output is correct |
47 |
Correct |
18 ms |
35540 KB |
Output is correct |
48 |
Correct |
19 ms |
36148 KB |
Output is correct |
49 |
Correct |
22 ms |
36672 KB |
Output is correct |
50 |
Correct |
22 ms |
36712 KB |
Output is correct |
51 |
Correct |
27 ms |
36160 KB |
Output is correct |
52 |
Correct |
21 ms |
36092 KB |
Output is correct |
53 |
Correct |
21 ms |
36276 KB |
Output is correct |
54 |
Correct |
20 ms |
36156 KB |
Output is correct |
55 |
Correct |
18 ms |
35512 KB |
Output is correct |
56 |
Correct |
117 ms |
54284 KB |
Output is correct |
57 |
Correct |
171 ms |
54212 KB |
Output is correct |
58 |
Correct |
22 ms |
36308 KB |
Output is correct |
59 |
Correct |
18 ms |
35540 KB |
Output is correct |
60 |
Correct |
17 ms |
35540 KB |
Output is correct |
61 |
Correct |
145 ms |
54268 KB |
Output is correct |
62 |
Correct |
23 ms |
36052 KB |
Output is correct |
63 |
Correct |
202 ms |
65252 KB |
Output is correct |
64 |
Correct |
146 ms |
54164 KB |
Output is correct |
65 |
Correct |
19 ms |
36128 KB |
Output is correct |
66 |
Correct |
173 ms |
54928 KB |
Output is correct |
67 |
Correct |
20 ms |
36092 KB |
Output is correct |
68 |
Correct |
22 ms |
36036 KB |
Output is correct |
69 |
Correct |
125 ms |
54312 KB |
Output is correct |
70 |
Correct |
23 ms |
36496 KB |
Output is correct |
71 |
Correct |
135 ms |
54236 KB |
Output is correct |
72 |
Correct |
190 ms |
58220 KB |
Output is correct |
73 |
Correct |
18 ms |
35616 KB |
Output is correct |
74 |
Correct |
180 ms |
65608 KB |
Output is correct |
75 |
Correct |
162 ms |
60900 KB |
Output is correct |
76 |
Correct |
20 ms |
36184 KB |
Output is correct |
77 |
Correct |
19 ms |
36092 KB |
Output is correct |
78 |
Correct |
112 ms |
52856 KB |
Output is correct |
79 |
Correct |
326 ms |
70808 KB |
Output is correct |
80 |
Correct |
21 ms |
36440 KB |
Output is correct |
81 |
Correct |
20 ms |
36084 KB |
Output is correct |
82 |
Correct |
17 ms |
35540 KB |
Output is correct |
83 |
Correct |
20 ms |
35552 KB |
Output is correct |
84 |
Correct |
23 ms |
36056 KB |
Output is correct |
85 |
Correct |
215 ms |
57384 KB |
Output is correct |
86 |
Correct |
22 ms |
36052 KB |
Output is correct |
87 |
Correct |
135 ms |
54524 KB |
Output is correct |
88 |
Correct |
17 ms |
35532 KB |
Output is correct |
89 |
Correct |
187 ms |
54048 KB |
Output is correct |
90 |
Correct |
180 ms |
58828 KB |
Output is correct |
91 |
Correct |
365 ms |
72444 KB |
Output is correct |
92 |
Correct |
111 ms |
54240 KB |
Output is correct |
93 |
Correct |
21 ms |
36304 KB |
Output is correct |
94 |
Correct |
17 ms |
35568 KB |
Output is correct |
95 |
Correct |
298 ms |
69344 KB |
Output is correct |
96 |
Correct |
336 ms |
78280 KB |
Output is correct |
97 |
Correct |
20 ms |
36160 KB |
Output is correct |
98 |
Correct |
18 ms |
35540 KB |
Output is correct |
99 |
Correct |
160 ms |
58604 KB |
Output is correct |
100 |
Correct |
22 ms |
36188 KB |
Output is correct |
101 |
Correct |
183 ms |
55900 KB |
Output is correct |
102 |
Correct |
335 ms |
77412 KB |
Output is correct |
103 |
Correct |
23 ms |
36636 KB |
Output is correct |
104 |
Correct |
24 ms |
36692 KB |
Output is correct |
105 |
Correct |
20 ms |
36152 KB |
Output is correct |
106 |
Correct |
19 ms |
36152 KB |
Output is correct |
107 |
Correct |
20 ms |
36264 KB |
Output is correct |
108 |
Correct |
947 ms |
143656 KB |
Output is correct |
109 |
Correct |
1284 ms |
155936 KB |
Output is correct |
110 |
Correct |
1226 ms |
173908 KB |
Output is correct |
111 |
Correct |
2606 ms |
250316 KB |
Output is correct |
112 |
Correct |
2267 ms |
210236 KB |
Output is correct |
113 |
Correct |
2301 ms |
205772 KB |
Output is correct |
114 |
Correct |
1084 ms |
129340 KB |
Output is correct |
115 |
Correct |
1123 ms |
129316 KB |
Output is correct |
116 |
Correct |
1060 ms |
133748 KB |
Output is correct |
117 |
Correct |
2668 ms |
221696 KB |
Output is correct |
118 |
Correct |
1080 ms |
128476 KB |
Output is correct |
119 |
Correct |
2668 ms |
221660 KB |
Output is correct |
120 |
Correct |
2791 ms |
247696 KB |
Output is correct |
121 |
Correct |
2796 ms |
221436 KB |
Output is correct |
122 |
Correct |
1407 ms |
152228 KB |
Output is correct |
123 |
Correct |
2191 ms |
222116 KB |
Output is correct |
124 |
Correct |
2272 ms |
188924 KB |
Output is correct |