Submission #1048928

# Submission time Handle Problem Language Result Execution time Memory
1048928 2024-08-08T11:16:06 Z 1L1YA Mergers (JOI19_mergers) C++14
70 / 100
772 ms 262144 KB
//1L1YA

#include<bits/stdc++.h>
using namespace std;

//#pragma GCC optimize ("O3,unrool-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

typedef long long       ll;
typedef pair<ll,ll>     pll;
typedef pair<int,int>   pii;

#define Pb              push_back
#define F               first
#define S               second
#define endl            '\n'
#define sep             ' '
#define all(x)          x.begin(),x.end()
#define al(x,n)         x+1,x+n+1
#define SZ(x)           ((int)x.size())
#define lc              (id<<1)
#define rc              (lc|1)
#define mid             (l+r>>1)
#define dokme(x)        cout << x << endl, exit(0)
#define sik(x)          cout << x << endl;continue;
#define FastIO          ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define FileIO          freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll oo=1e18;
const int mod=1e9+7;
const int inf=1e9+111;
const int N=5e5+11;
const int lg=20;

int n,k,ans,a[N],h[N],pr[N],cnt[N],par[lg][N];
vector<int> vc[N],adj[N];
set<int> st[N];
vector<pii> E;
bool mark[N];

void dfs(int v,int p=0){
	for(int u: adj[v])
		if(u!=p){
			h[u]=h[v]+1;
			par[0][u]=v;
			dfs(u,v);
		}
}

int jump(int v,int k){
	for(int i=0;i<lg;i++)
		if(k>>i & 1)
			v=par[i][v];
	return v;
}

int lca(int u,int v){
	if(h[u]>h[v])	swap(u,v);
	v=jump(v,h[v]-h[u]);
	if(u==v)	return v;
	for(int i=lg-1;~i;i--)
		if(par[i][u]!=par[i][v])
			u=par[i][u],v=par[i][v];
	return par[0][v];
}

void DFS(int v,int p=0){
	st[v].insert(a[v]);
	for(int u: adj[v])
		if(u!=p){
			DFS(u,v);
			cnt[v]+=cnt[u];
			if(SZ(st[v])<SZ(st[u]))	st[v].swap(st[u]);
			for(int x: st[u])	st[v].insert(x);
		}
	if(cnt[v]==SZ(st[v]))	mark[v]=1;
}

void dFs(int v,int p=0){
	for(int u: adj[v])
		if(u!=p){
			if(!mark[u])	pr[u]=pr[v];
			dFs(u,v);
		}
}

int main(){
	FastIO

	cin >> n >> k;
	for(int i=1;i<n;i++){
		int u,v;
		cin >> u >> v;
		adj[u].Pb(v);
		adj[v].Pb(u);
		E.Pb({u,v});
	}
	for(int i=1;i<=n;i++)	cin >> a[i],vc[a[i]].Pb(i);
	dfs(1);
	for(int i=1;i<lg;i++)
		for(int v=1;v<=n;v++)
			par[i][v]=par[i-1][par[i-1][v]];
	for(int i=1;i<=k;i++){
		int v=vc[i][0];
		for(int u: vc[i])	v=lca(u,v);
		cnt[v]++;
	}
	DFS(1);
	for(int i=1;i<=n;i++)	pr[i]=i;
	dFs(1);
	fill(cnt,cnt+n+1,0);
	for(auto [u,v]: E)
		if(pr[u]!=pr[v]){
			cnt[pr[u]]++;
			cnt[pr[v]]++;
			if(cnt[pr[u]]==1)	ans++;
			else if(cnt[pr[u]]==2)	ans--;
			if(cnt[pr[v]]==1)	ans++;
			else if(cnt[pr[v]]==2)	ans--;	
		}
	cout << (ans+1)/2 << endl;

	return 0;
}

Compilation message

