Submission #895445

# Submission time Handle Problem Language Result Execution time Memory
895445 2023-12-30T00:35:51 Z thunopro Capital City (JOI20_capital_city) C++14
100 / 100
178 ms 50368 KB
#include<bits/stdc++.h>
using namespace std ; 
#define ll long long 
#define maxn 200009 
#define fi first 
#define se second 
#define pb push_back 
#define left id<<1 
#define right id<<1|1 
#define re exit(0); 
#define _lower(v,x) lower_bound(v.begin(),v.end(),x)-v.begin()+1  

const int mod = 1e9+7 ; 
const int INF = 1e9 ; 
const int LOG = 18 ; 

typedef vector<int> vi ; 
typedef pair<int,int> pii ; 
typedef vector<ll> vl ; 
typedef vector<pii> vii ; 

void add ( int &a , int b ) 
{
	a += b ;
	if ( a < 0 ) a += mod ; 
	if ( a >= mod ) a -= mod ; 
}

template < typename T > void chkmin ( T &a , T b ) { if ( a > b ) a = b ; } 
template < typename T > void chkmax ( T &a , T b ) { if ( a < b ) a = b ; } 

void rf () 
{
	freopen ("bai1.inp","r",stdin) ; 
//	freopen ("bai1.out","w",stdout) ; 
}

int _pow ( int a , int n ) 
{
	if ( n == 0 ) return 1 ; 
	int res = _pow (a,n/2) ; 
	if ( n % 2 ) return 1ll*res*res%mod*a%mod ; 
	else return 1ll*res*res%mod ; 
}

int n , k ; 
int colour [maxn] , num_colour [maxn] ; 

vi adjList [maxn] ; 
vi _adjList [maxn] ; 
void add_edge ( int u , int v ) 
{
	_adjList [u] . pb (v) ; 
}

int sz [maxn] , bigChild [maxn] , ver [maxn] ; 
int tin [maxn] , tout [maxn] , timeDfs ; 
void dfs ( int u = 1 , int par = 1 ) 
{
	tin [u] = ++ timeDfs ; 
	ver [timeDfs] = u ; 
	sz [u] = 1 ; bigChild [u] = - 1 ; 
	for ( auto v : adjList [u] ) 
	{
		if ( v == par ) continue ; 
		dfs (v,u) ; 
		sz [u] += sz [v] ; 
		if ( bigChild[u] == - 1 || sz[bigChild[u]] < sz [v] ) bigChild [u] = v ; 
	}
	tout [u] = timeDfs ; 
}

int cnt [maxn] ; 

void add_colour ( int x , int y ) 
{
	cnt [x] += y ; 	
}
void dsu_on_tree ( int u = 1 , int par = 1 , bool keep = true ) 
{
	for ( auto v : adjList [u] ) 
	{
		if ( v != par && v != bigChild[u] ) dsu_on_tree (v,u,false) ; 
	}
	if ( bigChild [u] != -1 ) dsu_on_tree (bigChild[u],u,true) ; 
	
	for ( auto v : adjList [u] ) 
	{
		if ( v == par || v == bigChild[u] ) continue ; 
		for ( int i = tin [v] ; i <= tout [v] ; i ++ ) add_colour (colour[ver[i]],1) ; 
	}
	
	add_colour (colour[u],1) ;
	if ( cnt[colour[u]] != num_colour [colour[u]] && u != 1 ) add_edge (colour[u],colour[par]) ; 
	if ( !keep ) 
	{
		for ( int i = tin [u] ; i <= tout [u] ; i ++ ) add_colour (colour[ver[i]],-1) ; 
	}
}

int low [maxn] , num [maxn] ;  
int sz_gr [maxn] , gr [maxn] , num_gr ; stack <int> stk ; 
void tarjan ( int u , int par = -1 )
{
	stk . push (u) ; 
	low [u] = num [u] = ++ timeDfs ; 
	for ( auto v : adjList [u] ) 
	{
		if ( !num[v] ) 
		{
			tarjan (v,u) ; 
			chkmin (low[u],low[v]) ; 
		}
		else chkmin (low[u],num[v]) ; 
	}
	if ( low [u] == num [u] )
	{
		num_gr ++ ; 
		while ( true ) 
		{
			int v = stk.top() ; stk.pop() ; 
			gr [v] = num_gr ; sz_gr [num_gr] ++ ; 
			num [v] = low [v] = INF ; 
			if ( u == v ) break ; 
		}
	}
}

