Submission #105559

# Submission time Handle Problem Language Result Execution time Memory
105559 2019-04-13T13:29:17 Z zscoder Mergers (JOI19_mergers) C++17
34 / 100
3000 ms 28536 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]]--;
	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=1;i<n;i++)
	{
		//cerr<<"PAR : "<<i<<' '<<par[i]<<'\n';
		prep2(i,par[i]);
		int sum=0;
		for(int v:S) sum+=tot[v];
		if(siz[i]==sum) 
		{
			badsiz++;
			bad[i]=1;
			//cerr<<"BAD : "<<i<<'\n';
			//int cur=i;
			/*
			cerr<<"PAR HISTORY : ";
			while(cur>=0)
			{
				cerr<<cur<<' ';
				cur=par[cur];
			}
			cerr<<'\n';
			*/
		}
		S.clear();
	}
	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:63:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=1;i<adj[u].size();i++)
              ~^~~~~~~~~~~~~~
mergers.cpp:72: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 17 ms 15616 KB Output is correct
3 Correct 20 ms 15612 KB Output is correct
4 Correct 20 ms 15616 KB Output is correct
5 Correct 17 ms 15616 KB Output is correct
6 Correct 18 ms 15588 KB Output is correct
7 Correct 15 ms 13412 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 20 ms 15616 KB Output is correct
10 Correct 19 ms 15744 KB Output is correct
11 Correct 19 ms 15616 KB Output is correct
12 Correct 18 ms 15616 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 17 ms 15488 KB Output is correct
17 Correct 19 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 16 ms 15616 KB Output is correct
20 Correct 17 ms 15616 KB Output is correct
21 Correct 22 ms 15616 KB Output is correct
22 Correct 20 ms 15616 KB Output is correct
23 Correct 17 ms 15588 KB Output is correct
24 Correct 18 ms 15744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 17 ms 15616 KB Output is correct
3 Correct 20 ms 15612 KB Output is correct
4 Correct 20 ms 15616 KB Output is correct
5 Correct 17 ms 15616 KB Output is correct
6 Correct 18 ms 15588 KB Output is correct
7 Correct 15 ms 13412 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 20 ms 15616 KB Output is correct
10 Correct 19 ms 15744 KB Output is correct
11 Correct 19 ms 15616 KB Output is correct
12 Correct 18 ms 15616 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 17 ms 15488 KB Output is correct
17 Correct 19 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 16 ms 15616 KB Output is correct
20 Correct 17 ms 15616 KB Output is correct
21 Correct 22 ms 15616 KB Output is correct
22 Correct 20 ms 15616 KB Output is correct
23 Correct 17 ms 15588 KB Output is correct
24 Correct 18 ms 15744 KB Output is correct
25 Correct 18 ms 15588 KB Output is correct
26 Correct 29 ms 15872 KB Output is correct
27 Correct 29 ms 15744 KB Output is correct
28 Correct 454 ms 16052 KB Output is correct
29 Correct 31 ms 15992 KB Output is correct
30 Correct 25 ms 15744 KB Output is correct
31 Correct 15 ms 15616 KB Output is correct
32 Correct 470 ms 16244 KB Output is correct
33 Correct 15 ms 15616 KB Output is correct
34 Correct 18 ms 15744 KB Output is correct
35 Correct 28 ms 16000 KB Output is correct
36 Correct 27 ms 15772 KB Output is correct
37 Correct 33 ms 15872 KB Output is correct
38 Correct 20 ms 15616 KB Output is correct
39 Correct 26 ms 15836 KB Output is correct
40 Correct 20 ms 15744 KB Output is correct
41 Correct 22 ms 15868 KB Output is correct
42 Correct 19 ms 15872 KB Output is correct
43 Correct 130 ms 16052 KB Output is correct
44 Correct 18 ms 15488 KB Output is correct
45 Correct 216 ms 16120 KB Output is correct
46 Correct 35 ms 15872 KB Output is correct
47 Correct 18 ms 15736 KB Output is correct
48 Correct 26 ms 15744 KB Output is correct
49 Correct 22 ms 16000 KB Output is correct
50 Correct 231 ms 15980 KB Output is correct
51 Correct 24 ms 15872 KB Output is correct
52 Correct 21 ms 15744 KB Output is correct
53 Correct 21 ms 15916 KB Output is correct
54 Correct 26 ms 15744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 17 ms 15616 KB Output is correct
3 Correct 20 ms 15612 KB Output is correct
4 Correct 20 ms 15616 KB Output is correct
5 Correct 17 ms 15616 KB Output is correct
6 Correct 18 ms 15588 KB Output is correct
7 Correct 15 ms 13412 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 20 ms 15616 KB Output is correct
10 Correct 19 ms 15744 KB Output is correct
11 Correct 19 ms 15616 KB Output is correct
12 Correct 18 ms 15616 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 17 ms 15488 KB Output is correct
17 Correct 19 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 16 ms 15616 KB Output is correct
20 Correct 17 ms 15616 KB Output is correct
21 Correct 22 ms 15616 KB Output is correct
22 Correct 20 ms 15616 KB Output is correct
23 Correct 17 ms 15588 KB Output is correct
24 Correct 18 ms 15744 KB Output is correct
25 Correct 20 ms 15616 KB Output is correct
26 Correct 128 ms 22164 KB Output is correct
27 Correct 565 ms 21876 KB Output is correct
28 Correct 30 ms 15864 KB Output is correct
29 Correct 17 ms 15616 KB Output is correct
30 Correct 17 ms 15616 KB Output is correct
31 Correct 1479 ms 22276 KB Output is correct
32 Correct 21 ms 15744 KB Output is correct
33 Execution timed out 3077 ms 28536 KB Time limit exceeded
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 20732 KB Output is correct
2 Correct 188 ms 26240 KB Output is correct
3 Correct 28 ms 15872 KB Output is correct
4 Correct 26 ms 15744 KB Output is correct
5 Correct 21 ms 15616 KB Output is correct
6 Correct 15 ms 15616 KB Output is correct
7 Correct 19 ms 15872 KB Output is correct
8 Execution timed out 3100 ms 24756 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 15616 KB Output is correct
2 Correct 17 ms 15616 KB Output is correct
3 Correct 20 ms 15612 KB Output is correct
4 Correct 20 ms 15616 KB Output is correct
5 Correct 17 ms 15616 KB Output is correct
6 Correct 18 ms 15588 KB Output is correct
7 Correct 15 ms 13412 KB Output is correct
8 Correct 17 ms 15616 KB Output is correct
9 Correct 20 ms 15616 KB Output is correct
10 Correct 19 ms 15744 KB Output is correct
11 Correct 19 ms 15616 KB Output is correct
12 Correct 18 ms 15616 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 17 ms 15488 KB Output is correct
17 Correct 19 ms 15616 KB Output is correct
18 Correct 17 ms 15616 KB Output is correct
19 Correct 16 ms 15616 KB Output is correct
20 Correct 17 ms 15616 KB Output is correct
21 Correct 22 ms 15616 KB Output is correct
22 Correct 20 ms 15616 KB Output is correct
23 Correct 17 ms 15588 KB Output is correct
24 Correct 18 ms 15744 KB Output is correct
25 Correct 18 ms 15588 KB Output is correct
26 Correct 29 ms 15872 KB Output is correct
27 Correct 29 ms 15744 KB Output is correct
28 Correct 454 ms 16052 KB Output is correct
29 Correct 31 ms 15992 KB Output is correct
30 Correct 25 ms 15744 KB Output is correct
31 Correct 15 ms 15616 KB Output is correct
32 Correct 470 ms 16244 KB Output is correct
33 Correct 15 ms 15616 KB Output is correct
34 Correct 18 ms 15744 KB Output is correct
35 Correct 28 ms 16000 KB Output is correct
36 Correct 27 ms 15772 KB Output is correct
37 Correct 33 ms 15872 KB Output is correct
38 Correct 20 ms 15616 KB Output is correct
39 Correct 26 ms 15836 KB Output is correct
40 Correct 20 ms 15744 KB Output is correct
41 Correct 22 ms 15868 KB Output is correct
42 Correct 19 ms 15872 KB Output is correct
43 Correct 130 ms 16052 KB Output is correct
44 Correct 18 ms 15488 KB Output is correct
45 Correct 216 ms 16120 KB Output is correct
46 Correct 35 ms 15872 KB Output is correct
47 Correct 18 ms 15736 KB Output is correct
48 Correct 26 ms 15744 KB Output is correct
49 Correct 22 ms 16000 KB Output is correct
50 Correct 231 ms 15980 KB Output is correct
51 Correct 24 ms 15872 KB Output is correct
52 Correct 21 ms 15744 KB Output is correct
53 Correct 21 ms 15916 KB Output is correct
54 Correct 26 ms 15744 KB Output is correct
55 Correct 20 ms 15616 KB Output is correct
56 Correct 128 ms 22164 KB Output is correct
57 Correct 565 ms 21876 KB Output is correct
58 Correct 30 ms 15864 KB Output is correct
59 Correct 17 ms 15616 KB Output is correct
60 Correct 17 ms 15616 KB Output is correct
61 Correct 1479 ms 22276 KB Output is correct
62 Correct 21 ms 15744 KB Output is correct
63 Execution timed out 3077 ms 28536 KB Time limit exceeded
64 Halted 0 ms 0 KB -