mergers.cpp: In function 'int main()':
mergers.cpp:114:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  114 |  for(auto [u,v]: E)
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 10 ms 92764 KB Output is correct
2 Correct 9 ms 92760 KB Output is correct
3 Correct 10 ms 92764 KB Output is correct
4 Correct 10 ms 92764 KB Output is correct
5 Correct 10 ms 92640 KB Output is correct
6 Correct 10 ms 92764 KB Output is correct
7 Correct 9 ms 92736 KB Output is correct
8 Correct 10 ms 92768 KB Output is correct
9 Correct 11 ms 92808 KB Output is correct
10 Correct 10 ms 92712 KB Output is correct
11 Correct 10 ms 92824 KB Output is correct
12 Correct 10 ms 92764 KB Output is correct
13 Correct 10 ms 92764 KB Output is correct
14 Correct 10 ms 92764 KB Output is correct
15 Correct 11 ms 92652 KB Output is correct
16 Correct 11 ms 92764 KB Output is correct
17 Correct 10 ms 92768 KB Output is correct
18 Correct 10 ms 92680 KB Output is correct
19 Correct 9 ms 92648 KB Output is correct
20 Correct 10 ms 92760 KB Output is correct
21 Correct 10 ms 92764 KB Output is correct
22 Correct 9 ms 92736 KB Output is correct
23 Correct 11 ms 92800 KB Output is correct
24 Correct 9 ms 92748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 92764 KB Output is correct
2 Correct 9 ms 92760 KB Output is correct
3 Correct 10 ms 92764 KB Output is correct
4 Correct 10 ms 92764 KB Output is correct
5 Correct 10 ms 92640 KB Output is correct
6 Correct 10 ms 92764 KB Output is correct
7 Correct 9 ms 92736 KB Output is correct
8 Correct 10 ms 92768 KB Output is correct
9 Correct 11 ms 92808 KB Output is correct
10 Correct 10 ms 92712 KB Output is correct
11 Correct 10 ms 92824 KB Output is correct
12 Correct 10 ms 92764 KB Output is correct
13 Correct 10 ms 92764 KB Output is correct
14 Correct 10 ms 92764 KB Output is correct
15 Correct 11 ms 92652 KB Output is correct
16 Correct 11 ms 92764 KB Output is correct
17 Correct 10 ms 92768 KB Output is correct
18 Correct 10 ms 92680 KB Output is correct
19 Correct 9 ms 92648 KB Output is correct
20 Correct 10 ms 92760 KB Output is correct
21 Correct 10 ms 92764 KB Output is correct
22 Correct 9 ms 92736 KB Output is correct
23 Correct 11 ms 92800 KB Output is correct
24 Correct 9 ms 92748 KB Output is correct
25 Correct 10 ms 92764 KB Output is correct
26 Correct 15 ms 93276 KB Output is correct
27 Correct 11 ms 93020 KB Output is correct
28 Correct 11 ms 93276 KB Output is correct
29 Correct 12 ms 93680 KB Output is correct
30 Correct 12 ms 93276 KB Output is correct
31 Correct 10 ms 92764 KB Output is correct
32 Correct 11 ms 93404 KB Output is correct
33 Correct 10 ms 92640 KB Output is correct
34 Correct 12 ms 93016 KB Output is correct
35 Correct 12 ms 93532 KB Output is correct
36 Correct 11 ms 92888 KB Output is correct
37 Correct 12 ms 93420 KB Output is correct
38 Correct 10 ms 92764 KB Output is correct
39 Correct 12 ms 93528 KB Output is correct
40 Correct 11 ms 93012 KB Output is correct
41 Correct 11 ms 92952 KB Output is correct
42 Correct 12 ms 93020 KB Output is correct
43 Correct 11 ms 93276 KB Output is correct
44 Correct 9 ms 92764 KB Output is correct
45 Correct 12 ms 93272 KB Output is correct
46 Correct 12 ms 93276 KB Output is correct
47 Correct 10 ms 92760 KB Output is correct
48 Correct 11 ms 93020 KB Output is correct
49 Correct 11 ms 93328 KB Output is correct
50 Correct 11 ms 93276 KB Output is correct
51 Correct 11 ms 93020 KB Output is correct
52 Correct 11 ms 93020 KB Output is correct
53 Correct 12 ms 93020 KB Output is correct
54 Correct 11 ms 93172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 92764 KB Output is correct
2 Correct 9 ms 92760 KB Output is correct
3 Correct 10 ms 92764 KB Output is correct
4 Correct 10 ms 92764 KB Output is correct
5 Correct 10 ms 92640 KB Output is correct
6 Correct 10 ms 92764 KB Output is correct
7 Correct 9 ms 92736 KB Output is correct
8 Correct 10 ms 92768 KB Output is correct
9 Correct 11 ms 92808 KB Output is correct
10 Correct 10 ms 92712 KB Output is correct
11 Correct 10 ms 92824 KB Output is correct
12 Correct 10 ms 92764 KB Output is correct
13 Correct 10 ms 92764 KB Output is correct
14 Correct 10 ms 92764 KB Output is correct
15 Correct 11 ms 92652 KB Output is correct
16 Correct 11 ms 92764 KB Output is correct
17 Correct 10 ms 92768 KB Output is correct
18 Correct 10 ms 92680 KB Output is correct
19 Correct 9 ms 92648 KB Output is correct
20 Correct 10 ms 92760 KB Output is correct
21 Correct 10 ms 92764 KB Output is correct
22 Correct 9 ms 92736 KB Output is correct
23 Correct 11 ms 92800 KB Output is correct
24 Correct 9 ms 92748 KB Output is correct
25 Correct 10 ms 92760 KB Output is correct
26 Correct 51 ms 102792 KB Output is correct
27 Correct 80 ms 109340 KB Output is correct
28 Correct 11 ms 93016 KB Output is correct
29 Correct 10 ms 92764 KB Output is correct
30 Correct 10 ms 92764 KB Output is correct
31 Correct 59 ms 102644 KB Output is correct
32 Correct 11 ms 93020 KB Output is correct
33 Correct 77 ms 112696 KB Output is correct
34 Correct 81 ms 107976 KB Output is correct
35 Correct 11 ms 93020 KB Output is correct
36 Correct 66 ms 103620 KB Output is correct
37 Correct 11 ms 93268 KB Output is correct
38 Correct 13 ms 93020 KB Output is correct
39 Correct 50 ms 102844 KB Output is correct
40 Correct 11 ms 93272 KB Output is correct
41 Correct 61 ms 102476 KB Output is correct
42 Correct 71 ms 106012 KB Output is correct
43 Correct 10 ms 92764 KB Output is correct
44 Correct 71 ms 112832 KB Output is correct
45 Correct 79 ms 109124 KB Output is correct
46 Correct 11 ms 93020 KB Output is correct
47 Correct 11 ms 93188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 102848 KB Output is correct
2 Correct 82 ms 110504 KB Output is correct
3 Correct 12 ms 93276 KB Output is correct
4 Correct 11 ms 93020 KB Output is correct
5 Correct 10 ms 92760 KB Output is correct
6 Correct 10 ms 92704 KB Output is correct
7 Correct 10 ms 93020 KB Output is correct
8 Correct 97 ms 116680 KB Output is correct
9 Correct 11 ms 93016 KB Output is correct
10 Correct 61 ms 102936 KB Output is correct
11 Correct 11 ms 92760 KB Output is correct
12 Correct 66 ms 103128 KB Output is correct
13 Correct 100 ms 115756 KB Output is correct
14 Correct 129 ms 130276 KB Output is correct
15 Correct 53 ms 102828 KB Output is correct
16 Correct 11 ms 93016 KB Output is correct
17 Correct 10 ms 92668 KB Output is correct
18 Correct 101 ms 110708 KB Output is correct
19 Correct 103 ms 117960 KB Output is correct
20 Correct 12 ms 93276 KB Output is correct
21 Correct 10 ms 92752 KB Output is correct
22 Correct 78 ms 106552 KB Output is correct
23 Correct 11 ms 93016 KB Output is correct
24 Correct 69 ms 105156 KB Output is correct
25 Correct 95 ms 116500 KB Output is correct
26 Correct 12 ms 93276 KB Output is correct
27 Correct 11 ms 93388 KB Output is correct
28 Correct 12 ms 93056 KB Output is correct
29 Correct 11 ms 93020 KB Output is correct
30 Correct 11 ms 93216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 92764 KB Output is correct
2 Correct 9 ms 92760 KB Output is correct
3 Correct 10 ms 92764 KB Output is correct
4 Correct 10 ms 92764 KB Output is correct
5 Correct 10 ms 92640 KB Output is correct
6 Correct 10 ms 92764 KB Output is correct
7 Correct 9 ms 92736 KB Output is correct
8 Correct 10 ms 92768 KB Output is correct
9 Correct 11 ms 92808 KB Output is correct
10 Correct 10 ms 92712 KB Output is correct
11 Correct 10 ms 92824 KB Output is correct
12 Correct 10 ms 92764 KB Output is correct
13 Correct 10 ms 92764 KB Output is correct
14 Correct 10 ms 92764 KB Output is correct
15 Correct 11 ms 92652 KB Output is correct
16 Correct 11 ms 92764 KB Output is correct
17 Correct 10 ms 92768 KB Output is correct
18 Correct 10 ms 92680 KB Output is correct
19 Correct 9 ms 92648 KB Output is correct
20 Correct 10 ms 92760 KB Output is correct
21 Correct 10 ms 92764 KB Output is correct
22 Correct 9 ms 92736 KB Output is correct
23 Correct 11 ms 92800 KB Output is correct
24 Correct 9 ms 92748 KB Output is correct
25 Correct 10 ms 92764 KB Output is correct
26 Correct 15 ms 93276 KB Output is correct
27 Correct 11 ms 93020 KB Output is correct
28 Correct 11 ms 93276 KB Output is correct
29 Correct 12 ms 93680 KB Output is correct
30 Correct 12 ms 93276 KB Output is correct
31 Correct 10 ms 92764 KB Output is correct
32 Correct 11 ms 93404 KB Output is correct
33 Correct 10 ms 92640 KB Output is correct
34 Correct 12 ms 93016 KB Output is correct
35 Correct 12 ms 93532 KB Output is correct
36 Correct 11 ms 92888 KB Output is correct
37 Correct 12 ms 93420 KB Output is correct
38 Correct 10 ms 92764 KB Output is correct
39 Correct 12 ms 93528 KB Output is correct
40 Correct 11 ms 93012 KB Output is correct
41 Correct 11 ms 92952 KB Output is correct
42 Correct 12 ms 93020 KB Output is correct
43 Correct 11 ms 93276 KB Output is correct
44 Correct 9 ms 92764 KB Output is correct
45 Correct 12 ms 93272 KB Output is correct
46 Correct 12 ms 93276 KB Output is correct
47 Correct 10 ms 92760 KB Output is correct
48 Correct 11 ms 93020 KB Output is correct
49 Correct 11 ms 93328 KB Output is correct
50 Correct 11 ms 93276 KB Output is correct
51 Correct 11 ms 93020 KB Output is correct
52 Correct 11 ms 93020 KB Output is correct
53 Correct 12 ms 93020 KB Output is correct
54 Correct 11 ms 93172 KB Output is correct
55 Correct 10 ms 92760 KB Output is correct
56 Correct 51 ms 102792 KB Output is correct
57 Correct 80 ms 109340 KB Output is correct
58 Correct 11 ms 93016 KB Output is correct
59 Correct 10 ms 92764 KB Output is correct
60 Correct 10 ms 92764 KB Output is correct
61 Correct 59 ms 102644 KB Output is correct
62 Correct 11 ms 93020 KB Output is correct
63 Correct 77 ms 112696 KB Output is correct
64 Correct 81 ms 107976 KB Output is correct
65 Correct 11 ms 93020 KB Output is correct
66 Correct 66 ms 103620 KB Output is correct
67 Correct 11 ms 93268 KB Output is correct
68 Correct 13 ms 93020 KB Output is correct
69 Correct 50 ms 102844 KB Output is correct
70 Correct 11 ms 93272 KB Output is correct
71 Correct 61 ms 102476 KB Output is correct
72 Correct 71 ms 106012 KB Output is correct
73 Correct 10 ms 92764 KB Output is correct
74 Correct 71 ms 112832 KB Output is correct
75 Correct 79 ms 109124 KB Output is correct
76 Correct 11 ms 93020 KB Output is correct
77 Correct 11 ms 93188 KB Output is correct
78 Correct 56 ms 102848 KB Output is correct
79 Correct 82 ms 110504 KB Output is correct
80 Correct 12 ms 93276 KB Output is correct
81 Correct 11 ms 93020 KB Output is correct
82 Correct 10 ms 92760 KB Output is correct
83 Correct 10 ms 92704 KB Output is correct
84 Correct 10 ms 93020 KB Output is correct
85 Correct 97 ms 116680 KB Output is correct
86 Correct 11 ms 93016 KB Output is correct
87 Correct 61 ms 102936 KB Output is correct
88 Correct 11 ms 92760 KB Output is correct
89 Correct 66 ms 103128 KB Output is correct
90 Correct 100 ms 115756 KB Output is correct
91 Correct 129 ms 130276 KB Output is correct
92 Correct 53 ms 102828 KB Output is correct
93 Correct 11 ms 93016 KB Output is correct
94 Correct 10 ms 92668 KB Output is correct
95 Correct 101 ms 110708 KB Output is correct
96 Correct 103 ms 117960 KB Output is correct
97 Correct 12 ms 93276 KB Output is correct
98 Correct 10 ms 92752 KB Output is correct
99 Correct 78 ms 106552 KB Output is correct
100 Correct 11 ms 93016 KB Output is correct
101 Correct 69 ms 105156 KB Output is correct
102 Correct 95 ms 116500 KB Output is correct
103 Correct 12 ms 93276 KB Output is correct
104 Correct 11 ms 93388 KB Output is correct
105 Correct 12 ms 93056 KB Output is correct
106 Correct 11 ms 93020 KB Output is correct
107 Correct 11 ms 93216 KB Output is correct
108 Correct 505 ms 158304 KB Output is correct
109 Correct 533 ms 167576 KB Output is correct
110 Correct 563 ms 183224 KB Output is correct
111 Correct 772 ms 221880 KB Output is correct
112 Correct 767 ms 208976 KB Output is correct
113 Correct 635 ms 175580 KB Output is correct
114 Correct 381 ms 140264 KB Output is correct
115 Correct 367 ms 140320 KB Output is correct
116 Correct 612 ms 214584 KB Output is correct
117 Runtime error 529 ms 262144 KB Execution killed with signal 9
118 Halted 0 ms 0 KB -