int deg [maxn] ; 
void get_answer () 
{
	timeDfs = 0 ; 	
	for ( int i = 1 ; i <= k ; i ++ ) 
	{
		adjList [i] = _adjList [i] , _adjList [i].clear () ; 
		sort (adjList[i].begin(),adjList[i].end()) ; 
		adjList[i].resize(unique(adjList[i].begin(),adjList[i].end())-adjList[i].begin()) ; 
	}
	for ( int i = 1 ; i <= k ; i ++ ) if ( !num[i] ) tarjan (i) ; 
	for ( int i = 1 ; i <= k ; i ++ ) 
	{
		for ( auto x : adjList [i] ) 
		{
			int u = gr [x] , v = gr [i] ; 
			if ( u != v ) deg [v] ++ ; 
		}
	}
	int res = INF ; 
	for ( int i = 1 ; i <= num_gr ; i ++ ) if ( ! deg [i] ) res = min (res,sz_gr [i]) ; 
	cout << res - 1 ; 
}

int main () 
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);cout.tie(0) ; 
//	rf () ; 
	
	cin >> n >> k ; 
	for ( int i = 1 ; i < n ; i ++ ) 
	{
		int u , v ; cin >> u >> v ; 
		adjList [u] . pb (v) ; 
		adjList [v] . pb (u) ; 
	}
	
	for ( int i = 1 ; i <= n ; i ++ ) cin >> colour [i] , num_colour [colour[i]] ++ ; 
		
	dfs () ; 
	dsu_on_tree () ;
	get_answer () ; 
	
}

Compilation message

capital_city.cpp: In function 'void rf()':
capital_city.cpp:34:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |  freopen ("bai1.inp","r",stdin) ;
      |  ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 4 ms 14868 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 4 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 4 ms 14868 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 4 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 4 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14936 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 15092 KB Output is correct
