답안 #428452

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
428452 2021-06-15T11:55:59 Z amunduzbaev 수도 (JOI20_capital_city) C++14
100 / 100
530 ms 91168 KB
/* made by amunduzbaev */
 
#include "bits/stdc++.h"
 
using namespace std;
 
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template<class T> using oset = tree<T, 
//~ null_type, less_equal<T>, rb_tree_tag, 
//~ tree_order_statistics_node_update>;
 
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define mem(arr, v) memset(arr, v, sizeof arr)
#define int long long
#define degub(x) cout<<#x<<" : "<<x<<"\n"
#define GG cout<<"here"<<endl;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii; 
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T> bool umax(T& a, const T& b) { return a < b ? a = b, true : false; }
template<int sz> using tut = array<int, sz>;
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);  
	freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
 
const int N = 2e5+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
 
#define MULTI 0
int n, m, k, s, t, q, ans, res, a[N];
int lca[N][20], tin[N], tout[N], llca[N];
int ns[N], d[N];
vii edges[N], qwe[N];

void predfs(int u, int p = -1){
	tin[u] = t++;
	for(int i=1;i<20;i++) lca[u][i] = lca[lca[u][i-1]][i-1];
	for(auto x : qwe[u]){
		if(x == p) continue;
		lca[x][0] = u, d[x] = d[u] + 1;
		if(a[x] == a[u]) ns[x] = ns[u];
		else ns[x] = u;
		predfs(x, u);
	} tout[u] = t - 1;
}

bool par(int a, int b) { return (tin[a] <= tin[b] && tout[b] <= tout[a]); }

int LCA(int a, int b){
	if(par(a, b)) return a;
	if(par(b, a)) return b;
	for(int i=19;i>=0;i--) if(!par(lca[a][i], b)) a = lca[a][i];
	a = lca[a][0];
	return a;
}

void dfs(int u, int p = -1){
	if(d[ns[u]] >= d[llca[a[u]]] && ns[u]) edges[a[u]].pb(a[ns[u]]);
	for(auto x : qwe[u]){
		if(x == p) continue;
		dfs(x, u);
	}
}
int low[N], up[N], used[N], cmp[N], last, cost[N];
stack<int> ss;
vv<vii> tt;

void scc_dfs(int u){ used[u] = 1;
	low[u] = up[u] = t++;
	ss.push(u);
	for(auto x : edges[u]){
		if(used[x] && !cmp[x]) umin(low[u], up[x]);
		else if(!used[x]) scc_dfs(x), umin(low[u], low[x]);
	}
	
	if(low[u] == up[u]){
		int cur; ++last;
		tt.pb({});
		do{
			cur = ss.top(); ss.pop();
			tt.back().pb(cur);
			cmp[cur] = last;
		}while(cur != u); 
		cost[last] = sz(tt.back());
	}
}

vii top;
void topsort(int u){ used[u] = 1;
	for(auto x : qwe[u]){
		if(!used[x]) topsort(x);
	} top.pb(u);
}

void solve(int t_case){
	cin>>n>>k;
	for(int i=1;i<n;i++){
		int a, b; cin>>a>>b;
		qwe[a].pb(b), qwe[b].pb(a);
	} for(int i=1;i<=n;i++) cin>>a[i];
	predfs(1);
	mem(llca, -1);
	for(int i=1;i<=n;i++){
		if(~llca[a[i]]) llca[a[i]] = LCA(llca[a[i]], i);
		else llca[a[i]] = i;
	} dfs(1);
	for(int i=1;i<=k;i++){
		set<int> ss;
		for(auto x : edges[i]) ss.insert(x);
		edges[i].clear();
		for(auto x : ss) edges[i].pb(x);
		//~ cout<<i<<" : ";
		//~ for(auto x : edges[i]) cout<<x<<" ";
		//~ cout<<endl;
	} 
	//~ for(int i=1;i<=n;i++) cout<<ns[i]<<" ";
	//~ cout<<endl;
	t = 0;
	
/*

6 3
2 1
3 5
6 2
3 4
2 3
1 3 1 2 3 2

*/

	
	for(int i=1;i<=k;i++){
		if(used[i]) continue;
		scc_dfs(i);
	}
	//~ GG
	for(int i=1;i<=n;i++) qwe[i].clear();
	for(auto xx : tt){
		for(auto u : xx){
			for(auto x : edges[u]){
				qwe[cmp[u]].pb(cmp[x]);
			} 
		}
	}
	
	//~ for(int i=1;i<=k;i++) cout<<cmp[i]<<" ";
	//~ cout<<"\n";
	
	//~ GG
	n = last; mem(used, 0);
	for(int i=1;i<=n;i++){
		set<int> ss;
		for(auto x : qwe[i]) ss.insert(x);
		qwe[i].clear();
		ss.erase(i);
		for(auto x : ss) qwe[i].pb(x);
		//~ cout<<i<<" : ";
		
		//~ for(auto x : ss) cout<<x<<" ";
		//~ cout<<"\n";
	}
	
	res = inf;
	for(int i=1;i<=n;i++) if(!used[i]) topsort(i);
	for(auto u : top){
		for(auto x : qwe[u]) cost[u] += cost[x];
		umin(res, cost[u]);
	} cout<<res - 1<<"\n";
}

