답안 #938181

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
938181 2024-03-05T01:42:10 Z pcc 수도 (JOI20_capital_city) C++14
100 / 100
853 ms 272904 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>

const int C = 70;
const int mxn = 2e5+10;
vector<int> tree[mxn];
int par[mxn][20],id[mxn][20],dep[mxn];
int to[mxn*C],nid[mxn*C],head[mxn*C];
int col[mxn];
int N,K;
int ppp = 0;
int wei[mxn*C];
vector<int> v[mxn];
int pt;

namespace BUILD{
	void add_edge(int a,int b){
		ppp++;
		nid[ppp] = head[a];
		head[a] = ppp;
		to[ppp] = b;
		return;
	}

	void dfs(int now){
		id[now][0] = ++pt;
		add_edge(id[now][0],now);
		add_edge(id[now][0],par[now][0]);
		for(int i = 1;i<20;i++){
			id[now][i] = ++pt;
			par[now][i] = par[par[now][i-1]][i-1];
			add_edge(id[now][i],id[now][i-1]);
			add_edge(id[now][i],id[par[now][i-1]][i-1]);
		}
		for(auto nxt:tree[now]){
			if(nxt == par[now][0])continue;
			par[nxt][0] = now;
			dep[nxt] = dep[now]+1;
			dfs(nxt);
		}
		return;
	}
	int LCA(int a,int b){
		if(dep[a]<dep[b])swap(a,b);
		int d = dep[a]-dep[b];
		for(int i = 19;i>=0;i--){
			if(d&(1<<i))a = par[a][i];
		}
		for(int i = 19;i>=0;i--){
			if(par[a][i] != par[b][i])a = par[a][i],b = par[b][i];
		}
		return (a==b?a:par[a][0]);
	}

	void add(int a,int d){
		int now = a;
		for(int i = 19;i>=0;i--){
			if(d&(1<<i)){
				add_edge(a,id[now][i]);
				now = par[now][i];
			}
		}
		return;
	}

	void GO(){
		par[1][0] = 1;
		dfs(1);
		//cout<<"HH"<<endl;
		for(int i = 1;i<=K;i++){
			if(v[i].empty())continue;
			int now = v[i][0];
			for(auto &j:v[i]){
				add_edge(j,i+N);
				add_edge(i+N,j);
				now = LCA(now,j);
			}
			//cout<<i<<":"<<endl;for(auto &j:v[i])cout<<j<<',';cout<<now<<endl;
			for(auto &j:v[i]){
				add(j,dep[j]-dep[now]);
			}
		}
		return;
	}
	void PRINT(){
		for(int i = 1;i<=pt;i++){
			cout<<i<<":";
			for(int eid = head[i];eid;eid = nid[eid]){
				cout<<to[eid]<<',';
			}cout<<endl;
		}
		return;
	}

}

namespace SCC{
	int idx[mxn*C],low[mxn*C],cnt = 0,gid[mxn*C],gcnt = 0;
	vector<int> st;
	int in[mxn*C],out[mxn*C];
	int dp[mxn*C];
	bitset<mxn*C> vis;

	void dfs(int now){
		assert(!vis[now]);
		vis[now] = true;
		idx[now] = low[now] = ++cnt;
		st.push_back(now);
		for(int eid = head[now];eid;eid = nid[eid]){
			int nxt = to[eid];
			if(gid[nxt])continue;
			if(!idx[nxt]){
				dfs(nxt);
				low[now] = min(low[now],low[nxt]);
			}
			else low[now] = min(low[now],idx[nxt]);
		}
		if(idx[now] == low[now]){
			gcnt++;
			while(st.back() != now){
				gid[st.back()] = gcnt;
				st.pop_back();
			}
			gid[st.back()] = gcnt;
			st.pop_back();
		}
		return;
	}
	void GO(){
		for(int i = 1;i<=pt;i++){
			if(!gid[i])dfs(i);
		}
		return;
	}
	void GETANS(){
		for(int i = 1;i<=pt;i++){
			dp[gid[i]]+=wei[i];
			for(int eid = head[i];eid;eid = nid[eid]){
				int nxt = to[eid];
				if(gid[nxt] == gid[i])continue;
				out[gid[i]]++;
				in[gid[nxt]]++;
			}
		}
		int re = 1e9;
		for(int i = 1;i<=gcnt;i++){
			if(out[i])continue;
			re = min(re,dp[i]);
			//cout<<"SCC:"<<i<<":"<<dp[i]<<endl;
		}
		cout<<re-1<<'\n';
		return;
	}
	void PRINT(){
		for(int i = 1;i<=pt;i++){
			cout<<i<<":"<<gid[i]<<endl;
		}
		return;
	}
}

