답안 #883042

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883042 2023-12-04T12:42:01 Z 8pete8 수도 (JOI20_capital_city) C++14
100 / 100
272 ms 100468 KB
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
const int mod=1e9+7,mxn=2e5+5,lg=30,inf=1e18,minf=-1e18;
int n,k;
int h[mxn+10],up[mxn+10][lg+5],g[mxn+10],gl[mxn+10],low[mxn+10],disc[mxn+10];
vector<int>adj[mxn+10],group[mxn+10],adj2[mxn+10];
void dfs(int cur,int p){
	for(auto i:adj[cur]){
		if(i==p)continue;
		h[i]=h[cur]+1;
		up[i][0]=cur;
		dfs(i,cur);
	}
}
int lca(int a,int b){
	if(h[a]<h[b])swap(a,b);
	int k=h[a]-h[b];
	for(int i=0;i<=lg;i++)if(k&(1<<i))a=up[a][i];
	if(a==b)return a;
	for(int i=lg;i>=0;i--)if(up[a][i]!=up[b][i])a=up[a][i],b=up[b][i];
	return up[a][0];
}
bitset<mxn+10>on,vis;
stack<int>st;
int t=0,ans=inf;
int comp[mxn+10];
int compcnt=0;
void tarjan(int cur){
	vis[cur]=true;
	low[cur]=disc[cur]=++t;
	st.push(cur);
	on[cur]=true;
	for(auto i:adj2[cur]){
		if(!disc[i])tarjan(i),low[cur]=min(low[cur],low[i]);
		else if(on[i])low[cur]=min(low[cur],low[i]);
	}
	if(disc[cur]==low[cur]){
		compcnt++;
		bool yes=true;
		on[cur]=false;
		comp[cur]=compcnt;
		vector<int>v;
		int cnt=1;
		v.pb(cur);
		while(!st.empty()&&st.top()!=cur){
			v.pb(st.top());
			if(on[st.top()])cnt++;
			comp[st.top()]=compcnt;
			on[st.top()]=false;
			st.pop();
		}
		if(!st.empty())st.pop();
		for(auto i:v)for(auto j:adj2[i])yes&=(compcnt==comp[j]);
		if(yes)ans=min(ans,cnt-1);
	}
}
int32_t main(){
	fastio
	cin>>n>>k;
	for(int i=0;i<n-1;i++){
		int a,b;cin>>a>>b;
		adj[a].pb(b);
		adj[b].pb(a);
	}
	dfs(1,-1);
	for(int i=1;i<=n;i++)cin>>g[i],group[g[i]].pb(i);
	for(int i=1;i<=lg;i++)up[1][i]=1;
	for(int i=1;i<=lg;i++)for(int j=1;j<=n;j++)up[j][i]=up[up[j][i-1]][i-1];
	for(int i=1;i<=k;i++){
		if(group[i].empty())continue;
		int node=group[i][0];
		for(int j=1;j<group[i].size();j++)node=lca(node,group[i][j]);
		gl[i]=node;
	}
	for(int i=1;i<=n;i++)if(g[up[i][0]]!=g[i]&&i!=gl[g[i]])adj2[g[i]].pb(g[up[i][0]]);
	for(int i=1;i<=k;i++)if(group[i].size()&&!vis[i])tarjan(i);
	cout<<((ans==inf)?0:ans);
	return 0;
}

Compilation message

capital_city.cpp: In function 'int32_t main()':
capital_city.cpp:103:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |   for(int j=1;j<group[i].size();j++)node=lca(node,group[i][j]);
      |               ~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22872 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 4 ms 23040 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 4 ms 22876 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 4 ms 22876 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22872 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 4 ms 23040 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 4 ms 22876 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 4 ms 22876 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 5 ms 23132 KB Output is correct
