Submission #218090

# Submission time Handle Problem Language Result Execution time Memory
218090 2020-04-01T07:38:30 Z ryansee Capital City (JOI20_capital_city) C++14
100 / 100
1138 ms 41868 KB
#include "bits/stdc++.h"
using namespace std;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());    //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return (rng() % (y+1-x)) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }

typedef long long ll; 
typedef long double ld;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
typedef pair<ll,ll>pi; typedef pair<ll,pi>spi; typedef pair<pi,pi>dpi;

#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (200006)
ll n,k,C[MAXN],total,sz[MAXN],ans[MAXN],colour[MAXN],par[MAXN];
vector<int>v[MAXN],act[MAXN];
bitset<MAXN>done,vis;
void dfs1(int x,int p){
	++ total;
	sz[x]=1;
	for(auto i:v[x]) if(!done[i]) if(i^p) {
		dfs1(i,x);
		sz[x]+=sz[i];
	}
}
ll dfs2(int x,int p){
	for(auto i:v[x]) if(!done[i]&&i!=p&&sz[i]>total/2) return dfs2(i,x);
	return x;
}
void count(int x,int p){
	++ colour[C[x]];
	par[x]=p;
	for(auto i:v[x]) if(!done[i]) if(i^p) count(i,x);
}
void reset(int x,int p){
	colour[C[x]]=vis[C[x]]=0;
	for(auto i:v[x]) if(!done[i]) if(i^p) reset(i,x);
}
bool valid(ll c) {
	return colour[c] == siz(act[c]);
}
void dfs3(int x,int p){
	total=0;
	dfs1(x,p);
	x=dfs2(x,p);
	count(x,x);
	bool can=1;
	queue<ll> q;
	q.emplace(C[x]);
	vis[C[x]]=1;
	ans[x]=0;
	while(q.size()){
		ll c=q.front();
		q.pop();
		if(!valid(c)){
			can=0;
			break;
		}
		++ ans[x];
		for(auto i:act[c]) if(C[par[i]]^C[i]) if(!vis[C[par[i]]]) {
			q.emplace(C[par[i]]), vis[C[par[i]]]=1;
		}
	}
	if(!can) ans[x]=LLINF;
	else -- ans[x];
	reset(x,p);
	done[x]=1;
	for(auto i:v[x]) if(!done[i]) dfs3(i,x);
}
int main(){
	FAST
	cin>>n>>k;
	FOR(i,1,n-1){
		ll a,b;cin>>a>>b;
		v[a].eb(b), v[b].eb(a);
	}
	FOR(i,1,n)cin>>C[i],act[C[i]].pb(i);
	dfs3(1,-1);
	cout<<*min_element(ans+1,ans+n+1)<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 12 ms 9984 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 12 ms 9944 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 16 ms 10112 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 13 ms 10112 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
27 Correct 13 ms 10036 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 730 ms 41592 KB Output is correct
2 Correct 296 ms 41868 KB Output is correct
3 Correct 780 ms 41100 KB Output is correct
4 Correct 371 ms 41812 KB Output is correct
5 Correct 759 ms 38068 KB Output is correct
6 Correct 280 ms 41848 KB Output is correct
7 Correct 844 ms 38268 KB Output is correct
8 Correct 277 ms 41376 KB Output is correct
9 Correct 862 ms 36336 KB Output is correct
10 Correct 888 ms 33972 KB Output is correct
11 Correct 867 ms 36884 KB Output is correct
12 Correct 905 ms 39468 KB Output is correct
13 Correct 909 ms 33444 KB Output is correct
14 Correct 992 ms 39752 KB Output is correct
15 Correct 962 ms 39416 KB Output is correct
16 Correct 922 ms 34440 KB Output is correct
17 Correct 841 ms 35064 KB Output is correct
18 Correct 849 ms 35320 KB Output is correct
19 Correct 843 ms 38392 KB Output is correct
20 Correct 855 ms 40696 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9856 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 10 ms 9856 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 10 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 12 ms 9984 KB Output is correct
15 Correct 12 ms 9984 KB Output is correct
16 Correct 12 ms 9984 KB Output is correct
17 Correct 11 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 12 ms 9944 KB Output is correct
21 Correct 11 ms 9984 KB Output is correct
22 Correct 16 ms 10112 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 12 ms 9984 KB Output is correct
25 Correct 13 ms 10112 KB Output is correct
26 Correct 12 ms 10112 KB Output is correct
27 Correct 13 ms 10036 KB Output is correct
28 Correct 12 ms 9984 KB Output is correct
29 Correct 12 ms 9984 KB Output is correct
30 Correct 12 ms 9984 KB Output is correct
31 Correct 730 ms 41592 KB Output is correct
32 Correct 296 ms 41868 KB Output is correct
33 Correct 780 ms 41100 KB Output is correct
34 Correct 371 ms 41812 KB Output is correct
35 Correct 759 ms 38068 KB Output is correct
36 Correct 280 ms 41848 KB Output is correct
37 Correct 844 ms 38268 KB Output is correct
38 Correct 277 ms 41376 KB Output is correct
39 Correct 862 ms 36336 KB Output is correct
40 Correct 888 ms 33972 KB Output is correct
41 Correct 867 ms 36884 KB Output is correct
42 Correct 905 ms 39468 KB Output is correct
43 Correct 909 ms 33444 KB Output is correct
44 Correct 992 ms 39752 KB Output is correct
45 Correct 962 ms 39416 KB Output is correct
46 Correct 922 ms 34440 KB Output is correct
47 Correct 841 ms 35064 KB Output is correct
48 Correct 849 ms 35320 KB Output is correct
49 Correct 843 ms 38392 KB Output is correct
50 Correct 855 ms 40696 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 567 ms 24364 KB Output is correct
53 Correct 598 ms 24312 KB Output is correct
54 Correct 584 ms 24568 KB Output is correct
55 Correct 598 ms 24416 KB Output is correct
56 Correct 603 ms 24312 KB Output is correct
57 Correct 581 ms 24312 KB Output is correct
58 Correct 626 ms 28152 KB Output is correct
59 Correct 557 ms 28572 KB Output is correct
60 Correct 701 ms 27916 KB Output is correct
61 Correct 766 ms 27640 KB Output is correct
62 Correct 277 ms 41848 KB Output is correct
63 Correct 291 ms 41848 KB Output is correct
64 Correct 295 ms 41596 KB Output is correct
65 Correct 274 ms 41848 KB Output is correct
66 Correct 468 ms 32900 KB Output is correct
67 Correct 483 ms 33016 KB Output is correct
68 Correct 486 ms 32880 KB Output is correct
69 Correct 503 ms 33004 KB Output is correct
70 Correct 493 ms 33300 KB Output is correct
71 Correct 495 ms 32880 KB Output is correct
72 Correct 529 ms 32880 KB Output is correct
73 Correct 556 ms 32112 KB Output is correct
74 Correct 564 ms 33112 KB Output is correct
75 Correct 518 ms 33012 KB Output is correct
76 Correct 793 ms 31948 KB Output is correct
77 Correct 768 ms 30200 KB Output is correct
78 Correct 862 ms 34936 KB Output is correct
79 Correct 823 ms 33020 KB Output is correct
80 Correct 1138 ms 40184 KB Output is correct
81 Correct 807 ms 36600 KB Output is correct
82 Correct 805 ms 36600 KB Output is correct
83 Correct 813 ms 33272 KB Output is correct
84 Correct 841 ms 39288 KB Output is correct
85 Correct 840 ms 37624 KB Output is correct
86 Correct 850 ms 32888 KB Output is correct
87 Correct 807 ms 34808 KB Output is correct
88 Correct 693 ms 35704 KB Output is correct
89 Correct 685 ms 31736 KB Output is correct
90 Correct 756 ms 31616 KB Output is correct
91 Correct 686 ms 34040 KB Output is correct
92 Correct 701 ms 32632 KB Output is correct
93 Correct 675 ms 32248 KB Output is correct
94 Correct 676 ms 31608 KB Output is correct
95 Correct 700 ms 33144 KB Output is correct
96 Correct 702 ms 31992 KB Output is correct
97 Correct 684 ms 33912 KB Output is correct