# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
824115 |
2023-08-13T14:28:27 Z |
8pete8 |
Mergers (JOI19_mergers) |
C++14 |
|
823 ms |
141424 KB |
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<pii,int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define p push
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
const int mxn=5*1e5,mod=998244353,lg=19;
void setIO(string name) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int ans=0;
int pa[mxn+10],sz[mxn+10],up[mxn+10][lg+2],h[mxn+10];
vector<int>adj[mxn+10];
int find(int u){
if(u==pa[u])return u;
return pa[u]=find(pa[u]);
}
void merg(int u,int v){
int a=find(u),b=find(v);
if(a==b)return;
if(sz[a]>sz[b]){
pa[b]=a;
sz[a]+=sz[b];
return;
}
pa[a]=b;
sz[b]+=sz[a];
}
int st[mxn+10],un[mxn+10];
vector<int>s[mxn+10];
void solve(int cur,int p){
for(auto i:adj[cur]){
if(i==p)continue;
up[i][0]=cur;
h[i]=h[cur]+1;
solve(i,cur);
}
}
int lca(int u,int v){
if(h[u]<h[v])swap(u,v);
int k=h[u]-h[v];
for(int i=0;i<=30;i++)if(k&(1<<i))u=up[u][i];
if(u==v)return u;
for(int i=lg;i>=0;i--)if(up[u][i]!=up[v][i])u=up[u][i],v=up[v][i];
return up[u][0];
}
void solve2(int cur,int p){
int g=un[cur];
for(auto i:adj[cur]){
if(i==p)continue;
solve2(i,cur);
if(un[i]){
un[cur]=lca(un[cur],un[i]);
merg(st[cur],st[i]);
}
}
if(un[cur]==cur)un[cur]=0;
}
int deg[mxn+10];
int32_t main(){
//setIO("pieaters");
fastio
int n,m;cin>>n>>m;
for(int i=0;i<n-1;i++){
int u,v;cin>>u>>v;
adj[u].pb(v);
adj[v].pb(u);
}
h[0]=INT_MAX;
for(int i=1;i<=n;i++)cin>>st[i],s[st[i]].pb(i);
for(int i=1;i<=m;i++)pa[i]=i,sz[i]=1;
solve(1,-1);
for(int i=1;i<=lg;i++)for(int j=1;j<=n;j++)up[j][i]=up[up[j][i-1]][i-1];
for(int i=1;i<=m;i++){
int node=-1;
for(auto j:s[i]){
if(node==-1)node=j;
else node=lca(node,j);
}
for(auto j:s[i])un[j]=node;
}
solve2(1,-1);
int ans=0;
for(int i=1;i<=n;i++)st[i]=find(st[i]);
for(int i=1;i<=n;i++)for(auto j:adj[i])if(st[i]!=st[j])deg[st[j]]++;
for(int i=1;i<=n;i++)if(deg[i]==1)ans++;
cout<<(ans+1)/2;
}
Compilation message
mergers.cpp: In function 'void solve2(int, int)':
mergers.cpp:71:9: warning: unused variable 'g' [-Wunused-variable]
71 | int g=un[cur];
| ^
mergers.cpp: In function 'void setIO(std::string)':
mergers.cpp:31:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
31 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mergers.cpp:32:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
10 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23836 KB |
Output is correct |
4 |
Correct |
11 ms |
23788 KB |
Output is correct |
5 |
Correct |
11 ms |
23832 KB |
Output is correct |
6 |
Correct |
10 ms |
23832 KB |
Output is correct |
7 |
Correct |
10 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23824 KB |
Output is correct |
14 |
Correct |
11 ms |
23832 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23828 KB |
Output is correct |
17 |
Correct |
10 ms |
23832 KB |
Output is correct |
18 |
Correct |
10 ms |
23764 KB |
Output is correct |
19 |
Correct |
10 ms |
23764 KB |
Output is correct |
20 |
Correct |
9 ms |
23816 KB |
Output is correct |
21 |
Correct |
10 ms |
23832 KB |
Output is correct |
22 |
Correct |
10 ms |
23792 KB |
Output is correct |
23 |
Correct |
10 ms |
23848 KB |
Output is correct |
24 |
Correct |
10 ms |
23836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
10 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23836 KB |
Output is correct |
4 |
Correct |
11 ms |
23788 KB |
Output is correct |
5 |
Correct |
11 ms |
23832 KB |
Output is correct |
6 |
Correct |
10 ms |
23832 KB |
Output is correct |
7 |
Correct |
10 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23824 KB |
Output is correct |
14 |
Correct |
11 ms |
23832 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23828 KB |
Output is correct |
17 |
Correct |
10 ms |
23832 KB |
Output is correct |
18 |
Correct |
10 ms |
23764 KB |
Output is correct |
19 |
Correct |
10 ms |
23764 KB |
Output is correct |
20 |
Correct |
9 ms |
23816 KB |
Output is correct |
21 |
Correct |
10 ms |
23832 KB |
Output is correct |
22 |
Correct |
10 ms |
23792 KB |
Output is correct |
23 |
Correct |
10 ms |
23848 KB |
Output is correct |
24 |
Correct |
10 ms |
23836 KB |
Output is correct |
25 |
Correct |
11 ms |
23832 KB |
Output is correct |
26 |
Correct |
13 ms |
24268 KB |
Output is correct |
27 |
Correct |
14 ms |
24340 KB |
Output is correct |
28 |
Correct |
12 ms |
24404 KB |
Output is correct |
29 |
Correct |
12 ms |
24404 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23844 KB |
Output is correct |
32 |
Correct |
13 ms |
24480 KB |
Output is correct |
33 |
Correct |
11 ms |
23820 KB |
Output is correct |
34 |
Correct |
12 ms |
24284 KB |
Output is correct |
35 |
Correct |
12 ms |
24292 KB |
Output is correct |
36 |
Correct |
12 ms |
24276 KB |
Output is correct |
37 |
Correct |
12 ms |
24276 KB |
Output is correct |
38 |
Correct |
12 ms |
23824 KB |
Output is correct |
39 |
Correct |
13 ms |
24276 KB |
Output is correct |
40 |
Correct |
12 ms |
24280 KB |
Output is correct |
41 |
Correct |
12 ms |
24216 KB |
Output is correct |
42 |
Correct |
12 ms |
24276 KB |
Output is correct |
43 |
Correct |
12 ms |
24540 KB |
Output is correct |
44 |
Correct |
11 ms |
23764 KB |
Output is correct |
45 |
Correct |
12 ms |
24356 KB |
Output is correct |
46 |
Correct |
15 ms |
24276 KB |
Output is correct |
47 |
Correct |
11 ms |
23844 KB |
Output is correct |
48 |
Correct |
12 ms |
24268 KB |
Output is correct |
49 |
Correct |
12 ms |
24360 KB |
Output is correct |
50 |
Correct |
12 ms |
24404 KB |
Output is correct |
51 |
Correct |
13 ms |
24288 KB |
Output is correct |
52 |
Correct |
13 ms |
24228 KB |
Output is correct |
53 |
Correct |
14 ms |
24352 KB |
Output is correct |
54 |
Correct |
13 ms |
24276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
10 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23836 KB |
Output is correct |
4 |
Correct |
11 ms |
23788 KB |
Output is correct |
5 |
Correct |
11 ms |
23832 KB |
Output is correct |
6 |
Correct |
10 ms |
23832 KB |
Output is correct |
7 |
Correct |
10 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23824 KB |
Output is correct |
14 |
Correct |
11 ms |
23832 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23828 KB |
Output is correct |
17 |
Correct |
10 ms |
23832 KB |
Output is correct |
18 |
Correct |
10 ms |
23764 KB |
Output is correct |
19 |
Correct |
10 ms |
23764 KB |
Output is correct |
20 |
Correct |
9 ms |
23816 KB |
Output is correct |
21 |
Correct |
10 ms |
23832 KB |
Output is correct |
22 |
Correct |
10 ms |
23792 KB |
Output is correct |
23 |
Correct |
10 ms |
23848 KB |
Output is correct |
24 |
Correct |
10 ms |
23836 KB |
Output is correct |
25 |
Correct |
10 ms |
23764 KB |
Output is correct |
26 |
Correct |
53 ms |
38708 KB |
Output is correct |
27 |
Correct |
82 ms |
38436 KB |
Output is correct |
28 |
Correct |
13 ms |
24224 KB |
Output is correct |
29 |
Correct |
11 ms |
23764 KB |
Output is correct |
30 |
Correct |
11 ms |
23832 KB |
Output is correct |
31 |
Correct |
68 ms |
38404 KB |
Output is correct |
32 |
Correct |
13 ms |
24244 KB |
Output is correct |
33 |
Correct |
86 ms |
45600 KB |
Output is correct |
34 |
Correct |
67 ms |
38380 KB |
Output is correct |
35 |
Correct |
12 ms |
24228 KB |
Output is correct |
36 |
Correct |
75 ms |
39008 KB |
Output is correct |
37 |
Correct |
13 ms |
24184 KB |
Output is correct |
38 |
Correct |
13 ms |
24272 KB |
Output is correct |
39 |
Correct |
69 ms |
38580 KB |
Output is correct |
40 |
Correct |
13 ms |
24484 KB |
Output is correct |
41 |
Correct |
69 ms |
38288 KB |
Output is correct |
42 |
Correct |
79 ms |
40732 KB |
Output is correct |
43 |
Correct |
12 ms |
23764 KB |
Output is correct |
44 |
Correct |
88 ms |
45728 KB |
Output is correct |
45 |
Correct |
80 ms |
42068 KB |
Output is correct |
46 |
Correct |
13 ms |
24276 KB |
Output is correct |
47 |
Correct |
13 ms |
24224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
37288 KB |
Output is correct |
2 |
Correct |
57 ms |
40988 KB |
Output is correct |
3 |
Correct |
13 ms |
24324 KB |
Output is correct |
4 |
Correct |
13 ms |
24232 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23828 KB |
Output is correct |
7 |
Correct |
13 ms |
24228 KB |
Output is correct |
8 |
Correct |
109 ms |
40336 KB |
Output is correct |
9 |
Correct |
15 ms |
24288 KB |
Output is correct |
10 |
Correct |
76 ms |
38584 KB |
Output is correct |
11 |
Correct |
13 ms |
23860 KB |
Output is correct |
12 |
Correct |
76 ms |
38440 KB |
Output is correct |
13 |
Correct |
93 ms |
40200 KB |
Output is correct |
14 |
Correct |
72 ms |
42500 KB |
Output is correct |
15 |
Correct |
58 ms |
38616 KB |
Output is correct |
16 |
Correct |
14 ms |
24236 KB |
Output is correct |
17 |
Correct |
12 ms |
23872 KB |
Output is correct |
18 |
Correct |
64 ms |
42284 KB |
Output is correct |
19 |
Correct |
82 ms |
46368 KB |
Output is correct |
20 |
Correct |
14 ms |
24232 KB |
Output is correct |
21 |
Correct |
12 ms |
23864 KB |
Output is correct |
22 |
Correct |
66 ms |
40524 KB |
Output is correct |
23 |
Correct |
14 ms |
24224 KB |
Output is correct |
24 |
Correct |
81 ms |
38992 KB |
Output is correct |
25 |
Correct |
79 ms |
44992 KB |
Output is correct |
26 |
Correct |
15 ms |
24404 KB |
Output is correct |
27 |
Correct |
14 ms |
24356 KB |
Output is correct |
28 |
Correct |
15 ms |
24308 KB |
Output is correct |
29 |
Correct |
13 ms |
24224 KB |
Output is correct |
30 |
Correct |
13 ms |
24284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
23764 KB |
Output is correct |
2 |
Correct |
10 ms |
23764 KB |
Output is correct |
3 |
Correct |
11 ms |
23836 KB |
Output is correct |
4 |
Correct |
11 ms |
23788 KB |
Output is correct |
5 |
Correct |
11 ms |
23832 KB |
Output is correct |
6 |
Correct |
10 ms |
23832 KB |
Output is correct |
7 |
Correct |
10 ms |
23764 KB |
Output is correct |
8 |
Correct |
11 ms |
23764 KB |
Output is correct |
9 |
Correct |
11 ms |
23764 KB |
Output is correct |
10 |
Correct |
11 ms |
23840 KB |
Output is correct |
11 |
Correct |
12 ms |
23764 KB |
Output is correct |
12 |
Correct |
11 ms |
23764 KB |
Output is correct |
13 |
Correct |
12 ms |
23824 KB |
Output is correct |
14 |
Correct |
11 ms |
23832 KB |
Output is correct |
15 |
Correct |
11 ms |
23764 KB |
Output is correct |
16 |
Correct |
11 ms |
23828 KB |
Output is correct |
17 |
Correct |
10 ms |
23832 KB |
Output is correct |
18 |
Correct |
10 ms |
23764 KB |
Output is correct |
19 |
Correct |
10 ms |
23764 KB |
Output is correct |
20 |
Correct |
9 ms |
23816 KB |
Output is correct |
21 |
Correct |
10 ms |
23832 KB |
Output is correct |
22 |
Correct |
10 ms |
23792 KB |
Output is correct |
23 |
Correct |
10 ms |
23848 KB |
Output is correct |
24 |
Correct |
10 ms |
23836 KB |
Output is correct |
25 |
Correct |
11 ms |
23832 KB |
Output is correct |
26 |
Correct |
13 ms |
24268 KB |
Output is correct |
27 |
Correct |
14 ms |
24340 KB |
Output is correct |
28 |
Correct |
12 ms |
24404 KB |
Output is correct |
29 |
Correct |
12 ms |
24404 KB |
Output is correct |
30 |
Correct |
13 ms |
24276 KB |
Output is correct |
31 |
Correct |
12 ms |
23844 KB |
Output is correct |
32 |
Correct |
13 ms |
24480 KB |
Output is correct |
33 |
Correct |
11 ms |
23820 KB |
Output is correct |
34 |
Correct |
12 ms |
24284 KB |
Output is correct |
35 |
Correct |
12 ms |
24292 KB |
Output is correct |
36 |
Correct |
12 ms |
24276 KB |
Output is correct |
37 |
Correct |
12 ms |
24276 KB |
Output is correct |
38 |
Correct |
12 ms |
23824 KB |
Output is correct |
39 |
Correct |
13 ms |
24276 KB |
Output is correct |
40 |
Correct |
12 ms |
24280 KB |
Output is correct |
41 |
Correct |
12 ms |
24216 KB |
Output is correct |
42 |
Correct |
12 ms |
24276 KB |
Output is correct |
43 |
Correct |
12 ms |
24540 KB |
Output is correct |
44 |
Correct |
11 ms |
23764 KB |
Output is correct |
45 |
Correct |
12 ms |
24356 KB |
Output is correct |
46 |
Correct |
15 ms |
24276 KB |
Output is correct |
47 |
Correct |
11 ms |
23844 KB |
Output is correct |
48 |
Correct |
12 ms |
24268 KB |
Output is correct |
49 |
Correct |
12 ms |
24360 KB |
Output is correct |
50 |
Correct |
12 ms |
24404 KB |
Output is correct |
51 |
Correct |
13 ms |
24288 KB |
Output is correct |
52 |
Correct |
13 ms |
24228 KB |
Output is correct |
53 |
Correct |
14 ms |
24352 KB |
Output is correct |
54 |
Correct |
13 ms |
24276 KB |
Output is correct |
55 |
Correct |
10 ms |
23764 KB |
Output is correct |
56 |
Correct |
53 ms |
38708 KB |
Output is correct |
57 |
Correct |
82 ms |
38436 KB |
Output is correct |
58 |
Correct |
13 ms |
24224 KB |
Output is correct |
59 |
Correct |
11 ms |
23764 KB |
Output is correct |
60 |
Correct |
11 ms |
23832 KB |
Output is correct |
61 |
Correct |
68 ms |
38404 KB |
Output is correct |
62 |
Correct |
13 ms |
24244 KB |
Output is correct |
63 |
Correct |
86 ms |
45600 KB |
Output is correct |
64 |
Correct |
67 ms |
38380 KB |
Output is correct |
65 |
Correct |
12 ms |
24228 KB |
Output is correct |
66 |
Correct |
75 ms |
39008 KB |
Output is correct |
67 |
Correct |
13 ms |
24184 KB |
Output is correct |
68 |
Correct |
13 ms |
24272 KB |
Output is correct |
69 |
Correct |
69 ms |
38580 KB |
Output is correct |
70 |
Correct |
13 ms |
24484 KB |
Output is correct |
71 |
Correct |
69 ms |
38288 KB |
Output is correct |
72 |
Correct |
79 ms |
40732 KB |
Output is correct |
73 |
Correct |
12 ms |
23764 KB |
Output is correct |
74 |
Correct |
88 ms |
45728 KB |
Output is correct |
75 |
Correct |
80 ms |
42068 KB |
Output is correct |
76 |
Correct |
13 ms |
24276 KB |
Output is correct |
77 |
Correct |
13 ms |
24224 KB |
Output is correct |
78 |
Correct |
52 ms |
37288 KB |
Output is correct |
79 |
Correct |
57 ms |
40988 KB |
Output is correct |
80 |
Correct |
13 ms |
24324 KB |
Output is correct |
81 |
Correct |
13 ms |
24232 KB |
Output is correct |
82 |
Correct |
12 ms |
23764 KB |
Output is correct |
83 |
Correct |
11 ms |
23828 KB |
Output is correct |
84 |
Correct |
13 ms |
24228 KB |
Output is correct |
85 |
Correct |
109 ms |
40336 KB |
Output is correct |
86 |
Correct |
15 ms |
24288 KB |
Output is correct |
87 |
Correct |
76 ms |
38584 KB |
Output is correct |
88 |
Correct |
13 ms |
23860 KB |
Output is correct |
89 |
Correct |
76 ms |
38440 KB |
Output is correct |
90 |
Correct |
93 ms |
40200 KB |
Output is correct |
91 |
Correct |
72 ms |
42500 KB |
Output is correct |
92 |
Correct |
58 ms |
38616 KB |
Output is correct |
93 |
Correct |
14 ms |
24236 KB |
Output is correct |
94 |
Correct |
12 ms |
23872 KB |
Output is correct |
95 |
Correct |
64 ms |
42284 KB |
Output is correct |
96 |
Correct |
82 ms |
46368 KB |
Output is correct |
97 |
Correct |
14 ms |
24232 KB |
Output is correct |
98 |
Correct |
12 ms |
23864 KB |
Output is correct |
99 |
Correct |
66 ms |
40524 KB |
Output is correct |
100 |
Correct |
14 ms |
24224 KB |
Output is correct |
101 |
Correct |
81 ms |
38992 KB |
Output is correct |
102 |
Correct |
79 ms |
44992 KB |
Output is correct |
103 |
Correct |
15 ms |
24404 KB |
Output is correct |
104 |
Correct |
14 ms |
24356 KB |
Output is correct |
105 |
Correct |
15 ms |
24308 KB |
Output is correct |
106 |
Correct |
13 ms |
24224 KB |
Output is correct |
107 |
Correct |
13 ms |
24284 KB |
Output is correct |
108 |
Correct |
497 ms |
107116 KB |
Output is correct |
109 |
Correct |
771 ms |
114592 KB |
Output is correct |
110 |
Correct |
823 ms |
125908 KB |
Output is correct |
111 |
Correct |
752 ms |
141424 KB |
Output is correct |
112 |
Correct |
748 ms |
131636 KB |
Output is correct |
113 |
Correct |
439 ms |
117704 KB |
Output is correct |
114 |
Correct |
602 ms |
96716 KB |
Output is correct |
115 |
Correct |
560 ms |
96680 KB |
Output is correct |
116 |
Correct |
707 ms |
102504 KB |
Output is correct |
117 |
Correct |
555 ms |
118412 KB |
Output is correct |
118 |
Correct |
595 ms |
97272 KB |
Output is correct |
119 |
Correct |
559 ms |
118324 KB |
Output is correct |
120 |
Correct |
668 ms |
132388 KB |
Output is correct |
121 |
Correct |
594 ms |
118444 KB |
Output is correct |
122 |
Correct |
717 ms |
109776 KB |
Output is correct |
123 |
Correct |
405 ms |
119912 KB |
Output is correct |
124 |
Correct |
729 ms |
116356 KB |
Output is correct |