답안 #414359

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
414359 2021-05-30T11:27:11 Z ritul_kr_singh 수도 (JOI20_capital_city) C++17
100 / 100
605 ms 35792 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
 
const int MAXN = 2e5;
 
int n, k, color[MAXN], par[MAXN], s[MAXN], ans = MAXN, curr;
vector<int> g[MAXN], h[MAXN];
bitset<MAXN> r, vis, bad;
 
void calcSize(int u){
	s[u] = 1;
	for(int v : g[u]) if(!s[v]) calcSize(v), s[u] += s[v];
}
 
int find(int u, int p, int sz){
	bool f = 1;
	while(f){
		f = 0;
		s[p] -= s[u], s[u] = sz;
		for(int v : g[u]){
			if(!r[v] && s[v] > sz/2){
				p = u, u = v;
				f = 1;
				break;
			}
		}
	}
	return u;
}
 
void dfs(int u){
	vis[color[u]] = 0;
	for(int v : g[u]) if(s[v] < s[u] && !r[v]) dfs(v), par[v] = curr * MAXN + u;
}
 
void decompose(int c){
	r[c = curr = find(c, c, s[c])] = 1;
	par[c] = curr * MAXN + c;
	dfs(c);
 
	int res = 0;
	vis[color[c]] = 1;
	vector<int> st;
	for(int i : h[color[c]]){
		if(res == MAXN) break;
		if(par[i] / MAXN != c) res = MAXN;
		else st.push_back(i);
	}
 
	while(!st.empty() && res < MAXN){
		int u = st.back(); st.pop_back();
		int p = par[u] % MAXN;
		if(!vis[color[p]]){
			if(bad[color[p]]){
				res = MAXN;
				break;
			}
			++res;
			for(int v : h[color[p]]){
				if(par[v] / MAXN == c && res < MAXN) st.push_back(v);
				else res = MAXN;
			}
			if(res < MAXN) vis[color[p]] = 1;
			else bad[color[p]] = 1;
		}
	}
	ans = min(ans, res);

	bad[color[c]] = 1;
	for(int v : g[c]) if(!r[v]) decompose(v);
}
 
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int x, y; cin >> n >> k;
	for(int i=1; i<n; ++i){
		cin >> x >> y; --x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
 
	for(int i=0; i<n; ++i){
		cin >> color[i]; --color[i];
		h[color[i]].push_back(i);
	}
 
	calcSize(0);
	decompose(0);
 
	cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 7 ms 9720 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 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9628 KB Output is correct
9 Correct 7 ms 9636 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 7 ms 9720 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 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9628 KB Output is correct
9 Correct 7 ms 9636 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 9 ms 9948 KB Output is correct
12 Correct 7 ms 9944 KB Output is correct
13 Correct 7 ms 9944 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9952 KB Output is correct
16 Correct 8 ms 9812 KB Output is correct
17 Correct 7 ms 9876 KB Output is correct
18 Correct 7 ms 9860 KB Output is correct
19 Correct 7 ms 9932 KB Output is correct
20 Correct 7 ms 9932 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 8 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9936 KB Output is correct
25 Correct 9 ms 9932 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9988 KB Output is correct
28 Correct 8 ms 9944 KB Output is correct
29 Correct 8 ms 9932 KB Output is correct
30 Correct 8 ms 9932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 480 ms 34376 KB Output is correct
2 Correct 176 ms 34520 KB Output is correct
3 Correct 496 ms 34144 KB Output is correct
4 Correct 170 ms 34540 KB Output is correct
5 Correct 482 ms 32528 KB Output is correct
6 Correct 178 ms 34512 KB Output is correct
7 Correct 485 ms 32696 KB Output is correct
8 Correct 169 ms 35260 KB Output is correct
9 Correct 541 ms 32796 KB Output is correct
10 Correct 507 ms 32636 KB Output is correct
11 Correct 566 ms 33708 KB Output is correct
12 Correct 586 ms 35216 KB Output is correct
13 Correct 492 ms 32524 KB Output is correct
14 Correct 573 ms 35720 KB Output is correct
15 Correct 523 ms 35404 KB Output is correct
16 Correct 576 ms 32824 KB Output is correct
17 Correct 582 ms 32828 KB Output is correct
18 Correct 555 ms 32960 KB Output is correct
19 Correct 578 ms 34748 KB Output is correct
20 Correct 573 ms 35792 KB Output is correct
21 Correct 7 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 7 ms 9720 KB Output is correct
3 Correct 7 ms 9720 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 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9628 KB Output is correct
9 Correct 7 ms 9636 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 9 ms 9948 KB Output is correct
12 Correct 7 ms 9944 KB Output is correct
13 Correct 7 ms 9944 KB Output is correct
14 Correct 7 ms 9856 KB Output is correct
15 Correct 7 ms 9952 KB Output is correct
16 Correct 8 ms 9812 KB Output is correct
17 Correct 7 ms 9876 KB Output is correct
18 Correct 7 ms 9860 KB Output is correct
19 Correct 7 ms 9932 KB Output is correct
20 Correct 7 ms 9932 KB Output is correct
21 Correct 8 ms 9856 KB Output is correct
22 Correct 8 ms 9932 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9936 KB Output is correct
25 Correct 9 ms 9932 KB Output is correct
26 Correct 8 ms 9932 KB Output is correct
27 Correct 8 ms 9988 KB Output is correct
28 Correct 8 ms 9944 KB Output is correct
29 Correct 8 ms 9932 KB Output is correct
30 Correct 8 ms 9932 KB Output is correct
31 Correct 480 ms 34376 KB Output is correct
32 Correct 176 ms 34520 KB Output is correct
33 Correct 496 ms 34144 KB Output is correct
34 Correct 170 ms 34540 KB Output is correct
35 Correct 482 ms 32528 KB Output is correct
36 Correct 178 ms 34512 KB Output is correct
37 Correct 485 ms 32696 KB Output is correct
38 Correct 169 ms 35260 KB Output is correct
39 Correct 541 ms 32796 KB Output is correct
40 Correct 507 ms 32636 KB Output is correct
41 Correct 566 ms 33708 KB Output is correct
42 Correct 586 ms 35216 KB Output is correct
43 Correct 492 ms 32524 KB Output is correct
44 Correct 573 ms 35720 KB Output is correct
45 Correct 523 ms 35404 KB Output is correct
46 Correct 576 ms 32824 KB Output is correct
47 Correct 582 ms 32828 KB Output is correct
48 Correct 555 ms 32960 KB Output is correct
49 Correct 578 ms 34748 KB Output is correct
50 Correct 573 ms 35792 KB Output is correct
51 Correct 7 ms 9676 KB Output is correct
52 Correct 414 ms 28828 KB Output is correct
53 Correct 420 ms 28856 KB Output is correct
54 Correct 473 ms 28748 KB Output is correct
55 Correct 477 ms 28856 KB Output is correct
56 Correct 466 ms 28856 KB Output is correct
57 Correct 434 ms 28852 KB Output is correct
58 Correct 435 ms 30100 KB Output is correct
59 Correct 390 ms 29508 KB Output is correct
60 Correct 529 ms 28588 KB Output is correct
61 Correct 543 ms 28840 KB Output is correct
62 Correct 176 ms 35396 KB Output is correct
63 Correct 182 ms 35408 KB Output is correct
64 Correct 212 ms 35396 KB Output is correct
65 Correct 175 ms 35420 KB Output is correct
66 Correct 304 ms 32568 KB Output is correct
67 Correct 332 ms 32604 KB Output is correct
68 Correct 308 ms 32520 KB Output is correct
69 Correct 355 ms 32656 KB Output is correct
70 Correct 347 ms 32552 KB Output is correct
71 Correct 312 ms 32476 KB Output is correct
72 Correct 326 ms 32568 KB Output is correct
73 Correct 337 ms 32692 KB Output is correct
74 Correct 309 ms 32564 KB Output is correct
75 Correct 344 ms 32632 KB Output is correct
76 Correct 501 ms 31340 KB Output is correct
77 Correct 509 ms 30788 KB Output is correct
78 Correct 549 ms 32844 KB Output is correct
79 Correct 552 ms 32748 KB Output is correct
80 Correct 605 ms 35508 KB Output is correct
81 Correct 574 ms 33432 KB Output is correct
82 Correct 534 ms 33596 KB Output is correct
83 Correct 549 ms 33032 KB Output is correct
84 Correct 556 ms 35280 KB Output is correct
85 Correct 560 ms 34376 KB Output is correct
86 Correct 522 ms 32760 KB Output is correct
87 Correct 555 ms 32696 KB Output is correct
88 Correct 490 ms 33624 KB Output is correct
89 Correct 499 ms 31884 KB Output is correct
90 Correct 480 ms 31808 KB Output is correct
91 Correct 481 ms 32436 KB Output is correct
92 Correct 485 ms 32176 KB Output is correct
93 Correct 459 ms 31932 KB Output is correct
94 Correct 474 ms 31836 KB Output is correct
95 Correct 487 ms 32052 KB Output is correct
96 Correct 492 ms 31980 KB Output is correct
97 Correct 479 ms 32800 KB Output is correct