답안 #486715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486715 2021-11-12T13:09:35 Z Koosha_mv 수도 (JOI20_capital_city) C++14
100 / 100
848 ms 44680 KB
#include <bits/stdc++.h>
using namespace std;
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define print(v,r) f(i,0,r) cout<<v[i]<<" "; cout<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define Add(x,y) x=(x+y)%mod
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+99;

int n,t,k,cnt,ans=N,a[N],sz[N],col[N],nxt[N],vis[N],par[N],mark[N],prt[N];
vector<int> v,vc[N],g[N];

void dfs1(int x,int par=0){
	sz[x]=1;
	v.pb(x);
	f(i,0,g[x].size()){
		if(!vis[g[x][i]] && g[x][i]!=par){
			dfs1(g[x][i],x);
			sz[x]+=sz[g[x][i]];
		}
	}
}
void dfs2(int x){
	mark[x]=cnt;
	f(i,0,g[x].size()){
		if(!vis[g[x][i]] && g[x][i]!=par[x]){
			par[g[x][i]]=x;
			dfs2(g[x][i]);
		}
	}
}
void solve(int x){
	int mv=0,rt=0,res=0;
	cnt++;
	v.clear();
	dfs1(x);
	f(i,0,v.size()){
		if(sz[v[i]]>sz[x]/2){
			rt=v[i];
		}
	}
	//cout<<x<<" : "<<rt<<endl;
	
	par[rt]=rt;
	dfs2(rt);
	vector<int> q;
	q.pb(rt);
	mark[rt]=-cnt;
	f(i,0,q.size()){
		int u=q[i],v=u;
		//eror(u);
		while(mark[par[v]]==cnt){
			v=par[v];
			q.pb(v);
			mark[v]=-cnt;
		}
		if(abs(mark[nxt[u]])!=cnt){
			mv=1;
			break;
		}
		if(mark[nxt[u]]==cnt){
			q.pb(nxt[u]);
			mark[nxt[u]]=-cnt;
		}
	}
	if(!mv){
		f(i,0,q.size()){
			if(prt[col[q[i]]]==0){
				res++;
				prt[col[q[i]]]=1;
			}
		}
		f(i,0,q.size()){
			prt[col[q[i]]]=0;
		}
		minm(ans,res);
	}

	vis[rt]=1;
	f(i,0,g[rt].size()){
		//eror(g[rt][i]);
		if(!vis[g[rt][i]]){
			solve(g[rt][i]);
		}
	}	
}

int main(){
	cin>>n>>k;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	f(i,1,n+1){
		cin>>col[i];
		col[i]--;
		vc[col[i]].pb(i);
	}
	f(i,0,k){
		f(j,0,vc[i].size()){
			nxt[vc[i][j]]=vc[i][(j+1)%vc[i].size()];
		}	
	}
	solve(1);
	cout<<ans-1;
}

Compilation message

