답안 #261109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261109 2020-08-11T11:49:57 Z amoo_safar 수도 (JOI20_capital_city) C++17
100 / 100
793 ms 37428 KB
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>

#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'

using namespace std;

typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;

const ll Mod = 1000000007LL;
const int N = 2e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;

int n, K, col[N], ans = N;
vector<int> G[N], C[N];
int mkc[N], Tc = 1;
int slv[N], mk[N], Tm, vis[N], Tv;
int sub[N], par[N];

int Sub(int u, int p){
	sub[u] = 1;
	vis[u] = Tv;
	//par[u] = p;
	for(auto adj : G[u])
		if(adj != p && !slv[adj])
			sub[u] += Sub(adj, u);
	return sub[u];
}
void Par(int u, int p){
	par[u] = p;
	for(auto adj : G[u]){
		if(adj != p && !slv[adj])
			Par(adj, u);
	}
}

int Q[N];
void Solve(int u){
	Tv ++;
	int sz = Sub(u, -1);
	int cen = u;
	bool fnd = false;
	while(!fnd){
		fnd = true;
		for(auto adj : G[cen]){
			if(!slv[adj] && sub[adj] < sub[cen] && 2*sub[adj] >= sz){
				fnd = false;
				cen = adj;
				break;
			}
		}
	}
	Par(cen, -1);
	//cerr << u << " -> " << cen << '\n';
	int l = 0, r = 1;
	Q[0] = cen; 
	int fr, res = 0; Tc ++; Tm ++;
	mk[cen] = Tm;
	while(l < r){
		fr = Q[l]; l++;
		//if(cen == 3) debug(fr);
		if(mkc[col[fr]] != Tc){
			res ++;

			mkc[col[fr]] = Tc;
			for(auto adj : C[col[fr]]){
				if(vis[adj] != Tv){
					res = N;
					break;
				}
				if(mk[adj] != Tm){
					Q[r ++] = adj;
					mk[adj] = Tm;
				}
			}
		}
		if(par[fr] != -1 && mk[par[fr]] != Tm){
			mk[par[fr]] = Tm;
			Q[r ++] = par[fr];
		}
	}
	ans = min(ans, res - 1);
	slv[cen] = 1;
	for(auto adj : G[cen]) if(!slv[adj]) Solve(adj);
}