22 Correct 4 ms 15196 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 4 ms 14936 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 4 ms 14968 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 4 ms 14940 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 145 ms 49832 KB Output is correct
2 Correct 78 ms 50304 KB Output is correct
3 Correct 139 ms 49488 KB Output is correct
4 Correct 76 ms 50364 KB Output is correct
5 Correct 141 ms 46060 KB Output is correct
6 Correct 80 ms 50368 KB Output is correct
7 Correct 122 ms 46204 KB Output is correct
8 Correct 77 ms 49876 KB Output is correct
9 Correct 129 ms 44156 KB Output is correct
10 Correct 144 ms 41044 KB Output is correct
11 Correct 120 ms 44484 KB Output is correct
12 Correct 144 ms 47508 KB Output is correct
13 Correct 138 ms 40388 KB Output is correct
14 Correct 117 ms 47952 KB Output is correct
15 Correct 116 ms 47584 KB Output is correct
16 Correct 117 ms 41556 KB Output is correct
17 Correct 137 ms 42588 KB Output is correct
18 Correct 138 ms 42344 KB Output is correct
19 Correct 123 ms 46180 KB Output is correct
20 Correct 139 ms 48976 KB Output is correct
21 Correct 3 ms 14680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 14680 KB Output is correct
2 Correct 4 ms 14868 KB Output is correct
3 Correct 3 ms 14684 KB Output is correct
4 Correct 3 ms 14680 KB Output is correct
5 Correct 3 ms 14684 KB Output is correct
6 Correct 4 ms 14684 KB Output is correct
7 Correct 3 ms 14684 KB Output is correct
8 Correct 3 ms 14684 KB Output is correct
9 Correct 3 ms 14684 KB Output is correct
10 Correct 3 ms 14684 KB Output is correct
11 Correct 4 ms 14936 KB Output is correct
12 Correct 3 ms 14940 KB Output is correct
13 Correct 4 ms 14940 KB Output is correct
14 Correct 4 ms 14940 KB Output is correct
15 Correct 4 ms 14940 KB Output is correct
16 Correct 3 ms 14940 KB Output is correct
17 Correct 3 ms 14940 KB Output is correct
18 Correct 3 ms 14936 KB Output is correct
19 Correct 3 ms 14940 KB Output is correct
20 Correct 4 ms 14940 KB Output is correct
21 Correct 4 ms 15092 KB Output is correct
22 Correct 4 ms 15196 KB Output is correct
23 Correct 3 ms 14940 KB Output is correct
24 Correct 4 ms 14936 KB Output is correct
25 Correct 4 ms 14940 KB Output is correct
26 Correct 4 ms 14968 KB Output is correct
27 Correct 3 ms 14936 KB Output is correct
28 Correct 3 ms 14940 KB Output is correct
29 Correct 4 ms 14940 KB Output is correct
30 Correct 4 ms 14940 KB Output is correct
31 Correct 145 ms 49832 KB Output is correct
32 Correct 78 ms 50304 KB Output is correct
33 Correct 139 ms 49488 KB Output is correct
34 Correct 76 ms 50364 KB Output is correct
35 Correct 141 ms 46060 KB Output is correct
36 Correct 80 ms 50368 KB Output is correct
37 Correct 122 ms 46204 KB Output is correct
38 Correct 77 ms 49876 KB Output is correct
39 Correct 129 ms 44156 KB Output is correct
40 Correct 144 ms 41044 KB Output is correct
41 Correct 120 ms 44484 KB Output is correct
42 Correct 144 ms 47508 KB Output is correct
43 Correct 138 ms 40388 KB Output is correct
44 Correct 117 ms 47952 KB Output is correct
45 Correct 116 ms 47584 KB Output is correct
46 Correct 117 ms 41556 KB Output is correct
47 Correct 137 ms 42588 KB Output is correct
48 Correct 138 ms 42344 KB Output is correct
49 Correct 123 ms 46180 KB Output is correct
50 Correct 139 ms 48976 KB Output is correct
51 Correct 3 ms 14680 KB Output is correct
52 Correct 170 ms 30540 KB Output is correct
53 Correct 122 ms 30716 KB Output is correct
54 Correct 137 ms 30648 KB Output is correct
55 Correct 128 ms 30720 KB Output is correct
56 Correct 113 ms 30548 KB Output is correct
57 Correct 122 ms 30584 KB Output is correct
58 Correct 108 ms 33616 KB Output is correct
59 Correct 141 ms 34380 KB Output is correct
60 Correct 122 ms 33616 KB Output is correct
61 Correct 151 ms 33108 KB Output is correct
62 Correct 77 ms 50260 KB Output is correct
63 Correct 79 ms 50228 KB Output is correct
64 Correct 79 ms 49896 KB Output is correct
65 Correct 82 ms 50172 KB Output is correct
66 Correct 122 ms 40008 KB Output is correct
67 Correct 102 ms 40016 KB Output is correct
68 Correct 94 ms 40164 KB Output is correct
69 Correct 106 ms 40236 KB Output is correct
70 Correct 124 ms 40080 KB Output is correct
71 Correct 94 ms 40012 KB Output is correct
72 Correct 96 ms 40144 KB Output is correct
73 Correct 116 ms 39164 KB Output is correct
74 Correct 117 ms 40012 KB Output is correct
75 Correct 99 ms 40012 KB Output is correct
76 Correct 119 ms 38400 KB Output is correct
77 Correct 115 ms 36224 KB Output is correct
78 Correct 119 ms 42276 KB Output is correct
79 Correct 126 ms 39776 KB Output is correct
80 Correct 128 ms 48264 KB Output is correct
81 Correct 144 ms 44192 KB Output is correct
82 Correct 118 ms 44372 KB Output is correct
83 Correct 116 ms 40536 KB Output is correct
84 Correct 156 ms 47276 KB Output is correct
85 Correct 118 ms 45400 KB Output is correct
86 Correct 126 ms 40020 KB Output is correct
87 Correct 129 ms 41948 KB Output is correct
88 Correct 116 ms 43336 KB Output is correct
89 Correct 125 ms 38484 KB Output is correct
90 Correct 145 ms 38480 KB Output is correct
91 Correct 137 ms 41044 KB Output is correct
92 Correct 178 ms 39348 KB Output is correct
93 Correct 157 ms 39176 KB Output is correct
94 Correct 172 ms 38308 KB Output is correct
95 Correct 157 ms 40276 KB Output is correct
96 Correct 145 ms 38576 KB Output is correct
97 Correct 138 ms 41128 KB Output is correct