답안 #328553

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
328553 2020-11-17T04:55:08 Z YJU 수도 (JOI20_capital_city) C++14
100 / 100
829 ms 64996 KB
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=4e5+5;
const ll K=350;
const ld pi=3.14159265359;
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()

ll n,m,x,y,col[N],maxn[N],vis[N],sub[N],vis_col[N],ans=INF;
vector<ll> v[N],C[N];
ll pa[N],tag[N];

ll Find_root(ll nd){
	function<void(ll,ll)> DFS = [&](ll id,ll p){
		sub[id]=1;maxn[id]=-1;
		for(auto i:v[id]){
			if(i!=p&&!vis[i]){
				DFS(i,id);
				sub[id]+=sub[i];
				if(maxn[id]==-1||sub[maxn[id]]<sub[i])maxn[id]=i;
			}
		}
	};
	DFS(nd,0);
	ll tot=sub[nd];
	while(maxn[nd]!=-1&&sub[maxn[nd]]*2>=tot)nd=maxn[nd];
	return nd;
}

void centroid(ll nd){
	nd=Find_root(nd);
	ll rt=nd;
	pa[nd]=nd;
	function<void(ll,ll)> DFS = [&](ll id,ll p){
		pa[id]=p;tag[id]=rt;
		for(auto i:v[id]){
			if(!vis[i]&&i!=p)DFS(i,id);
		}
	};
	DFS(nd,nd);
	vector<ll> res;
	queue<ll> q;
	q.push(rt);
	ll cnt=-1,ok=1,u;
	while(SZ(q)&&ok){
		u=q.front();q.pop();
		++cnt;
		while(ok){
			if(tag[u]==0)break;
			if(!vis_col[col[u]]){
				cnt-=SZ(C[col[u]]);
				vis_col[col[u]]=1;
				res.pb(col[u]);
				for(auto i:C[col[u]]){
					q.push(i);
					if(tag[i]!=rt){
						ok=0;break;
					}
				}
			}
			tag[u]=0;
			u=pa[u];
			if(u==rt)break;
		}
	}
	for(auto i:res)vis_col[i]=0;
	if(cnt==0)ans=min(ans,SZ(res)-1);
	vis[nd]=1;
	for(auto i:v[nd])if(!vis[i])centroid(i);
}

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>n>>m;
	REP1(i,n-1)cin>>x>>y,v[x].pb(y),v[y].pb(x);
	REP1(i,n)cin>>col[i],C[col[i]].pb(i);
	centroid(1);
	cout<<ans<<"\n"; 
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19180 KB Output is correct
2 Correct 12 ms 19180 KB Output is correct
3 Correct 12 ms 19180 KB Output is correct
4 Correct 12 ms 19180 KB Output is correct
5 Correct 13 ms 19180 KB Output is correct
6 Correct 12 ms 19180 KB Output is correct
7 Correct 12 ms 19180 KB Output is correct
8 Correct 12 ms 19180 KB Output is correct
9 Correct 12 ms 19180 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19180 KB Output is correct
2 Correct 12 ms 19180 KB Output is correct
3 Correct 12 ms 19180 KB Output is correct
4 Correct 12 ms 19180 KB Output is correct
5 Correct 13 ms 19180 KB Output is correct
6 Correct 12 ms 19180 KB Output is correct
7 Correct 12 ms 19180 KB Output is correct
8 Correct 12 ms 19180 KB Output is correct
9 Correct 12 ms 19180 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
11 Correct 14 ms 19436 KB Output is correct
12 Correct 14 ms 19436 KB Output is correct
13 Correct 15 ms 19436 KB Output is correct
14 Correct 14 ms 19436 KB Output is correct
15 Correct 14 ms 19436 KB Output is correct
16 Correct 14 ms 19436 KB Output is correct
17 Correct 14 ms 19436 KB Output is correct
18 Correct 15 ms 19564 KB Output is correct
19 Correct 14 ms 19584 KB Output is correct
20 Correct 14 ms 19564 KB Output is correct
21 Correct 14 ms 19564 KB Output is correct
22 Correct 14 ms 19564 KB Output is correct
23 Correct 14 ms 19564 KB Output is correct
24 Correct 18 ms 19548 KB Output is correct
25 Correct 15 ms 19564 KB Output is correct
26 Correct 15 ms 19564 KB Output is correct
27 Correct 14 ms 19564 KB Output is correct
28 Correct 15 ms 19564 KB Output is correct
29 Correct 14 ms 19564 KB Output is correct
30 Correct 15 ms 19692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 801 ms 59908 KB Output is correct
2 Correct 247 ms 60768 KB Output is correct
3 Correct 779 ms 59500 KB Output is correct
4 Correct 250 ms 60652 KB Output is correct
5 Correct 774 ms 55792 KB Output is correct
6 Correct 250 ms 64364 KB Output is correct
7 Correct 776 ms 60136 KB Output is correct
8 Correct 256 ms 64996 KB Output is correct
9 Correct 814 ms 58728 KB Output is correct
10 Correct 807 ms 55144 KB Output is correct
11 Correct 824 ms 59276 KB Output is correct
12 Correct 818 ms 62916 KB Output is correct
13 Correct 825 ms 54628 KB Output is correct
14 Correct 821 ms 63336 KB Output is correct
15 Correct 829 ms 63084 KB Output is correct
16 Correct 816 ms 56032 KB Output is correct
17 Correct 825 ms 56808 KB Output is correct
18 Correct 818 ms 57196 KB Output is correct
19 Correct 828 ms 61404 KB Output is correct
20 Correct 827 ms 64416 KB Output is correct
21 Correct 12 ms 19180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 19180 KB Output is correct
2 Correct 12 ms 19180 KB Output is correct
3 Correct 12 ms 19180 KB Output is correct
4 Correct 12 ms 19180 KB Output is correct
5 Correct 13 ms 19180 KB Output is correct
6 Correct 12 ms 19180 KB Output is correct
7 Correct 12 ms 19180 KB Output is correct
8 Correct 12 ms 19180 KB Output is correct
9 Correct 12 ms 19180 KB Output is correct
10 Correct 12 ms 19180 KB Output is correct
11 Correct 14 ms 19436 KB Output is correct
12 Correct 14 ms 19436 KB Output is correct
13 Correct 15 ms 19436 KB Output is correct
14 Correct 14 ms 19436 KB Output is correct
15 Correct 14 ms 19436 KB Output is correct
16 Correct 14 ms 19436 KB Output is correct
17 Correct 14 ms 19436 KB Output is correct
18 Correct 15 ms 19564 KB Output is correct
19 Correct 14 ms 19584 KB Output is correct
20 Correct 14 ms 19564 KB Output is correct
21 Correct 14 ms 19564 KB Output is correct
22 Correct 14 ms 19564 KB Output is correct
23 Correct 14 ms 19564 KB Output is correct
24 Correct 18 ms 19548 KB Output is correct
25 Correct 15 ms 19564 KB Output is correct
26 Correct 15 ms 19564 KB Output is correct
27 Correct 14 ms 19564 KB Output is correct
28 Correct 15 ms 19564 KB Output is correct
29 Correct 14 ms 19564 KB Output is correct
30 Correct 15 ms 19692 KB Output is correct
31 Correct 801 ms 59908 KB Output is correct
32 Correct 247 ms 60768 KB Output is correct
33 Correct 779 ms 59500 KB Output is correct
34 Correct 250 ms 60652 KB Output is correct
35 Correct 774 ms 55792 KB Output is correct
36 Correct 250 ms 64364 KB Output is correct
37 Correct 776 ms 60136 KB Output is correct
38 Correct 256 ms 64996 KB Output is correct
39 Correct 814 ms 58728 KB Output is correct
40 Correct 807 ms 55144 KB Output is correct
41 Correct 824 ms 59276 KB Output is correct
42 Correct 818 ms 62916 KB Output is correct
43 Correct 825 ms 54628 KB Output is correct
44 Correct 821 ms 63336 KB Output is correct
45 Correct 829 ms 63084 KB Output is correct
46 Correct 816 ms 56032 KB Output is correct
47 Correct 825 ms 56808 KB Output is correct
48 Correct 818 ms 57196 KB Output is correct
49 Correct 828 ms 61404 KB Output is correct
50 Correct 827 ms 64416 KB Output is correct
51 Correct 12 ms 19180 KB Output is correct
52 Correct 629 ms 43756 KB Output is correct
53 Correct 676 ms 44008 KB Output is correct
54 Correct 655 ms 43624 KB Output is correct
55 Correct 666 ms 43872 KB Output is correct
56 Correct 668 ms 43880 KB Output is correct
57 Correct 657 ms 43628 KB Output is correct
58 Correct 632 ms 47904 KB Output is correct
59 Correct 649 ms 48748 KB Output is correct
60 Correct 779 ms 46828 KB Output is correct
61 Correct 758 ms 46188 KB Output is correct
62 Correct 248 ms 64364 KB Output is correct
63 Correct 254 ms 64364 KB Output is correct
64 Correct 249 ms 64400 KB Output is correct
65 Correct 245 ms 64364 KB Output is correct
66 Correct 508 ms 53984 KB Output is correct
67 Correct 516 ms 53728 KB Output is correct
68 Correct 504 ms 53856 KB Output is correct
69 Correct 503 ms 53984 KB Output is correct
70 Correct 515 ms 53728 KB Output is correct
71 Correct 530 ms 53856 KB Output is correct
72 Correct 520 ms 53984 KB Output is correct
73 Correct 519 ms 52704 KB Output is correct
74 Correct 522 ms 53984 KB Output is correct
75 Correct 530 ms 53984 KB Output is correct
76 Correct 756 ms 52712 KB Output is correct
77 Correct 756 ms 49884 KB Output is correct
78 Correct 822 ms 56680 KB Output is correct
79 Correct 812 ms 53860 KB Output is correct
80 Correct 814 ms 63712 KB Output is correct
81 Correct 810 ms 58984 KB Output is correct
82 Correct 807 ms 58964 KB Output is correct
83 Correct 821 ms 54120 KB Output is correct
84 Correct 826 ms 62528 KB Output is correct
85 Correct 813 ms 60260 KB Output is correct
86 Correct 815 ms 53564 KB Output is correct
87 Correct 814 ms 56296 KB Output is correct
88 Correct 736 ms 58140 KB Output is correct
89 Correct 724 ms 52524 KB Output is correct
90 Correct 734 ms 52108 KB Output is correct
91 Correct 728 ms 55520 KB Output is correct
92 Correct 723 ms 53712 KB Output is correct
93 Correct 728 ms 53148 KB Output is correct
94 Correct 733 ms 52276 KB Output is correct
95 Correct 717 ms 54284 KB Output is correct
96 Correct 724 ms 52656 KB Output is correct
97 Correct 727 ms 55556 KB Output is correct