Submission #569280

# Submission time Handle Problem Language Result Execution time Memory
569280 2022-05-27T08:26:33 Z hgj123 Capital City (JOI20_capital_city) C++14
100 / 100
719 ms 42068 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)
int n, k, C[MAXN], ans, sz[MAXN], total, label[MAXN], vis[MAXN], co, p[MAXN];
vector<int> v[MAXN], c[MAXN];
bitset<MAXN> done;
void dfs1(int x,int p) {
	++ total, sz[x] = 1, label[x] = co;
	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;
}
void hehe(int x,int P) {
	p[x] = P;
	for(auto i:v[x]) if(i^P && !done[i]) hehe(i, x);
}
void dfs3(int x) {
	total = 0, ++ co, dfs1(x, x);
	x = dfs2(x, x);
	hehe(x, x);
	int mi = 0;
	queue<int> q;
	for(auto i:c[C[x]]) {
		if(label[i] != co) {
			mi = INF;
			break;
		}
		vis[i]=co, q.emplace(i);
	}
	while(mi < ans && q.size()) {
		int x = q.front();
		q.pop();
		if(vis[p[x]] == co) continue;
		int y=p[x];
		++ mi;
		for(auto i:c[C[y]]) {
			if(label[i] != co) {
				mi = INF;
				break;
			}
			vis[i] = co, q.emplace(i);
		}
	}
	ans = min(ans, mi);
	done[x] = 1;
	for(auto i:v[x]) if(!done[i]) dfs3(i);
}
int main() {
	FAST
	cin>>n>>k;
	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], c[C[i]].eb(i);
	ans=k-1, dfs3(1);
	cout<<ans<<'\n';
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9732 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 7 ms 9736 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9732 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 7 ms 9736 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 8 ms 9888 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 10 ms 9788 KB Output is correct
14 Correct 8 ms 9872 KB Output is correct
15 Correct 9 ms 9940 KB Output is correct
16 Correct 7 ms 9920 KB Output is correct
17 Correct 8 ms 9872 KB Output is correct
18 Correct 9 ms 9940 KB Output is correct
19 Correct 7 ms 9880 KB Output is correct
20 Correct 8 ms 9920 KB Output is correct
21 Correct 8 ms 9940 KB Output is correct
22 Correct 9 ms 10068 KB Output is correct
23 Correct 8 ms 9940 KB Output is correct
24 Correct 7 ms 9928 KB Output is correct
25 Correct 10 ms 9940 KB Output is correct
26 Correct 8 ms 10000 KB Output is correct
27 Correct 8 ms 9940 KB Output is correct
28 Correct 10 ms 9916 KB Output is correct
29 Correct 9 ms 9872 KB Output is correct
30 Correct 9 ms 9924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 544 ms 41880 KB Output is correct
2 Correct 199 ms 41984 KB Output is correct
3 Correct 573 ms 41548 KB Output is correct
4 Correct 193 ms 41984 KB Output is correct
5 Correct 551 ms 38584 KB Output is correct
6 Correct 209 ms 42068 KB Output is correct
7 Correct 550 ms 38920 KB Output is correct
8 Correct 209 ms 41952 KB Output is correct
9 Correct 598 ms 37276 KB Output is correct
10 Correct 569 ms 34864 KB Output is correct
11 Correct 588 ms 37724 KB Output is correct
12 Correct 653 ms 40464 KB Output is correct
13 Correct 581 ms 34380 KB Output is correct
14 Correct 599 ms 40684 KB Output is correct
15 Correct 593 ms 40328 KB Output is correct
16 Correct 649 ms 35360 KB Output is correct
17 Correct 649 ms 35848 KB Output is correct
18 Correct 617 ms 36084 KB Output is correct
19 Correct 719 ms 39164 KB Output is correct
20 Correct 622 ms 41604 KB Output is correct
21 Correct 6 ms 9736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9732 KB Output is correct
6 Correct 8 ms 9684 KB Output is correct
7 Correct 7 ms 9732 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 7 ms 9736 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 8 ms 9888 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 10 ms 9788 KB Output is correct
14 Correct 8 ms 9872 KB Output is correct
15 Correct 9 ms 9940 KB Output is correct
16 Correct 7 ms 9920 KB Output is correct
17 Correct 8 ms 9872 KB Output is correct
18 Correct 9 ms 9940 KB Output is correct
19 Correct 7 ms 9880 KB Output is correct
20 Correct 8 ms 9920 KB Output is correct
21 Correct 8 ms 9940 KB Output is correct
22 Correct 9 ms 10068 KB Output is correct
23 Correct 8 ms 9940 KB Output is correct
24 Correct 7 ms 9928 KB Output is correct
25 Correct 10 ms 9940 KB Output is correct
26 Correct 8 ms 10000 KB Output is correct
27 Correct 8 ms 9940 KB Output is correct
28 Correct 10 ms 9916 KB Output is correct
29 Correct 9 ms 9872 KB Output is correct
30 Correct 9 ms 9924 KB Output is correct
31 Correct 544 ms 41880 KB Output is correct
32 Correct 199 ms 41984 KB Output is correct
33 Correct 573 ms 41548 KB Output is correct
34 Correct 193 ms 41984 KB Output is correct
35 Correct 551 ms 38584 KB Output is correct
36 Correct 209 ms 42068 KB Output is correct
37 Correct 550 ms 38920 KB Output is correct
38 Correct 209 ms 41952 KB Output is correct
39 Correct 598 ms 37276 KB Output is correct
40 Correct 569 ms 34864 KB Output is correct
41 Correct 588 ms 37724 KB Output is correct
42 Correct 653 ms 40464 KB Output is correct
43 Correct 581 ms 34380 KB Output is correct
44 Correct 599 ms 40684 KB Output is correct
45 Correct 593 ms 40328 KB Output is correct
46 Correct 649 ms 35360 KB Output is correct
47 Correct 649 ms 35848 KB Output is correct
48 Correct 617 ms 36084 KB Output is correct
49 Correct 719 ms 39164 KB Output is correct
50 Correct 622 ms 41604 KB Output is correct
51 Correct 6 ms 9736 KB Output is correct
52 Correct 430 ms 25716 KB Output is correct
53 Correct 482 ms 25696 KB Output is correct
54 Correct 461 ms 25684 KB Output is correct
55 Correct 458 ms 25724 KB Output is correct
56 Correct 461 ms 25696 KB Output is correct
57 Correct 453 ms 25704 KB Output is correct
58 Correct 467 ms 28864 KB Output is correct
59 Correct 358 ms 29216 KB Output is correct
60 Correct 456 ms 28452 KB Output is correct
61 Correct 553 ms 28240 KB Output is correct
62 Correct 201 ms 41976 KB Output is correct
63 Correct 221 ms 41888 KB Output is correct
64 Correct 227 ms 41796 KB Output is correct
65 Correct 201 ms 42000 KB Output is correct
66 Correct 408 ms 34256 KB Output is correct
67 Correct 335 ms 34104 KB Output is correct
68 Correct 335 ms 34220 KB Output is correct
69 Correct 340 ms 34292 KB Output is correct
70 Correct 351 ms 34152 KB Output is correct
71 Correct 379 ms 34168 KB Output is correct
72 Correct 334 ms 34032 KB Output is correct
73 Correct 351 ms 33336 KB Output is correct
74 Correct 370 ms 34128 KB Output is correct
75 Correct 321 ms 34204 KB Output is correct
76 Correct 431 ms 32364 KB Output is correct
77 Correct 396 ms 30660 KB Output is correct
78 Correct 620 ms 35816 KB Output is correct
79 Correct 629 ms 33768 KB Output is correct
80 Correct 588 ms 41020 KB Output is correct
81 Correct 634 ms 37492 KB Output is correct
82 Correct 663 ms 37620 KB Output is correct
83 Correct 630 ms 34184 KB Output is correct
84 Correct 678 ms 40044 KB Output is correct
85 Correct 661 ms 38504 KB Output is correct
86 Correct 672 ms 33860 KB Output is correct
87 Correct 622 ms 35628 KB Output is correct
88 Correct 497 ms 36632 KB Output is correct
89 Correct 457 ms 32624 KB Output is correct
90 Correct 450 ms 32604 KB Output is correct
91 Correct 491 ms 35000 KB Output is correct
92 Correct 462 ms 33488 KB Output is correct
93 Correct 470 ms 33304 KB Output is correct
94 Correct 449 ms 32648 KB Output is correct
95 Correct 449 ms 34168 KB Output is correct
96 Correct 466 ms 32688 KB Output is correct
97 Correct 468 ms 34892 KB Output is correct