signed main(){
	NeedForSpeed
	if(MULTI){
		int t; cin>>t;
		for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
	} else solve(1);
	return 0;
}

Compilation message

capital_city.cpp: In function 'void usaco(std::string)':
capital_city.cpp:38:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 | void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
capital_city.cpp:39:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |  freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
      |  ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12876 KB Output is correct
2 Correct 8 ms 12884 KB Output is correct
3 Correct 9 ms 12912 KB Output is correct
4 Correct 9 ms 12876 KB Output is correct
5 Correct 9 ms 12828 KB Output is correct
6 Correct 11 ms 12876 KB Output is correct
7 Correct 11 ms 12876 KB Output is correct
8 Correct 9 ms 12856 KB Output is correct
9 Correct 9 ms 12876 KB Output is correct
10 Correct 10 ms 12804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12876 KB Output is correct
2 Correct 8 ms 12884 KB Output is correct
3 Correct 9 ms 12912 KB Output is correct
4 Correct 9 ms 12876 KB Output is correct
5 Correct 9 ms 12828 KB Output is correct
6 Correct 11 ms 12876 KB Output is correct
7 Correct 11 ms 12876 KB Output is correct
8 Correct 9 ms 12856 KB Output is correct
9 Correct 9 ms 12876 KB Output is correct
10 Correct 10 ms 12804 KB Output is correct
11 Correct 12 ms 13500 KB Output is correct
12 Correct 11 ms 13388 KB Output is correct
13 Correct 10 ms 13388 KB Output is correct
14 Correct 12 ms 13456 KB Output is correct
15 Correct 12 ms 13540 KB Output is correct
16 Correct 12 ms 13516 KB Output is correct
17 Correct 10 ms 13512 KB Output is correct
18 Correct 11 ms 13476 KB Output is correct
19 Correct 11 ms 13560 KB Output is correct
20 Correct 12 ms 13452 KB Output is correct
21 Correct 12 ms 13488 KB Output is correct
22 Correct 10 ms 13596 KB Output is correct
23 Correct 11 ms 13516 KB Output is correct
24 Correct 12 ms 13592 KB Output is correct
25 Correct 11 ms 13504 KB Output is correct
26 Correct 11 ms 13516 KB Output is correct
27 Correct 11 ms 13580 KB Output is correct
28 Correct 11 ms 13472 KB Output is correct
29 Correct 10 ms 13532 KB Output is correct
30 Correct 10 ms 13516 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 516 ms 86940 KB Output is correct
2 Correct 202 ms 91048 KB Output is correct
3 Correct 507 ms 90276 KB Output is correct
4 Correct 259 ms 91104 KB Output is correct
5 Correct 473 ms 86740 KB Output is correct
6 Correct 209 ms 91020 KB Output is correct
7 Correct 441 ms 86500 KB Output is correct
8 Correct 223 ms 88880 KB Output is correct
9 Correct 453 ms 78764 KB Output is correct
10 Correct 439 ms 76368 KB Output is correct
11 Correct 426 ms 79216 KB Output is correct
12 Correct 430 ms 82020 KB Output is correct
13 Correct 451 ms 75968 KB Output is correct
14 Correct 521 ms 82108 KB Output is correct
15 Correct 440 ms 81976 KB Output is correct
16 Correct 441 ms 76964 KB Output is correct
17 Correct 439 ms 77548 KB Output is correct
18 Correct 440 ms 77668 KB Output is correct
19 Correct 440 ms 80804 KB Output is correct
20 Correct 467 ms 83028 KB Output is correct
21 Correct 10 ms 12900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12876 KB Output is correct
2 Correct 8 ms 12884 KB Output is correct
3 Correct 9 ms 12912 KB Output is correct
4 Correct 9 ms 12876 KB Output is correct
5 Correct 9 ms 12828 KB Output is correct
6 Correct 11 ms 12876 KB Output is correct
7 Correct 11 ms 12876 KB Output is correct
8 Correct 9 ms 12856 KB Output is correct
9 Correct 9 ms 12876 KB Output is correct
10 Correct 10 ms 12804 KB Output is correct
11 Correct 12 ms 13500 KB Output is correct
12 Correct 11 ms 13388 KB Output is correct
13 Correct 10 ms 13388 KB Output is correct
14 Correct 12 ms 13456 KB Output is correct
15 Correct 12 ms 13540 KB Output is correct
16 Correct 12 ms 13516 KB Output is correct
17 Correct 10 ms 13512 KB Output is correct
18 Correct 11 ms 13476 KB Output is correct
19 Correct 11 ms 13560 KB Output is correct
20 Correct 12 ms 13452 KB Output is correct
21 Correct 12 ms 13488 KB Output is correct
22 Correct 10 ms 13596 KB Output is correct
23 Correct 11 ms 13516 KB Output is correct
24 Correct 12 ms 13592 KB Output is correct
25 Correct 11 ms 13504 KB Output is correct
26 Correct 11 ms 13516 KB Output is correct
27 Correct 11 ms 13580 KB Output is correct
28 Correct 11 ms 13472 KB Output is correct
29 Correct 10 ms 13532 KB Output is correct
30 Correct 10 ms 13516 KB Output is correct
31 Correct 516 ms 86940 KB Output is correct
32 Correct 202 ms 91048 KB Output is correct
33 Correct 507 ms 90276 KB Output is correct
34 Correct 259 ms 91104 KB Output is correct
35 Correct 473 ms 86740 KB Output is correct
36 Correct 209 ms 91020 KB Output is correct
37 Correct 441 ms 86500 KB Output is correct
38 Correct 223 ms 88880 KB Output is correct
39 Correct 453 ms 78764 KB Output is correct
40 Correct 439 ms 76368 KB Output is correct
41 Correct 426 ms 79216 KB Output is correct
42 Correct 430 ms 82020 KB Output is correct
43 Correct 451 ms 75968 KB Output is correct
44 Correct 521 ms 82108 KB Output is correct
45 Correct 440 ms 81976 KB Output is correct
46 Correct 441 ms 76964 KB Output is correct
47 Correct 439 ms 77548 KB Output is correct
48 Correct 440 ms 77668 KB Output is correct
49 Correct 440 ms 80804 KB Output is correct
50 Correct 467 ms 83028 KB Output is correct
51 Correct 10 ms 12900 KB Output is correct
52 Correct 424 ms 69728 KB Output is correct
53 Correct 421 ms 69676 KB Output is correct
54 Correct 406 ms 69692 KB Output is correct
55 Correct 409 ms 69716 KB Output is correct
56 Correct 449 ms 69780 KB Output is correct
57 Correct 427 ms 69760 KB Output is correct
58 Correct 450 ms 75820 KB Output is correct
59 Correct 429 ms 76260 KB Output is correct
60 Correct 530 ms 77056 KB Output is correct
61 Correct 506 ms 76636 KB Output is correct
62 Correct 200 ms 91168 KB Output is correct
63 Correct 197 ms 91112 KB Output is correct
64 Correct 192 ms 89608 KB Output is correct
65 Correct 227 ms 90872 KB Output is correct
66 Correct 354 ms 77612 KB Output is correct
67 Correct 346 ms 77692 KB Output is correct
68 Correct 333 ms 77624 KB Output is correct
69 Correct 386 ms 77844 KB Output is correct
70 Correct 398 ms 77488 KB Output is correct
71 Correct 348 ms 77672 KB Output is correct
72 Correct 354 ms 77652 KB Output is correct
73 Correct 335 ms 76776 KB Output is correct
74 Correct 322 ms 77904 KB Output is correct
75 Correct 339 ms 77732 KB Output is correct
76 Correct 489 ms 82844 KB Output is correct
77 Correct 483 ms 80836 KB Output is correct
78 Correct 434 ms 77516 KB Output is correct
79 Correct 401 ms 75444 KB Output is correct
80 Correct 440 ms 82564 KB Output is correct
81 Correct 414 ms 78816 KB Output is correct
82 Correct 397 ms 79160 KB Output is correct
83 Correct 379 ms 75880 KB Output is correct
84 Correct 469 ms 81708 KB Output is correct
85 Correct 415 ms 80196 KB Output is correct
86 Correct 391 ms 75384 KB Output is correct
87 Correct 413 ms 77004 KB Output is correct
88 Correct 451 ms 79956 KB Output is correct
89 Correct 447 ms 75812 KB Output is correct
90 Correct 434 ms 75880 KB Output is correct
91 Correct 460 ms 78084 KB Output is correct
92 Correct 466 ms 76648 KB Output is correct
93 Correct 438 ms 76396 KB Output is correct
94 Correct 473 ms 75680 KB Output is correct
95 Correct 452 ms 77308 KB Output is correct
96 Correct 434 ms 76140 KB Output is correct
97 Correct 471 ms 77832 KB Output is correct