Submission #105560

# Submission time Handle Problem Language Result Execution time Memory
105560 2019-04-13T13:34:46 Z zscoder Mergers (JOI19_mergers) C++17
100 / 100
1923 ms 72612 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
 
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld; 
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> pbds;

vi adj[555555];
int tot[555555];
int a[555555];
int bad[555555];
int ct;
int act[555555];
int siz[555555];
int par[555555];
void calcsiz(int u, int p=-1)
{
	par[u]=p;
	if(adj[u].size()>1&&adj[u][0]==p) swap(adj[u][1],adj[u][0]);
	siz[u]=1;
	for(auto &v:adj[u])
	{
		if(v==p) continue;
		calcsiz(v,u);
		siz[u]+=siz[v];
		if(siz[v]>siz[adj[u][0]]) swap(v,adj[u][0]);
	}
}

vector<int> V;

void dfs2(int u, int p=-1)
{
	V.pb(a[u]);
	if(act[a[u]]==-1)
	{
		ct++; act[a[u]]=tot[a[u]];
	}
	act[a[u]]--;
	if(act[a[u]]==0) ct--;
	for(int v:adj[u])
	{
		if(v==p) continue;
		dfs2(v,u);
	}
}

void prep(int u, int p=-1)
{
	for(int i=1;i<adj[u].size();i++)
	{
		int v=adj[u][i];
		if(v==p) continue;
		prep(v,u);
		while(!V.empty()){act[V.back()]=-1; V.pop_back();}
		ct=0;
	}
	if(adj[u][0]!=p) prep(adj[u][0],u);
	for(int i=1;i<adj[u].size();i++)
	{
		int v=adj[u][i];
		if(v==p) continue;
		dfs2(v,u);
	}
	if(act[a[u]]==-1)
	{
		ct++; act[a[u]]=tot[a[u]];
	}
	act[a[u]]--;
	V.pb(a[u]);
	if(act[a[u]]==0) ct--;
	if(ct==0) bad[u]=1;
	//if(bad[u]) cerr<<"BAD "<<u<<'\n';
}

set<int> S;

void prep2(int u, int p)
{
	for(int v:adj[u])
	{
		if(v==p) continue;
		prep2(v,u);
	}
	S.insert(a[u]);
}
/*
int dp[555555][2];
const int INF = int(1e9);

void dfs(int u, int p=-1)
{
	vi pre(2,INF);
	pre[0]=0;
	for(int v:adj[u])
	{
		if(v==p) continue;
		dfs(v,u);
		vi nw(2,INF);
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<2;j++)
			{
				if(j==0&&bad[v]) continue;
				if(j==0) nw[i]=min(nw[i],pre[i]+dp[v][j]);
				else nw[i^1]=min(nw[i^1],pre[i]+dp[v][j]+(i==1?-1:0));
				//cerr<<i<<' '<<j<<' '<<nw[0]<<' '<<nw[1]<<'\n';
			}
		}
		pre=nw;
		//cerr<<u<<' '<<pre[0]<<' '<<pre[1]<<'\n';
	}
	dp[u][0]=min(pre[0],pre[1]); dp[u][1]=min(pre[0]+1,pre[1]);
	if(bad[u]) dp[u][0]=INF;
	//if(bad[u]) cerr<<"DP : "<<u<<' '<<dp[u][0]<<' '<<dp[u][1]<<'\n';
}
*/

int subsiz[555555];

void dfs(int u, int p=-1)
{
	subsiz[u]=bad[u];
	for(int v:adj[u])
	{
		if(v==p) continue;
		dfs(v,u);
		subsiz[u]+=subsiz[v];
	}
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0); 
	int n,k; cin>>n>>k;
	if(n==1){cout<<0<<'\n'; return 0;}
	memset(act,-1,sizeof(act));
	for(int i=0;i<n-1;i++)
	{
		int u,v; cin>>u>>v; u--; v--;
		adj[u].pb(v); adj[v].pb(u);
	}
	for(int i=0;i<n;i++)
	{
		cin>>a[i]; a[i]--;
		tot[a[i]]++;
	}
	calcsiz(0);
	
	prep(0); bad[0]=0;
	int badsiz=0;
	for(int i=0;i<n;i++) badsiz+=bad[i];
	dfs(0);
	int cnt=0;
	for(int i=0;i<n;i++)
	{
		if(bad[i])
		{
			if(subsiz[i]==1||subsiz[i]==badsiz) cnt++;
		}
	}
	cout<<(cnt+1)/2<<'\n';
}

Compilation message

mergers.cpp: In function 'void prep(int, int)':
mergers.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<adj[u].size();i++)
              ~^~~~~~~~~~~~~~
mergers.cpp:73:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<adj[u].size();i++)
              ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 15 ms 15616 KB Output is correct