int main(){
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> K;
	int u, v;
	for(int i = 1; i < n; i++){
		cin >> u >> v;
		G[u].pb(v);
		G[v].pb(u);
	}
	for(int i = 1; i <= n; i++){
		cin >> col[i];
		C[col[i]].pb(i);
	}
	//cerr << '\n';
	Solve(1);
	cout << ans << '\n';
	return 0;
}
/*
6 3
2 1
3 5
6 2
3 4
2 3
1
3
1
2
3
2
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 9 ms 9984 KB Output is correct
13 Correct 8 ms 9984 KB Output is correct
14 Correct 8 ms 9984 KB Output is correct
15 Correct 8 ms 9984 KB Output is correct
16 Correct 9 ms 9984 KB Output is correct
17 Correct 8 ms 9984 KB Output is correct
18 Correct 8 ms 9984 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 8 ms 10112 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 9 ms 9984 KB Output is correct
23 Correct 10 ms 9984 KB Output is correct
24 Correct 8 ms 9984 KB Output is correct
25 Correct 9 ms 9984 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 9 ms 9984 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
29 Correct 9 ms 9984 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 570 ms 33400 KB Output is correct
2 Correct 205 ms 37240 KB Output is correct
3 Correct 552 ms 36728 KB Output is correct
4 Correct 204 ms 37240 KB Output is correct
5 Correct 580 ms 34936 KB Output is correct
6 Correct 203 ms 37368 KB Output is correct
7 Correct 574 ms 35196 KB Output is correct
8 Correct 201 ms 37108 KB Output is correct
9 Correct 691 ms 34228 KB Output is correct
10 Correct 706 ms 32504 KB Output is correct
11 Correct 734 ms 34444 KB Output is correct
12 Correct 736 ms 35960 KB Output is correct
13 Correct 648 ms 32376 KB Output is correct
14 Correct 660 ms 36092 KB Output is correct
15 Correct 719 ms 35832 KB Output is correct
16 Correct 708 ms 33004 KB Output is correct
17 Correct 680 ms 33312 KB Output is correct
18 Correct 680 ms 33272 KB Output is correct
19 Correct 678 ms 35320 KB Output is correct
20 Correct 667 ms 36600 KB Output is correct
21 Correct 7 ms 9728 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 7 ms 9728 KB Output is correct
4 Correct 7 ms 9728 KB Output is correct
5 Correct 7 ms 9728 KB Output is correct
6 Correct 7 ms 9728 KB Output is correct
7 Correct 7 ms 9728 KB Output is correct
8 Correct 7 ms 9728 KB Output is correct
9 Correct 7 ms 9728 KB Output is correct
10 Correct 8 ms 9728 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 9 ms 9984 KB Output is correct
13 Correct 8 ms 9984 KB Output is correct
14 Correct 8 ms 9984 KB Output is correct
15 Correct 8 ms 9984 KB Output is correct
16 Correct 9 ms 9984 KB Output is correct
17 Correct 8 ms 9984 KB Output is correct
18 Correct 8 ms 9984 KB Output is correct
19 Correct 8 ms 9984 KB Output is correct
20 Correct 8 ms 10112 KB Output is correct
21 Correct 8 ms 9984 KB Output is correct
22 Correct 9 ms 9984 KB Output is correct
23 Correct 10 ms 9984 KB Output is correct
24 Correct 8 ms 9984 KB Output is correct
25 Correct 9 ms 9984 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 9 ms 9984 KB Output is correct
28 Correct 9 ms 9984 KB Output is correct
29 Correct 9 ms 9984 KB Output is correct
30 Correct 9 ms 9984 KB Output is correct
31 Correct 570 ms 33400 KB Output is correct
32 Correct 205 ms 37240 KB Output is correct
33 Correct 552 ms 36728 KB Output is correct
34 Correct 204 ms 37240 KB Output is correct
35 Correct 580 ms 34936 KB Output is correct
36 Correct 203 ms 37368 KB Output is correct
37 Correct 574 ms 35196 KB Output is correct
38 Correct 201 ms 37108 KB Output is correct
39 Correct 691 ms 34228 KB Output is correct
40 Correct 706 ms 32504 KB Output is correct
41 Correct 734 ms 34444 KB Output is correct
42 Correct 736 ms 35960 KB Output is correct
43 Correct 648 ms 32376 KB Output is correct
44 Correct 660 ms 36092 KB Output is correct
45 Correct 719 ms 35832 KB Output is correct
46 Correct 708 ms 33004 KB Output is correct
47 Correct 680 ms 33312 KB Output is correct
48 Correct 680 ms 33272 KB Output is correct
49 Correct 678 ms 35320 KB Output is correct
50 Correct 667 ms 36600 KB Output is correct
51 Correct 7 ms 9728 KB Output is correct
52 Correct 480 ms 26872 KB Output is correct
53 Correct 505 ms 26744 KB Output is correct
54 Correct 536 ms 26852 KB Output is correct
55 Correct 558 ms 27000 KB Output is correct
56 Correct 574 ms 26744 KB Output is correct
57 Correct 585 ms 26852 KB Output is correct
58 Correct 583 ms 28920 KB Output is correct
59 Correct 584 ms 29176 KB Output is correct
60 Correct 730 ms 28920 KB Output is correct
61 Correct 725 ms 28764 KB Output is correct
62 Correct 218 ms 37232 KB Output is correct
63 Correct 212 ms 37240 KB Output is correct
64 Correct 206 ms 37240 KB Output is correct
65 Correct 207 ms 37428 KB Output is correct
66 Correct 372 ms 32112 KB Output is correct
67 Correct 365 ms 31984 KB Output is correct
68 Correct 404 ms 32236 KB Output is correct
69 Correct 428 ms 32116 KB Output is correct
70 Correct 439 ms 32112 KB Output is correct
71 Correct 436 ms 31984 KB Output is correct
72 Correct 398 ms 32112 KB Output is correct
73 Correct 431 ms 31728 KB Output is correct
74 Correct 386 ms 32116 KB Output is correct
75 Correct 430 ms 32112 KB Output is correct
76 Correct 592 ms 31680 KB Output is correct
77 Correct 591 ms 30432 KB Output is correct
78 Correct 764 ms 33144 KB Output is correct
79 Correct 707 ms 32120 KB Output is correct
80 Correct 716 ms 36472 KB Output is correct
81 Correct 781 ms 34032 KB Output is correct
82 Correct 793 ms 34040 KB Output is correct
83 Correct 678 ms 32188 KB Output is correct
84 Correct 715 ms 35760 KB Output is correct
85 Correct 653 ms 34680 KB Output is correct
86 Correct 659 ms 31736 KB Output is correct
87 Correct 684 ms 32888 KB Output is correct
88 Correct 539 ms 33656 KB Output is correct
89 Correct 577 ms 31224 KB Output is correct
90 Correct 537 ms 30968 KB Output is correct
91 Correct 612 ms 32760 KB Output is correct
92 Correct 561 ms 31864 KB Output is correct
93 Correct 630 ms 31436 KB Output is correct
94 Correct 594 ms 30976 KB Output is correct
95 Correct 624 ms 32060 KB Output is correct
96 Correct 572 ms 31224 KB Output is correct
97 Correct 663 ms 32636 KB Output is correct