Submission #405100

# Submission time Handle Problem Language Result Execution time Memory
405100 2021-05-15T17:24:46 Z ryansee Capital City (JOI20_capital_city) C++14
100 / 100
1087 ms 42468 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());
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive
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); }

using ll=long long;
using ld=long double;
#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)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>; 

long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (200006)
ll n, k, C[MAXN], ans, legit[MAXN];
vector<int> v[MAXN];

int total, sz[MAXN];
bitset<MAXN> done;
void dfs1(int x,int p) {
	++ total, sz[x] = 1;
	for(auto i:v[x]) if(i^p && !done[i]) dfs1(i, x), sz[x] += sz[i];
}
int dfs2(int x,int p) {
	for(auto i:v[x]) if(i^p && !done[i] && sz[i]>total/2) return dfs2(i, x);
	return x;
}
vector<int> ppl[MAXN];
bitset<MAXN> vis;
int P[MAXN];
void solve(int x) {
	function<void(int,int)>dfs=[&](int x,int p) {
		P[x]=p, ppl[C[x]].eb(x);
		for(auto i:v[x]) if(i^p && !done[i]) dfs(i, x);
	};
	dfs(x, x);
	
	queue<int> q;
	ll sum = 0;
	if(ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
	for(auto i:ppl[C[x]]) vis[i]=1, q.emplace(i);
	ppl[C[x]].clear();
	while(q.size()) {
		int x = q.front();
		q.pop();
		sum += !ppl[C[x]].empty();
		if(ppl[C[x]].size() && ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
		while(ppl[C[x]].size()) {
			if(!vis[ppl[C[x]].back()]) vis[ppl[C[x]].back()]=1, q.emplace(ppl[C[x]].back());
			ppl[C[x]].pop_back();
		}
		if(!vis[P[x]]) vis[P[x]]=1, q.emplace(P[x]);
	}
	
	function<void(int,int)>dfs2=[&](int x,int p) {
		vis[x] = 0, ppl[C[x]].clear();
		for(auto i:v[x]) if(i^p && !done[i]) dfs2(i, x);
	};
	dfs2(x, x);
	
	ans = min(ans, sum);
}
void dfs3(int x) {
	total = 0;
	dfs1(x, x);
	x = dfs2(x, x);
	solve(x);
	done[x] = 1;
	for(auto i:v[x]) if(!done[i]) dfs3(i);
}

int main() {
	FAST
	cin>>n>>k, ans=k-1;
	FOR(i,1,n-1) {
		int a, b;
		cin>>a>>b;
		v[a].eb(b), v[b].eb(a);
	}
	FOR(i,1,n) cin>>C[i], ++legit[C[i]];
	dfs3(1);
	cout<<ans<<'\n';
}

Compilation message

capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:54:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   54 |  if(ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
capital_city.cpp:61:43: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   61 |   if(ppl[C[x]].size() && ppl[C[x]].size() != legit[C[x]]) sum = LLINF;
      |                          ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9716 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9716 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
11 Correct 9 ms 9804 KB Output is correct
12 Correct 9 ms 9828 KB Output is correct
13 Correct 8 ms 9804 KB Output is correct
14 Correct 9 ms 9804 KB Output is correct
15 Correct 9 ms 9932 KB Output is correct
16 Correct 10 ms 9932 KB Output is correct
17 Correct 9 ms 9964 KB Output is correct
18 Correct 8 ms 9932 KB Output is correct
19 Correct 8 ms 10060 KB Output is correct
20 Correct 8 ms 9888 KB Output is correct
21 Correct 8 ms 9956 KB Output is correct
22 Correct 9 ms 10012 KB Output is correct
23 Correct 9 ms 9932 KB Output is correct
24 Correct 11 ms 9932 KB Output is correct
25 Correct 10 ms 9872 KB Output is correct
26 Correct 9 ms 10012 KB Output is correct
27 Correct 9 ms 10012 KB Output is correct
28 Correct 10 ms 9952 KB Output is correct
29 Correct 10 ms 9892 KB Output is correct
30 Correct 9 ms 9868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 886 ms 38212 KB Output is correct
2 Correct 298 ms 38788 KB Output is correct
3 Correct 902 ms 37764 KB Output is correct
4 Correct 289 ms 38596 KB Output is correct
5 Correct 931 ms 34896 KB Output is correct
6 Correct 306 ms 42396 KB Output is correct
7 Correct 875 ms 38992 KB Output is correct
8 Correct 304 ms 42068 KB Output is correct
9 Correct 1059 ms 36892 KB Output is correct
10 Correct 1022 ms 34604 KB Output is correct
11 Correct 1029 ms 37416 KB Output is correct
12 Correct 1041 ms 39900 KB Output is correct
13 Correct 1020 ms 33980 KB Output is correct
14 Correct 1044 ms 40260 KB Output is correct
15 Correct 1055 ms 39920 KB Output is correct
16 Correct 1045 ms 35012 KB Output is correct
17 Correct 1025 ms 35476 KB Output is correct
18 Correct 1029 ms 35600 KB Output is correct
19 Correct 1038 ms 38712 KB Output is correct
20 Correct 1026 ms 41156 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 6 ms 9676 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9676 KB Output is correct
7 Correct 6 ms 9716 KB Output is correct
8 Correct 6 ms 9676 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9732 KB Output is correct
11 Correct 9 ms 9804 KB Output is correct
12 Correct 9 ms 9828 KB Output is correct
13 Correct 8 ms 9804 KB Output is correct
14 Correct 9 ms 9804 KB Output is correct
15 Correct 9 ms 9932 KB Output is correct
16 Correct 10 ms 9932 KB Output is correct
17 Correct 9 ms 9964 KB Output is correct
18 Correct 8 ms 9932 KB Output is correct
19 Correct 8 ms 10060 KB Output is correct
20 Correct 8 ms 9888 KB Output is correct
21 Correct 8 ms 9956 KB Output is correct
22 Correct 9 ms 10012 KB Output is correct
23 Correct 9 ms 9932 KB Output is correct
24 Correct 11 ms 9932 KB Output is correct
25 Correct 10 ms 9872 KB Output is correct
26 Correct 9 ms 10012 KB Output is correct
27 Correct 9 ms 10012 KB Output is correct
28 Correct 10 ms 9952 KB Output is correct
29 Correct 10 ms 9892 KB Output is correct
30 Correct 9 ms 9868 KB Output is correct
31 Correct 886 ms 38212 KB Output is correct
32 Correct 298 ms 38788 KB Output is correct
33 Correct 902 ms 37764 KB Output is correct
34 Correct 289 ms 38596 KB Output is correct
35 Correct 931 ms 34896 KB Output is correct
36 Correct 306 ms 42396 KB Output is correct
37 Correct 875 ms 38992 KB Output is correct
38 Correct 304 ms 42068 KB Output is correct
39 Correct 1059 ms 36892 KB Output is correct
40 Correct 1022 ms 34604 KB Output is correct
41 Correct 1029 ms 37416 KB Output is correct
42 Correct 1041 ms 39900 KB Output is correct
43 Correct 1020 ms 33980 KB Output is correct
44 Correct 1044 ms 40260 KB Output is correct
45 Correct 1055 ms 39920 KB Output is correct
46 Correct 1045 ms 35012 KB Output is correct
47 Correct 1025 ms 35476 KB Output is correct
48 Correct 1029 ms 35600 KB Output is correct
49 Correct 1038 ms 38712 KB Output is correct
50 Correct 1026 ms 41156 KB Output is correct
51 Correct 7 ms 9676 KB Output is correct
52 Correct 667 ms 24940 KB Output is correct
53 Correct 724 ms 24900 KB Output is correct
54 Correct 721 ms 24900 KB Output is correct
55 Correct 726 ms 25028 KB Output is correct
56 Correct 732 ms 24864 KB Output is correct
57 Correct 746 ms 24884 KB Output is correct
58 Correct 675 ms 28712 KB Output is correct
59 Correct 647 ms 29028 KB Output is correct
60 Correct 836 ms 28360 KB Output is correct
61 Correct 984 ms 28180 KB Output is correct
62 Correct 290 ms 42384 KB Output is correct
63 Correct 316 ms 42468 KB Output is correct
64 Correct 308 ms 42068 KB Output is correct
65 Correct 296 ms 42268 KB Output is correct
66 Correct 515 ms 33976 KB Output is correct
67 Correct 527 ms 33672 KB Output is correct
68 Correct 539 ms 33788 KB Output is correct
69 Correct 517 ms 33880 KB Output is correct
70 Correct 518 ms 33756 KB Output is correct
71 Correct 530 ms 33804 KB Output is correct
72 Correct 512 ms 33784 KB Output is correct
73 Correct 510 ms 32956 KB Output is correct
74 Correct 521 ms 33852 KB Output is correct
75 Correct 514 ms 33836 KB Output is correct
76 Correct 792 ms 32412 KB Output is correct
77 Correct 724 ms 30532 KB Output is correct
78 Correct 1036 ms 35380 KB Output is correct
79 Correct 1040 ms 33408 KB Output is correct
80 Correct 978 ms 40588 KB Output is correct
81 Correct 1024 ms 37164 KB Output is correct
82 Correct 1057 ms 37200 KB Output is correct
83 Correct 1087 ms 33860 KB Output is correct
84 Correct 1072 ms 39684 KB Output is correct
85 Correct 1045 ms 38140 KB Output is correct
86 Correct 1054 ms 33500 KB Output is correct
87 Correct 987 ms 35220 KB Output is correct
88 Correct 909 ms 36292 KB Output is correct
89 Correct 930 ms 32324 KB Output is correct
90 Correct 912 ms 32340 KB Output is correct
91 Correct 905 ms 34584 KB Output is correct
92 Correct 887 ms 33092 KB Output is correct
93 Correct 914 ms 32832 KB Output is correct
94 Correct 882 ms 32192 KB Output is correct
95 Correct 892 ms 33836 KB Output is correct
96 Correct 944 ms 32244 KB Output is correct
97 Correct 921 ms 34500 KB Output is correct