3 Correct 17 ms 15616 KB Output is correct
4 Correct 27 ms 15616 KB Output is correct
5 Correct 21 ms 15616 KB Output is correct
6 Correct 19 ms 15616 KB Output is correct
7 Correct 14 ms 13440 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 18 ms 15616 KB Output is correct
10 Correct 16 ms 15560 KB Output is correct
11 Correct 17 ms 15616 KB Output is correct
12 Correct 20 ms 15608 KB Output is correct
13 Correct 17 ms 15616 KB Output is correct
14 Correct 18 ms 15616 KB Output is correct
15 Correct 17 ms 15616 KB Output is correct
16 Correct 19 ms 15616 KB Output is correct
17 Correct 17 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 17 ms 15616 KB Output is correct
20 Correct 16 ms 15616 KB Output is correct
21 Correct 19 ms 15744 KB Output is correct
22 Correct 17 ms 15616 KB Output is correct
23 Correct 16 ms 15616 KB Output is correct
24 Correct 17 ms 15616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 15 ms 15616 KB Output is correct
3 Correct 17 ms 15616 KB Output is correct
4 Correct 27 ms 15616 KB Output is correct
5 Correct 21 ms 15616 KB Output is correct
6 Correct 19 ms 15616 KB Output is correct
7 Correct 14 ms 13440 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 18 ms 15616 KB Output is correct
10 Correct 16 ms 15560 KB Output is correct
11 Correct 17 ms 15616 KB Output is correct
12 Correct 20 ms 15608 KB Output is correct
13 Correct 17 ms 15616 KB Output is correct
14 Correct 18 ms 15616 KB Output is correct
15 Correct 17 ms 15616 KB Output is correct
16 Correct 19 ms 15616 KB Output is correct
17 Correct 17 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 17 ms 15616 KB Output is correct
20 Correct 16 ms 15616 KB Output is correct
21 Correct 19 ms 15744 KB Output is correct
22 Correct 17 ms 15616 KB Output is correct
23 Correct 16 ms 15616 KB Output is correct
24 Correct 17 ms 15616 KB Output is correct
25 Correct 17 ms 15616 KB Output is correct
26 Correct 19 ms 15824 KB Output is correct
27 Correct 21 ms 15872 KB Output is correct
28 Correct 19 ms 15872 KB Output is correct
29 Correct 22 ms 15872 KB Output is correct
30 Correct 18 ms 15744 KB Output is correct
31 Correct 17 ms 15588 KB Output is correct
32 Correct 17 ms 16000 KB Output is correct
33 Correct 16 ms 15616 KB Output is correct
34 Correct 18 ms 15744 KB Output is correct
35 Correct 21 ms 15744 KB Output is correct
36 Correct 20 ms 15872 KB Output is correct
37 Correct 18 ms 15872 KB Output is correct
38 Correct 16 ms 15616 KB Output is correct
39 Correct 18 ms 15744 KB Output is correct
40 Correct 18 ms 15744 KB Output is correct
41 Correct 17 ms 15872 KB Output is correct
42 Correct 17 ms 15872 KB Output is correct
43 Correct 20 ms 16000 KB Output is correct
44 Correct 16 ms 15616 KB Output is correct
45 Correct 18 ms 15872 KB Output is correct
46 Correct 20 ms 15864 KB Output is correct
47 Correct 20 ms 15616 KB Output is correct
48 Correct 23 ms 15872 KB Output is correct
49 Correct 18 ms 15872 KB Output is correct
50 Correct 19 ms 15872 KB Output is correct
51 Correct 19 ms 15992 KB Output is correct
52 Correct 18 ms 15872 KB Output is correct
53 Correct 18 ms 15744 KB Output is correct
54 Correct 21 ms 15744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 15 ms 15616 KB Output is correct
3 Correct 17 ms 15616 KB Output is correct
4 Correct 27 ms 15616 KB Output is correct
5 Correct 21 ms 15616 KB Output is correct
6 Correct 19 ms 15616 KB Output is correct
7 Correct 14 ms 13440 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 18 ms 15616 KB Output is correct
10 Correct 16 ms 15560 KB Output is correct
11 Correct 17 ms 15616 KB Output is correct
12 Correct 20 ms 15608 KB Output is correct
13 Correct 17 ms 15616 KB Output is correct
14 Correct 18 ms 15616 KB Output is correct
15 Correct 17 ms 15616 KB Output is correct
16 Correct 19 ms 15616 KB Output is correct
17 Correct 17 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 17 ms 15616 KB Output is correct
20 Correct 16 ms 15616 KB Output is correct
21 Correct 19 ms 15744 KB Output is correct
22 Correct 17 ms 15616 KB Output is correct
23 Correct 16 ms 15616 KB Output is correct
24 Correct 17 ms 15616 KB Output is correct
25 Correct 17 ms 15616 KB Output is correct
26 Correct 88 ms 21172 KB Output is correct
27 Correct 197 ms 20948 KB Output is correct
28 Correct 18 ms 15744 KB Output is correct
29 Correct 17 ms 15616 KB Output is correct
30 Correct 16 ms 15616 KB Output is correct
31 Correct 198 ms 21140 KB Output is correct
32 Correct 20 ms 15872 KB Output is correct
33 Correct 196 ms 28144 KB Output is correct
34 Correct 222 ms 22336 KB Output is correct
35 Correct 19 ms 15744 KB Output is correct
36 Correct 195 ms 23324 KB Output is correct
37 Correct 20 ms 15744 KB Output is correct
38 Correct 23 ms 15864 KB Output is correct
39 Correct 102 ms 22652 KB Output is correct
40 Correct 21 ms 16000 KB Output is correct
41 Correct 187 ms 22256 KB Output is correct
42 Correct 236 ms 24824 KB Output is correct
43 Correct 17 ms 15616 KB Output is correct
44 Correct 228 ms 29652 KB Output is correct
45 Correct 204 ms 26224 KB Output is correct
46 Correct 22 ms 15872 KB Output is correct
47 Correct 21 ms 15744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 101 ms 21264 KB Output is correct
2 Correct 156 ms 22036 KB Output is correct
3 Correct 23 ms 15864 KB Output is correct
4 Correct 20 ms 15744 KB Output is correct
5 Correct 18 ms 15616 KB Output is correct
6 Correct 20 ms 15616 KB Output is correct
7 Correct 21 ms 15768 KB Output is correct
8 Correct 192 ms 21480 KB Output is correct
9 Correct 21 ms 15864 KB Output is correct
10 Correct 182 ms 22876 KB Output is correct
11 Correct 19 ms 15616 KB Output is correct
12 Correct 216 ms 22788 KB Output is correct
13 Correct 216 ms 23176 KB Output is correct
14 Correct 193 ms 23428 KB Output is correct
15 Correct 111 ms 22636 KB Output is correct
16 Correct 19 ms 15872 KB Output is correct
17 Correct 19 ms 15620 KB Output is correct
18 Correct 118 ms 23768 KB Output is correct
19 Correct 220 ms 27376 KB Output is correct
20 Correct 22 ms 15744 KB Output is correct
21 Correct 19 ms 15616 KB Output is correct
22 Correct 118 ms 23408 KB Output is correct
23 Correct 23 ms 15864 KB Output is correct
24 Correct 181 ms 22896 KB Output is correct
25 Correct 159 ms 25972 KB Output is correct
26 Correct 18 ms 15872 KB Output is correct
27 Correct 19 ms 15872 KB Output is correct
28 Correct 19 ms 15864 KB Output is correct
29 Correct 19 ms 15872 KB Output is correct
30 Correct 18 ms 15872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 15 ms 15616 KB Output is correct
3 Correct 17 ms 15616 KB Output is correct
4 Correct 27 ms 15616 KB Output is correct
5 Correct 21 ms 15616 KB Output is correct
6 Correct 19 ms 15616 KB Output is correct
7 Correct 14 ms 13440 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 18 ms 15616 KB Output is correct
10 Correct 16 ms 15560 KB Output is correct
11 Correct 17 ms 15616 KB Output is correct
12 Correct 20 ms 15608 KB Output is correct
13 Correct 17 ms 15616 KB Output is correct
14 Correct 18 ms 15616 KB Output is correct
15 Correct 17 ms 15616 KB Output is correct
16 Correct 19 ms 15616 KB Output is correct
17 Correct 17 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 17 ms 15616 KB Output is correct
20 Correct 16 ms 15616 KB Output is correct
21 Correct 19 ms 15744 KB Output is correct
22 Correct 17 ms 15616 KB Output is correct
23 Correct 16 ms 15616 KB Output is correct
24 Correct 17 ms 15616 KB Output is correct
25 Correct 17 ms 15616 KB Output is correct
26 Correct 19 ms 15824 KB Output is correct
27 Correct 21 ms 15872 KB Output is correct
28 Correct 19 ms 15872 KB Output is correct
29 Correct 22 ms 15872 KB Output is correct
30 Correct 18 ms 15744 KB Output is correct
31 Correct 17 ms 15588 KB Output is correct
32 Correct 17 ms 16000 KB Output is correct
33 Correct 16 ms 15616 KB Output is correct
34 Correct 18 ms 15744 KB Output is correct
35 Correct 21 ms 15744 KB Output is correct
36 Correct 20 ms 15872 KB Output is correct
37 Correct 18 ms 15872 KB Output is correct
38 Correct 16 ms 15616 KB Output is correct
39 Correct 18 ms 15744 KB Output is correct
40 Correct 18 ms 15744 KB Output is correct
41 Correct 17 ms 15872 KB Output is correct
42 Correct 17 ms 15872 KB Output is correct
43 Correct 20 ms 16000 KB Output is correct
44 Correct 16 ms 15616 KB Output is correct
45 Correct 18 ms 15872 KB Output is correct
46 Correct 20 ms 15864 KB Output is correct
47 Correct 20 ms 15616 KB Output is correct
48 Correct 23 ms 15872 KB Output is correct
49 Correct 18 ms 15872 KB Output is correct
50 Correct 19 ms 15872 KB Output is correct
51 Correct 19 ms 15992 KB Output is correct
52 Correct 18 ms 15872 KB Output is correct
53 Correct 18 ms 15744 KB Output is correct
54 Correct 21 ms 15744 KB Output is correct
55 Correct 17 ms 15616 KB Output is correct
56 Correct 88 ms 21172 KB Output is correct
57 Correct 197 ms 20948 KB Output is correct
58 Correct 18 ms 15744 KB Output is correct
59 Correct 17 ms 15616 KB Output is correct
60 Correct 16 ms 15616 KB Output is correct
61 Correct 198 ms 21140 KB Output is correct
62 Correct 20 ms 15872 KB Output is correct
63 Correct 196 ms 28144 KB Output is correct
64 Correct 222 ms 22336 KB Output is correct
65 Correct 19 ms 15744 KB Output is correct
66 Correct 195 ms 23324 KB Output is correct
67 Correct 20 ms 15744 KB Output is correct
68 Correct 23 ms 15864 KB Output is correct
69 Correct 102 ms 22652 KB Output is correct
70 Correct 21 ms 16000 KB Output is correct
71 Correct 187 ms 22256 KB Output is correct
72 Correct 236 ms 24824 KB Output is correct
73 Correct 17 ms 15616 KB Output is correct
74 Correct 228 ms 29652 KB Output is correct
75 Correct 204 ms 26224 KB Output is correct
76 Correct 22 ms 15872 KB Output is correct
77 Correct 21 ms 15744 KB Output is correct
78 Correct 101 ms 21264 KB Output is correct
79 Correct 156 ms 22036 KB Output is correct
80 Correct 23 ms 15864 KB Output is correct
81 Correct 20 ms 15744 KB Output is correct
82 Correct 18 ms 15616 KB Output is correct
83 Correct 20 ms 15616 KB Output is correct
84 Correct 21 ms 15768 KB Output is correct
85 Correct 192 ms 21480 KB Output is correct
86 Correct 21 ms 15864 KB Output is correct
87 Correct 182 ms 22876 KB Output is correct
88 Correct 19 ms 15616 KB Output is correct
89 Correct 216 ms 22788 KB Output is correct
90 Correct 216 ms 23176 KB Output is correct
91 Correct 193 ms 23428 KB Output is correct
92 Correct 111 ms 22636 KB Output is correct
93 Correct 19 ms 15872 KB Output is correct
94 Correct 19 ms 15620 KB Output is correct
95 Correct 118 ms 23768 KB Output is correct
96 Correct 220 ms 27376 KB Output is correct
97 Correct 22 ms 15744 KB Output is correct
98 Correct 19 ms 15616 KB Output is correct
99 Correct 118 ms 23408 KB Output is correct
100 Correct 23 ms 15864 KB Output is correct
101 Correct 181 ms 22896 KB Output is correct
102 Correct 159 ms 25972 KB Output is correct
103 Correct 18 ms 15872 KB Output is correct
104 Correct 19 ms 15872 KB Output is correct
105 Correct 19 ms 15864 KB Output is correct
106 Correct 19 ms 15872 KB Output is correct
107 Correct 18 ms 15872 KB Output is correct
108 Correct 759 ms 47516 KB Output is correct
109 Correct 1413 ms 61648 KB Output is correct
110 Correct 1393 ms 72612 KB Output is correct
111 Correct 1491 ms 70520 KB Output is correct
112 Correct 1577 ms 60568 KB Output is correct
113 Correct 793 ms 48596 KB Output is correct
114 Correct 1326 ms 43396 KB Output is correct
115 Correct 1409 ms 43528 KB Output is correct
116 Correct 1411 ms 45540 KB Output is correct
117 Correct 1707 ms 47508 KB Output is correct
118 Correct 1377 ms 44384 KB Output is correct
119 Correct 1627 ms 47348 KB Output is correct
120 Correct 1516 ms 61108 KB Output is correct
121 Correct 1600 ms 47328 KB Output is correct
122 Correct 1486 ms 46440 KB Output is correct
123 Correct 801 ms 48480 KB Output is correct
124 Correct 1923 ms 46696 KB Output is correct