12 Correct 5 ms 23132 KB Output is correct
13 Correct 7 ms 23212 KB Output is correct
14 Correct 5 ms 23132 KB Output is correct
15 Correct 6 ms 22980 KB Output is correct
16 Correct 5 ms 23132 KB Output is correct
17 Correct 6 ms 23132 KB Output is correct
18 Correct 5 ms 23132 KB Output is correct
19 Correct 5 ms 22968 KB Output is correct
20 Correct 5 ms 23132 KB Output is correct
21 Correct 5 ms 23132 KB Output is correct
22 Correct 5 ms 23132 KB Output is correct
23 Correct 5 ms 23132 KB Output is correct
24 Correct 5 ms 23128 KB Output is correct
25 Correct 5 ms 23128 KB Output is correct
26 Correct 5 ms 23208 KB Output is correct
27 Correct 5 ms 23132 KB Output is correct
28 Correct 5 ms 23132 KB Output is correct
29 Correct 5 ms 23384 KB Output is correct
30 Correct 5 ms 23132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 255 ms 97620 KB Output is correct
2 Correct 113 ms 96760 KB Output is correct
3 Correct 211 ms 96588 KB Output is correct
4 Correct 113 ms 96848 KB Output is correct
5 Correct 267 ms 97484 KB Output is correct
6 Correct 108 ms 96636 KB Output is correct
7 Correct 220 ms 95564 KB Output is correct
8 Correct 117 ms 96040 KB Output is correct
9 Correct 225 ms 94548 KB Output is correct
10 Correct 192 ms 93460 KB Output is correct
11 Correct 213 ms 98388 KB Output is correct
12 Correct 217 ms 99228 KB Output is correct
13 Correct 177 ms 97016 KB Output is correct
14 Correct 213 ms 99608 KB Output is correct
15 Correct 194 ms 99440 KB Output is correct
16 Correct 200 ms 97492 KB Output is correct
17 Correct 193 ms 97900 KB Output is correct
18 Correct 184 ms 98076 KB Output is correct
19 Correct 191 ms 99120 KB Output is correct
20 Correct 195 ms 99840 KB Output is correct
21 Correct 5 ms 22872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22872 KB Output is correct
2 Correct 4 ms 22876 KB Output is correct
3 Correct 4 ms 22876 KB Output is correct
4 Correct 4 ms 23040 KB Output is correct
5 Correct 5 ms 22876 KB Output is correct
6 Correct 4 ms 22876 KB Output is correct
7 Correct 4 ms 22876 KB Output is correct
8 Correct 4 ms 22876 KB Output is correct
9 Correct 4 ms 22876 KB Output is correct
10 Correct 4 ms 22876 KB Output is correct
11 Correct 5 ms 23132 KB Output is correct
12 Correct 5 ms 23132 KB Output is correct
13 Correct 7 ms 23212 KB Output is correct
14 Correct 5 ms 23132 KB Output is correct
15 Correct 6 ms 22980 KB Output is correct
16 Correct 5 ms 23132 KB Output is correct
17 Correct 6 ms 23132 KB Output is correct
18 Correct 5 ms 23132 KB Output is correct
19 Correct 5 ms 22968 KB Output is correct
20 Correct 5 ms 23132 KB Output is correct
21 Correct 5 ms 23132 KB Output is correct
22 Correct 5 ms 23132 KB Output is correct
23 Correct 5 ms 23132 KB Output is correct
24 Correct 5 ms 23128 KB Output is correct
25 Correct 5 ms 23128 KB Output is correct
26 Correct 5 ms 23208 KB Output is correct
27 Correct 5 ms 23132 KB Output is correct
28 Correct 5 ms 23132 KB Output is correct
29 Correct 5 ms 23384 KB Output is correct
30 Correct 5 ms 23132 KB Output is correct
31 Correct 255 ms 97620 KB Output is correct
32 Correct 113 ms 96760 KB Output is correct
33 Correct 211 ms 96588 KB Output is correct
34 Correct 113 ms 96848 KB Output is correct
35 Correct 267 ms 97484 KB Output is correct
36 Correct 108 ms 96636 KB Output is correct
37 Correct 220 ms 95564 KB Output is correct
38 Correct 117 ms 96040 KB Output is correct
39 Correct 225 ms 94548 KB Output is correct
40 Correct 192 ms 93460 KB Output is correct
41 Correct 213 ms 98388 KB Output is correct
42 Correct 217 ms 99228 KB Output is correct
43 Correct 177 ms 97016 KB Output is correct
44 Correct 213 ms 99608 KB Output is correct
45 Correct 194 ms 99440 KB Output is correct
46 Correct 200 ms 97492 KB Output is correct
47 Correct 193 ms 97900 KB Output is correct
48 Correct 184 ms 98076 KB Output is correct
49 Correct 191 ms 99120 KB Output is correct
50 Correct 195 ms 99840 KB Output is correct
51 Correct 5 ms 22872 KB Output is correct
52 Correct 211 ms 97360 KB Output is correct
53 Correct 186 ms 97340 KB Output is correct
54 Correct 196 ms 97284 KB Output is correct
55 Correct 184 ms 97364 KB Output is correct
56 Correct 177 ms 97328 KB Output is correct
57 Correct 239 ms 97200 KB Output is correct
58 Correct 186 ms 97216 KB Output is correct
59 Correct 194 ms 97364 KB Output is correct
60 Correct 272 ms 96820 KB Output is correct
61 Correct 227 ms 96848 KB Output is correct
62 Correct 133 ms 100296 KB Output is correct
63 Correct 108 ms 100468 KB Output is correct
64 Correct 109 ms 99840 KB Output is correct
65 Correct 109 ms 100276 KB Output is correct
66 Correct 174 ms 96196 KB Output is correct
67 Correct 155 ms 95936 KB Output is correct
68 Correct 181 ms 96196 KB Output is correct
69 Correct 167 ms 96192 KB Output is correct
70 Correct 167 ms 95936 KB Output is correct
71 Correct 166 ms 96168 KB Output is correct
72 Correct 179 ms 96204 KB Output is correct
73 Correct 196 ms 95812 KB Output is correct
74 Correct 184 ms 96016 KB Output is correct
75 Correct 215 ms 96452 KB Output is correct
76 Correct 217 ms 100208 KB Output is correct
77 Correct 199 ms 100204 KB Output is correct
78 Correct 237 ms 97876 KB Output is correct
79 Correct 221 ms 96880 KB Output is correct
80 Correct 213 ms 99456 KB Output is correct
81 Correct 204 ms 98272 KB Output is correct
82 Correct 214 ms 98276 KB Output is correct
83 Correct 177 ms 97012 KB Output is correct
84 Correct 202 ms 99300 KB Output is correct
85 Correct 187 ms 98624 KB Output is correct
86 Correct 179 ms 97040 KB Output is correct
87 Correct 171 ms 97572 KB Output is correct
88 Correct 183 ms 98388 KB Output is correct
89 Correct 189 ms 96852 KB Output is correct
90 Correct 175 ms 96680 KB Output is correct
91 Correct 189 ms 97508 KB Output is correct
92 Correct 182 ms 97528 KB Output is correct
93 Correct 180 ms 97040 KB Output is correct
94 Correct 186 ms 96724 KB Output is correct
95 Correct 188 ms 97388 KB Output is correct
96 Correct 185 ms 97128 KB Output is correct
97 Correct 193 ms 97668 KB Output is correct