This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |