Submission #336618

# Submission time Handle Problem Language Result Execution time Memory
336618 2020-12-16T02:53:57 Z alishahali1382 Capital City (JOI20_capital_city) C++14
100 / 100
664 ms 42604 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=200010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans=inf, shit;
int C[MAXN], mark2[MAXN], mark3[MAXN], ted[MAXN];
bool dead[MAXN], dead2[MAXN], mark[MAXN];
int par[MAXN], sz[MAXN];
vector<int> G[MAXN], V[MAXN];

int dfs1(int node, int par){
	sz[node]=1;
	for (int v:G[node]) if (v!=par && !dead[v]) sz[node]+=dfs1(v, node);
	return sz[node];
}
int Centroid(int node, int par, int n){
	for (int v:G[node]) if (v!=par && !dead[v] && sz[v]*2>n) return Centroid(v, node, n);
	return node;
}
inline void addc(int c){
	if (mark2[c]==shit) ted[c]++;
	else mark2[c]=shit, ted[c]=1;
}
void dfs2(int node){
	mark[node]=0;
	addc(C[node]);
	for (int v:G[node]) if (v!=par[node] && !dead[v]){
		par[v]=node;
		dfs2(v);
	}
}
void Decompose(int root){
	root=Centroid(root, root, dfs1(root, root));
//	debug(root)
	shit++;
	if (!dead2[C[root]]){
		par[root]=0;
		dfs2(root);
		vector<int> vec={C[root]};
		mark3[C[root]]=shit;
		int res=0, bad=0;
		while (vec.size() && !bad){
			int c=vec.back();
			vec.pop_back();
			if (V[c].size()!=ted[c] || dead2[c]){
				bad=1;
				break ;
			}
//			debug(c)
			res++;
			for (int v:V[c]){
				while (v && !mark[v]){
					mark[v]=1;
					if (mark3[C[v]]!=shit){
						int cc=C[v];
						vec.pb(cc);
						mark3[cc]=shit;
					}
					v=par[v];
				}
				if (bad) break ;
			}
		}
		dead2[C[root]]=1;
		if (!bad) ans=min(ans, res);
	}
	dead[root]=1;
	for (int v:G[root]) if (!dead[v]) Decompose(v);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for (int i=1; i<n; i++){
		cin>>u>>v;
		G[u].pb(v);
		G[v].pb(u);
	}
	for (int i=1; i<=n; i++) cin>>C[i], V[C[i]].pb(i);
	Decompose(1);
	cout<<ans-1<<"\n";
	
	return 0;
}

Compilation message

