Submission #366040

# Submission time Handle Problem Language Result Execution time Memory
366040 2021-02-12T19:54:13 Z negar_a Capital City (JOI20_capital_city) C++14
11 / 100
3000 ms 38508 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <int, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second

const int maxn = 2e5 + 5;
vector <int> adj[maxn], vec[maxn];
int sz[maxn], par[maxn], c[maxn];
int ans;
bool cent[maxn], mark[maxn], com[maxn], col[maxn];

void dfs(int v, int p = -1){
	sz[v] = 1;
	com[v] = true;
	for(int u : adj[v]){
		if(!cent[u] && u != p){
			dfs(u, v);
			sz[v] += sz[u];
		}
	}
}
int find_centroid(int v, int s, int p = -1){
	for(int u : adj[v]){
		if(!cent[u] && u != p && sz[u] > s / 2){
			return find_centroid(u, s, v);
		}
	}
	return v;
}
void dfs2(int v, int p = -1){
	for(int u : adj[v]){
		if(!cent[u] && u != p){
			par[u] = v;
			dfs2(u, v);
		}
	}
}
void clean(int v, int p = -1){
	com[v] = par[v] = sz[v] = mark[v] = 0;
	for(int u : adj[v]){
		if(u != p && !cent[u])
			clean(u, v);
	}
}

void solve(int v){
	dfs(v);
	int cn = find_centroid(v, sz[v]);
//	cout << "cen : " << cn << endl; 
	
	par[cn] = -1;
	dfs2(cn);
	
	queue <int> q;
	vector <int> vc;
	q.push(cn);
	
	bool flag = true; 
	while(q.size() && flag){
		int x = q.front();
		q.pop();
		if(!col[c[x]]){
			col[c[x]] = true;
			vc.pb(c[x]);
			for(int u : vec[c[x]]){
				if(!com[u]){
					flag = false;
					break;
				}
				if(!mark[u]){
					q.push(u);
				}
			}	
		}
		if(par[x] != -1){
			if(!com[par[x]]){
				flag = false;
				break;
			}
			if(!mark[par[x]]){
				q.push(par[x]);
			}
		}
	}
	if(flag)
		ans = min(ans, (int)vc.size() - 1);
		
	//clear
	for(int u : vc){
		col[u] = false;
	}
	clean(v);
		
	cent[cn] = true;
	for(int u : adj[cn]){
		if(!cent[u]){
			solve(u);
		}
	}
}

int main(){
	fast_io;
	int n, k;
	cin >> n >> k;
	for(int i = 0; i < n - 1; i ++){
		int x, y;
		cin >> x >> y;
		x --; y --;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	for(int i = 0; i < n; i ++){
		cin >> c[i];
		vec[c[i]].pb(i);
	}
	ans = k - 1;
	solve(0);
	cout << ans;
	
	return 0;
}
/*
6 3
2 1
3 5
6 2
3 4
2 3
1 3 1 2 3 2

8 4
4 1
1 3
3 6
6 7
7 2
2 5
5 8
2 4 3 1 1 2 3 4

12 4
7 9
1 3
4 6
2 4
10 12
1 2
2 10
11 1
2 8
5 3
6 7
3 1 1 2 4 3 3 2 2 3 4 4
*/

Compilation message

capital_city.cpp: In function 'void clean(int, int)':
capital_city.cpp:46:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   46 |  com[v] = par[v] = sz[v] = mark[v] = 0;
      |           ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
11 Correct 9 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 9 ms 9964 KB Output is correct
15 Correct 9 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 10 ms 9964 KB Output is correct
18 Correct 10 ms 9984 KB Output is correct
19 Correct 10 ms 9964 KB Output is correct
20 Correct 10 ms 10092 KB Output is correct
21 Correct 10 ms 9964 KB Output is correct
22 Correct 8 ms 10092 KB Output is correct
23 Correct 9 ms 9964 KB Output is correct
24 Correct 10 ms 9964 KB Output is correct
25 Correct 13 ms 9964 KB Output is correct
26 Correct 14 ms 9964 KB Output is correct
27 Correct 14 ms 9964 KB Output is correct
28 Correct 15 ms 9964 KB Output is correct
29 Correct 11 ms 9984 KB Output is correct
30 Correct 15 ms 9964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 716 ms 37996 KB Output is correct
2 Correct 238 ms 38380 KB Output is correct
3 Correct 681 ms 37868 KB Output is correct
4 Correct 243 ms 38508 KB Output is correct
5 Correct 2207 ms 35548 KB Output is correct
6 Correct 275 ms 38380 KB Output is correct
7 Execution timed out 3063 ms 35528 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 6 ms 9708 KB Output is correct
4 Correct 6 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 6 ms 9708 KB Output is correct
11 Correct 9 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 9 ms 9964 KB Output is correct
15 Correct 9 ms 9964 KB Output is correct
16 Correct 9 ms 9964 KB Output is correct
17 Correct 10 ms 9964 KB Output is correct
18 Correct 10 ms 9984 KB Output is correct
19 Correct 10 ms 9964 KB Output is correct
20 Correct 10 ms 10092 KB Output is correct
21 Correct 10 ms 9964 KB Output is correct
22 Correct 8 ms 10092 KB Output is correct
23 Correct 9 ms 9964 KB Output is correct
24 Correct 10 ms 9964 KB Output is correct
25 Correct 13 ms 9964 KB Output is correct
26 Correct 14 ms 9964 KB Output is correct
27 Correct 14 ms 9964 KB Output is correct
28 Correct 15 ms 9964 KB Output is correct
29 Correct 11 ms 9984 KB Output is correct
30 Correct 15 ms 9964 KB Output is correct
31 Correct 716 ms 37996 KB Output is correct
32 Correct 238 ms 38380 KB Output is correct
33 Correct 681 ms 37868 KB Output is correct
34 Correct 243 ms 38508 KB Output is correct
35 Correct 2207 ms 35548 KB Output is correct
36 Correct 275 ms 38380 KB Output is correct
37 Execution timed out 3063 ms 35528 KB Time limit exceeded
38 Halted 0 ms 0 KB -