int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin>>N>>K;
	for(int i = 1;i<N;i++){
		int a,b;
		cin>>a>>b;
		tree[a].push_back(b);
		tree[b].push_back(a);
	}
	for(int i = 1;i<=N;i++){
		cin>>col[i];
		v[col[i]].push_back(i);
	}
	for(int i = 1;i<=K;i++)wei[i+N] = 1;
	pt = N+K;
	//cout<<"HI"<<endl;
	BUILD::GO();
	//BUILD::PRINT();
	//cout<<"HI"<<endl;
	SCC::GO();
	//if(N>2000)return 0;
	//cout<<"HI"<<endl;
	//SCC::PRINT();
	SCC::GETANS();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33880 KB Output is correct
2 Correct 5 ms 33884 KB Output is correct
3 Correct 5 ms 33884 KB Output is correct
4 Correct 6 ms 33880 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33884 KB Output is correct
8 Correct 5 ms 33880 KB Output is correct
9 Correct 6 ms 34068 KB Output is correct
10 Correct 6 ms 33884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33880 KB Output is correct
2 Correct 5 ms 33884 KB Output is correct
3 Correct 5 ms 33884 KB Output is correct
4 Correct 6 ms 33880 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33884 KB Output is correct
8 Correct 5 ms 33880 KB Output is correct
9 Correct 6 ms 34068 KB Output is correct
10 Correct 6 ms 33884 KB Output is correct
11 Correct 8 ms 34652 KB Output is correct
12 Correct 10 ms 34536 KB Output is correct
13 Correct 8 ms 34652 KB Output is correct
14 Correct 10 ms 34736 KB Output is correct
15 Correct 8 ms 34392 KB Output is correct
16 Correct 9 ms 34396 KB Output is correct
17 Correct 7 ms 34652 KB Output is correct
18 Correct 7 ms 34648 KB Output is correct
19 Correct 8 ms 34648 KB Output is correct
20 Correct 7 ms 34652 KB Output is correct
21 Correct 7 ms 34652 KB Output is correct
22 Correct 7 ms 34756 KB Output is correct
23 Correct 8 ms 34396 KB Output is correct
24 Correct 7 ms 34652 KB Output is correct
25 Correct 8 ms 34652 KB Output is correct
26 Correct 9 ms 34528 KB Output is correct
27 Correct 8 ms 34648 KB Output is correct
28 Correct 8 ms 34652 KB Output is correct
29 Correct 8 ms 34904 KB Output is correct
30 Correct 8 ms 34652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 853 ms 265408 KB Output is correct
2 Correct 442 ms 270528 KB Output is correct
3 Correct 753 ms 264372 KB Output is correct
4 Correct 472 ms 270264 KB Output is correct
5 Correct 739 ms 253632 KB Output is correct
6 Correct 451 ms 269528 KB Output is correct
7 Correct 725 ms 259664 KB Output is correct
8 Correct 436 ms 264120 KB Output is correct
9 Correct 582 ms 240576 KB Output is correct
10 Correct 594 ms 236972 KB Output is correct
11 Correct 609 ms 240592 KB Output is correct
12 Correct 645 ms 245164 KB Output is correct
13 Correct 614 ms 240744 KB Output is correct
14 Correct 625 ms 245360 KB Output is correct
15 Correct 648 ms 242108 KB Output is correct
16 Correct 605 ms 240496 KB Output is correct
17 Correct 582 ms 241316 KB Output is correct
18 Correct 576 ms 242628 KB Output is correct
19 Correct 628 ms 245368 KB Output is correct
20 Correct 652 ms 246048 KB Output is correct
21 Correct 6 ms 33884 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 33880 KB Output is correct
2 Correct 5 ms 33884 KB Output is correct
3 Correct 5 ms 33884 KB Output is correct
4 Correct 6 ms 33880 KB Output is correct
5 Correct 6 ms 33884 KB Output is correct
6 Correct 6 ms 33884 KB Output is correct
7 Correct 7 ms 33884 KB Output is correct
8 Correct 5 ms 33880 KB Output is correct
9 Correct 6 ms 34068 KB Output is correct
10 Correct 6 ms 33884 KB Output is correct
11 Correct 8 ms 34652 KB Output is correct
12 Correct 10 ms 34536 KB Output is correct
13 Correct 8 ms 34652 KB Output is correct
14 Correct 10 ms 34736 KB Output is correct
15 Correct 8 ms 34392 KB Output is correct
16 Correct 9 ms 34396 KB Output is correct
17 Correct 7 ms 34652 KB Output is correct
18 Correct 7 ms 34648 KB Output is correct
19 Correct 8 ms 34648 KB Output is correct
20 Correct 7 ms 34652 KB Output is correct
21 Correct 7 ms 34652 KB Output is correct
22 Correct 7 ms 34756 KB Output is correct
23 Correct 8 ms 34396 KB Output is correct
24 Correct 7 ms 34652 KB Output is correct
25 Correct 8 ms 34652 KB Output is correct
26 Correct 9 ms 34528 KB Output is correct
27 Correct 8 ms 34648 KB Output is correct
28 Correct 8 ms 34652 KB Output is correct
29 Correct 8 ms 34904 KB Output is correct
30 Correct 8 ms 34652 KB Output is correct
31 Correct 853 ms 265408 KB Output is correct
32 Correct 442 ms 270528 KB Output is correct
33 Correct 753 ms 264372 KB Output is correct
34 Correct 472 ms 270264 KB Output is correct
35 Correct 739 ms 253632 KB Output is correct
36 Correct 451 ms 269528 KB Output is correct
37 Correct 725 ms 259664 KB Output is correct
38 Correct 436 ms 264120 KB Output is correct
39 Correct 582 ms 240576 KB Output is correct
40 Correct 594 ms 236972 KB Output is correct
41 Correct 609 ms 240592 KB Output is correct
42 Correct 645 ms 245164 KB Output is correct
43 Correct 614 ms 240744 KB Output is correct
44 Correct 625 ms 245360 KB Output is correct
45 Correct 648 ms 242108 KB Output is correct
46 Correct 605 ms 240496 KB Output is correct
47 Correct 582 ms 241316 KB Output is correct
48 Correct 576 ms 242628 KB Output is correct
49 Correct 628 ms 245368 KB Output is correct
50 Correct 652 ms 246048 KB Output is correct
51 Correct 6 ms 33884 KB Output is correct
52 Correct 517 ms 253884 KB Output is correct
53 Correct 491 ms 253644 KB Output is correct
54 Correct 494 ms 253660 KB Output is correct
55 Correct 455 ms 253624 KB Output is correct
56 Correct 482 ms 253640 KB Output is correct
57 Correct 461 ms 253592 KB Output is correct
58 Correct 510 ms 262584 KB Output is correct
59 Correct 547 ms 261216 KB Output is correct
60 Correct 603 ms 247152 KB Output is correct
61 Correct 612 ms 245952 KB Output is correct
62 Correct 448 ms 272904 KB Output is correct
63 Correct 445 ms 271556 KB Output is correct
64 Correct 417 ms 265980 KB Output is correct
65 Correct 453 ms 272752 KB Output is correct
66 Correct 436 ms 249680 KB Output is correct
67 Correct 463 ms 248272 KB Output is correct
68 Correct 445 ms 250184 KB Output is correct
69 Correct 435 ms 250100 KB Output is correct
70 Correct 449 ms 249716 KB Output is correct
71 Correct 446 ms 248944 KB Output is correct
72 Correct 445 ms 248692 KB Output is correct
73 Correct 442 ms 248120 KB Output is correct
74 Correct 432 ms 248460 KB Output is correct
75 Correct 437 ms 249304 KB Output is correct
76 Correct 488 ms 270024 KB Output is correct
77 Correct 450 ms 257512 KB Output is correct
78 Correct 552 ms 241592 KB Output is correct
79 Correct 583 ms 239536 KB Output is correct
80 Correct 588 ms 246448 KB Output is correct
81 Correct 603 ms 243428 KB Output is correct
82 Correct 595 ms 243896 KB Output is correct
83 Correct 572 ms 239028 KB Output is correct
84 Correct 615 ms 245196 KB Output is correct
85 Correct 557 ms 244896 KB Output is correct
86 Correct 606 ms 239792 KB Output is correct
87 Correct 580 ms 241124 KB Output is correct
88 Correct 579 ms 245364 KB Output is correct
89 Correct 564 ms 242532 KB Output is correct
90 Correct 555 ms 242756 KB Output is correct
91 Correct 549 ms 243636 KB Output is correct
92 Correct 570 ms 242416 KB Output is correct
93 Correct 562 ms 243384 KB Output is correct
94 Correct 547 ms 244564 KB Output is correct
95 Correct 562 ms 242604 KB Output is correct
96 Correct 541 ms 242184 KB Output is correct
97 Correct 575 ms 243376 KB Output is correct