capital_city.cpp: In function 'void dfs1(int, int)':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   28 |  f(i,0,g[x].size()){
      |    ~~~~~~~~~~~~~~~             
capital_city.cpp:28:2: note: in expansion of macro 'f'
   28 |  f(i,0,g[x].size()){
      |  ^
capital_city.cpp: In function 'void dfs2(int)':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   37 |  f(i,0,g[x].size()){
      |    ~~~~~~~~~~~~~~~             
capital_city.cpp:37:2: note: in expansion of macro 'f'
   37 |  f(i,0,g[x].size()){
      |  ^
capital_city.cpp: In function 'void solve(int)':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   49 |  f(i,0,v.size()){
      |    ~~~~~~~~~~~~                
capital_city.cpp:49:2: note: in expansion of macro 'f'
   49 |  f(i,0,v.size()){
      |  ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   61 |  f(i,0,q.size()){
      |    ~~~~~~~~~~~~                
capital_city.cpp:61:2: note: in expansion of macro 'f'
   61 |  f(i,0,q.size()){
      |  ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   79 |   f(i,0,q.size()){
      |     ~~~~~~~~~~~~               
capital_city.cpp:79:3: note: in expansion of macro 'f'
   79 |   f(i,0,q.size()){
      |   ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   85 |   f(i,0,q.size()){
      |     ~~~~~~~~~~~~               
capital_city.cpp:85:3: note: in expansion of macro 'f'
   85 |   f(i,0,q.size()){
      |   ^
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   92 |  f(i,0,g[rt].size()){
      |    ~~~~~~~~~~~~~~~~            
capital_city.cpp:92:2: note: in expansion of macro 'f'
   92 |  f(i,0,g[rt].size()){
      |  ^
capital_city.cpp: In function 'int main()':
capital_city.cpp:7:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define f(i,a,b) for(int i=a;i<b;i++)
......
  114 |   f(j,0,vc[i].size()){
      |     ~~~~~~~~~~~~~~~~           
capital_city.cpp:114:3: note: in expansion of macro 'f'
  114 |   f(j,0,vc[i].size()){
      |   ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 6 ms 9732 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 7 ms 9696 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 7 ms 9620 KB Output is correct
9 Correct 6 ms 9820 KB Output is correct
10 Correct 6 ms 9716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 6 ms 9732 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 7 ms 9696 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 7 ms 9620 KB Output is correct
9 Correct 6 ms 9820 KB Output is correct
10 Correct 6 ms 9716 KB Output is correct
11 Correct 9 ms 9972 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9804 KB Output is correct
14 Correct 7 ms 9804 KB Output is correct
15 Correct 8 ms 9932 KB Output is correct
16 Correct 8 ms 9864 KB Output is correct
17 Correct 8 ms 9932 KB Output is correct
18 Correct 8 ms 9972 KB Output is correct
19 Correct 9 ms 9932 KB Output is correct
20 Correct 9 ms 9932 KB Output is correct
21 Correct 8 ms 9932 KB Output is correct
22 Correct 8 ms 10060 KB Output is correct
23 Correct 10 ms 9988 KB Output is correct
24 Correct 9 ms 9932 KB Output is correct
25 Correct 9 ms 9968 KB Output is correct
26 Correct 9 ms 9932 KB Output is correct
27 Correct 9 ms 9964 KB Output is correct
28 Correct 9 ms 9992 KB Output is correct
29 Correct 10 ms 9984 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 617 ms 43452 KB Output is correct
2 Correct 359 ms 43952 KB Output is correct
3 Correct 553 ms 43188 KB Output is correct
4 Correct 318 ms 44020 KB Output is correct
5 Correct 682 ms 40612 KB Output is correct
6 Correct 365 ms 43888 KB Output is correct
7 Correct 568 ms 41152 KB Output is correct
8 Correct 374 ms 44000 KB Output is correct
9 Correct 829 ms 40108 KB Output is correct
10 Correct 723 ms 37664 KB Output is correct
11 Correct 766 ms 40472 KB Output is correct
12 Correct 841 ms 43204 KB Output is correct
13 Correct 835 ms 37256 KB Output is correct
14 Correct 775 ms 43504 KB Output is correct
15 Correct 763 ms 43252 KB Output is correct
16 Correct 826 ms 38228 KB Output is correct
17 Correct 703 ms 38772 KB Output is correct
18 Correct 713 ms 39088 KB Output is correct
19 Correct 764 ms 42128 KB Output is correct
20 Correct 692 ms 44680 KB Output is correct
21 Correct 5 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9676 KB Output is correct
2 Correct 6 ms 9804 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 6 ms 9732 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 7 ms 9696 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 7 ms 9620 KB Output is correct
9 Correct 6 ms 9820 KB Output is correct
10 Correct 6 ms 9716 KB Output is correct
11 Correct 9 ms 9972 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9804 KB Output is correct
14 Correct 7 ms 9804 KB Output is correct
15 Correct 8 ms 9932 KB Output is correct
16 Correct 8 ms 9864 KB Output is correct
17 Correct 8 ms 9932 KB Output is correct
18 Correct 8 ms 9972 KB Output is correct
19 Correct 9 ms 9932 KB Output is correct
20 Correct 9 ms 9932 KB Output is correct
21 Correct 8 ms 9932 KB Output is correct
22 Correct 8 ms 10060 KB Output is correct
23 Correct 10 ms 9988 KB Output is correct
24 Correct 9 ms 9932 KB Output is correct
25 Correct 9 ms 9968 KB Output is correct
26 Correct 9 ms 9932 KB Output is correct
27 Correct 9 ms 9964 KB Output is correct
28 Correct 9 ms 9992 KB Output is correct
29 Correct 10 ms 9984 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
31 Correct 617 ms 43452 KB Output is correct
32 Correct 359 ms 43952 KB Output is correct
33 Correct 553 ms 43188 KB Output is correct
34 Correct 318 ms 44020 KB Output is correct
35 Correct 682 ms 40612 KB Output is correct
36 Correct 365 ms 43888 KB Output is correct
37 Correct 568 ms 41152 KB Output is correct
38 Correct 374 ms 44000 KB Output is correct
39 Correct 829 ms 40108 KB Output is correct
40 Correct 723 ms 37664 KB Output is correct
41 Correct 766 ms 40472 KB Output is correct
42 Correct 841 ms 43204 KB Output is correct
43 Correct 835 ms 37256 KB Output is correct
44 Correct 775 ms 43504 KB Output is correct
45 Correct 763 ms 43252 KB Output is correct
46 Correct 826 ms 38228 KB Output is correct
47 Correct 703 ms 38772 KB Output is correct
48 Correct 713 ms 39088 KB Output is correct
49 Correct 764 ms 42128 KB Output is correct
50 Correct 692 ms 44680 KB Output is correct
51 Correct 5 ms 9676 KB Output is correct
52 Correct 584 ms 27700 KB Output is correct
53 Correct 592 ms 27708 KB Output is correct
54 Correct 563 ms 27676 KB Output is correct
55 Correct 579 ms 27700 KB Output is correct
56 Correct 535 ms 27732 KB Output is correct
57 Correct 511 ms 27804 KB Output is correct
58 Correct 482 ms 31156 KB Output is correct
59 Correct 453 ms 30864 KB Output is correct
60 Correct 608 ms 30352 KB Output is correct
61 Correct 802 ms 30360 KB Output is correct
62 Correct 339 ms 43996 KB Output is correct
63 Correct 340 ms 43960 KB Output is correct
64 Correct 322 ms 44116 KB Output is correct
65 Correct 336 ms 43888 KB Output is correct
66 Correct 413 ms 36196 KB Output is correct
67 Correct 460 ms 36088 KB Output is correct
68 Correct 461 ms 36264 KB Output is correct
69 Correct 535 ms 36276 KB Output is correct
70 Correct 463 ms 36172 KB Output is correct
71 Correct 528 ms 36180 KB Output is correct
72 Correct 423 ms 36284 KB Output is correct
73 Correct 429 ms 35396 KB Output is correct
74 Correct 469 ms 36212 KB Output is correct
75 Correct 463 ms 36236 KB Output is correct
76 Correct 608 ms 34972 KB Output is correct
77 Correct 556 ms 33032 KB Output is correct
78 Correct 848 ms 38976 KB Output is correct
79 Correct 761 ms 36932 KB Output is correct
80 Correct 763 ms 43880 KB Output is correct
81 Correct 788 ms 40280 KB Output is correct
82 Correct 754 ms 40444 KB Output is correct
83 Correct 744 ms 37088 KB Output is correct
84 Correct 786 ms 42968 KB Output is correct
85 Correct 784 ms 41500 KB Output is correct
86 Correct 729 ms 36752 KB Output is correct
87 Correct 738 ms 38420 KB Output is correct
88 Correct 610 ms 39372 KB Output is correct
89 Correct 639 ms 35444 KB Output is correct
90 Correct 738 ms 34984 KB Output is correct
91 Correct 703 ms 37688 KB Output is correct
92 Correct 638 ms 36380 KB Output is correct
93 Correct 613 ms 35956 KB Output is correct
94 Correct 660 ms 35256 KB Output is correct
95 Correct 675 ms 36760 KB Output is correct
96 Correct 653 ms 35364 KB Output is correct
97 Correct 678 ms 37448 KB Output is correct