capital_city.cpp: In function 'void Decompose(int)':
capital_city.cpp:65:19: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   65 |    if (V[c].size()!=ted[c] || dead2[c]){
      |        ~~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 8 ms 9964 KB Output is correct
12 Correct 9 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 8 ms 9984 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 8 ms 9964 KB Output is correct
18 Correct 8 ms 9964 KB Output is correct
19 Correct 8 ms 9964 KB Output is correct
20 Correct 8 ms 9964 KB Output is correct
21 Correct 9 ms 9964 KB Output is correct
22 Correct 9 ms 10108 KB Output is correct
23 Correct 9 ms 9972 KB Output is correct
24 Correct 9 ms 9984 KB Output is correct
25 Correct 9 ms 9964 KB Output is correct
26 Correct 9 ms 10092 KB Output is correct
27 Correct 9 ms 9964 KB Output is correct
28 Correct 11 ms 9964 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 9 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 551 ms 38380 KB Output is correct
2 Correct 184 ms 38764 KB Output is correct
3 Correct 539 ms 38088 KB Output is correct
4 Correct 185 ms 38764 KB Output is correct
5 Correct 510 ms 35236 KB Output is correct
6 Correct 189 ms 42604 KB Output is correct
7 Correct 514 ms 39020 KB Output is correct
8 Correct 189 ms 42088 KB Output is correct
9 Correct 614 ms 36836 KB Output is correct
10 Correct 628 ms 34240 KB Output is correct
11 Correct 611 ms 37256 KB Output is correct
12 Correct 662 ms 39872 KB Output is correct
13 Correct 618 ms 33828 KB Output is correct
14 Correct 653 ms 40164 KB Output is correct
15 Correct 644 ms 40036 KB Output is correct
16 Correct 664 ms 35168 KB Output is correct
17 Correct 629 ms 35556 KB Output is correct
18 Correct 615 ms 35720 KB Output is correct
19 Correct 612 ms 38852 KB Output is correct
20 Correct 632 ms 41096 KB Output is correct
21 Correct 7 ms 9836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9836 KB Output is correct
2 Correct 7 ms 9836 KB Output is correct
3 Correct 7 ms 9836 KB Output is correct
4 Correct 7 ms 9836 KB Output is correct
5 Correct 7 ms 9836 KB Output is correct
6 Correct 7 ms 9836 KB Output is correct
7 Correct 7 ms 9836 KB Output is correct
8 Correct 7 ms 9836 KB Output is correct
9 Correct 7 ms 9836 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 8 ms 9964 KB Output is correct
12 Correct 9 ms 9964 KB Output is correct
13 Correct 9 ms 9964 KB Output is correct
14 Correct 8 ms 9984 KB Output is correct
15 Correct 8 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 8 ms 9964 KB Output is correct
18 Correct 8 ms 9964 KB Output is correct
19 Correct 8 ms 9964 KB Output is correct
20 Correct 8 ms 9964 KB Output is correct
21 Correct 9 ms 9964 KB Output is correct
22 Correct 9 ms 10108 KB Output is correct
23 Correct 9 ms 9972 KB Output is correct
24 Correct 9 ms 9984 KB Output is correct
25 Correct 9 ms 9964 KB Output is correct
26 Correct 9 ms 10092 KB Output is correct
27 Correct 9 ms 9964 KB Output is correct
28 Correct 11 ms 9964 KB Output is correct
29 Correct 9 ms 9964 KB Output is correct
30 Correct 9 ms 9964 KB Output is correct
31 Correct 551 ms 38380 KB Output is correct
32 Correct 184 ms 38764 KB Output is correct
33 Correct 539 ms 38088 KB Output is correct
34 Correct 185 ms 38764 KB Output is correct
35 Correct 510 ms 35236 KB Output is correct
36 Correct 189 ms 42604 KB Output is correct
37 Correct 514 ms 39020 KB Output is correct
38 Correct 189 ms 42088 KB Output is correct
39 Correct 614 ms 36836 KB Output is correct
40 Correct 628 ms 34240 KB Output is correct
41 Correct 611 ms 37256 KB Output is correct
42 Correct 662 ms 39872 KB Output is correct
43 Correct 618 ms 33828 KB Output is correct
44 Correct 653 ms 40164 KB Output is correct
45 Correct 644 ms 40036 KB Output is correct
46 Correct 664 ms 35168 KB Output is correct
47 Correct 629 ms 35556 KB Output is correct
48 Correct 615 ms 35720 KB Output is correct
49 Correct 612 ms 38852 KB Output is correct
50 Correct 632 ms 41096 KB Output is correct
51 Correct 7 ms 9836 KB Output is correct
52 Correct 369 ms 24428 KB Output is correct
53 Correct 414 ms 24368 KB Output is correct
54 Correct 392 ms 24300 KB Output is correct
55 Correct 393 ms 24684 KB Output is correct
56 Correct 366 ms 24300 KB Output is correct
57 Correct 380 ms 24476 KB Output is correct
58 Correct 492 ms 28908 KB Output is correct
59 Correct 452 ms 29144 KB Output is correct
60 Correct 569 ms 28524 KB Output is correct
61 Correct 614 ms 28268 KB Output is correct
62 Correct 189 ms 42604 KB Output is correct
63 Correct 186 ms 42476 KB Output is correct
64 Correct 184 ms 42220 KB Output is correct
65 Correct 183 ms 42476 KB Output is correct
66 Correct 297 ms 33508 KB Output is correct
67 Correct 309 ms 33352 KB Output is correct
68 Correct 297 ms 33372 KB Output is correct
69 Correct 302 ms 33380 KB Output is correct
70 Correct 298 ms 33252 KB Output is correct
71 Correct 299 ms 33252 KB Output is correct
72 Correct 290 ms 33252 KB Output is correct
73 Correct 290 ms 32484 KB Output is correct
74 Correct 290 ms 33508 KB Output is correct
75 Correct 293 ms 33380 KB Output is correct
76 Correct 602 ms 32668 KB Output is correct
77 Correct 582 ms 30700 KB Output is correct
78 Correct 606 ms 35592 KB Output is correct
79 Correct 594 ms 33416 KB Output is correct
80 Correct 611 ms 40456 KB Output is correct
81 Correct 613 ms 37032 KB Output is correct
82 Correct 598 ms 37028 KB Output is correct
83 Correct 613 ms 33892 KB Output is correct
84 Correct 604 ms 39620 KB Output is correct
85 Correct 601 ms 38292 KB Output is correct
86 Correct 617 ms 33288 KB Output is correct
87 Correct 583 ms 35108 KB Output is correct
88 Correct 495 ms 36204 KB Output is correct
89 Correct 468 ms 32364 KB Output is correct
90 Correct 509 ms 32072 KB Output is correct
91 Correct 503 ms 34428 KB Output is correct
92 Correct 466 ms 33132 KB Output is correct
93 Correct 463 ms 33004 KB Output is correct
94 Correct 497 ms 31980 KB Output is correct
95 Correct 485 ms 33644 KB Output is correct
96 Correct 493 ms 32236 KB Output is correct
97 Correct 494 ms 34412 KB Output is correct