Submission #414312

# Submission time Handle Problem Language Result Execution time Memory
414312 2021-05-30T10:11:43 Z ritul_kr_singh Capital City (JOI20_capital_city) C++17
11 / 100
3000 ms 37316 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'

const int MAXN = 2e5;

int n, k, color[MAXN], par[MAXN], s[MAXN], ans = MAXN;
vector<int> g[MAXN], h[MAXN];
bitset<MAXN> r, vis;

void calcSize(int u){
	s[u] = 1;
	for(int v : g[u]) if(!s[v]) calcSize(v), s[u] += s[v];
}

int find(int u, int p, int sz){
	s[p] -= s[u], s[u] = sz;
	for(int v : g[u]) if(!r[v] && s[v] > sz/2) return find(v, u, sz);
	return u;
}

void dfs(int u, int p, bool val){
	par[u] = val ? p : -1;
	for(int v : g[u]) if(v != p && !r[v]) dfs(v, u, val);
}

void decompose(int c){
	r[c = find(c, c, s[c])] = 1;
	dfs(c, c, 1);

	int res = 0;
	vector<int> rollback = {color[c]};
	vis[color[c]] = 1;
	queue<int> q;
	for(int i : h[color[c]]){
		if(par[i] < 0) res = MAXN;
		else q.push(i);
	}

	while(!q.empty() && res < MAXN){
		int u = q.front(); q.pop();
		if(!vis[color[par[u]]]){
			++res;
			rollback.push_back(color[par[u]]);
			vis[color[par[u]]] = 1;
			for(int v : h[color[par[u]]]){
				if(par[v] >= 0 && res < MAXN) q.push(v);
				else res = MAXN;
			}
		}
	}
	for(int i : rollback) vis[i] = 0;
	vector<int> ().swap(rollback);
	ans = min(ans, res);

	dfs(c, c, 0);
	for(int v : g[c]) if(!r[v]) decompose(v);
}

signed main(){
	cin.tie(0)->sync_with_stdio(0);
	int x, y; cin >> n >> k;
	for(int i=1; i<n; ++i){
		cin >> x >> y; --x, --y;
		g[x].push_back(y);
		g[y].push_back(x);
	}

	for(int i=0; i<n; ++i){
		cin >> color[i]; --color[i];
		h[color[i]].push_back(i);
	}

	calcSize(0);
	decompose(0);

	cout << ans;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9724 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 8 ms 9676 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 6 ms 9720 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9724 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 8 ms 9676 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 6 ms 9720 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9720 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9804 KB Output is correct
14 Correct 8 ms 9932 KB Output is correct
15 Correct 8 ms 9932 KB Output is correct
16 Correct 10 ms 9852 KB Output is correct
17 Correct 9 ms 9932 KB Output is correct
18 Correct 9 ms 9932 KB Output is correct
19 Correct 9 ms 9972 KB Output is correct
20 Correct 9 ms 9956 KB Output is correct
21 Correct 9 ms 9856 KB Output is correct
22 Correct 8 ms 9992 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 8 ms 9932 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 9 ms 9916 KB Output is correct
28 Correct 9 ms 9860 KB Output is correct
29 Correct 9 ms 9932 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 525 ms 36580 KB Output is correct
2 Correct 200 ms 36832 KB Output is correct
3 Correct 519 ms 36388 KB Output is correct
4 Correct 203 ms 36884 KB Output is correct
5 Correct 971 ms 34940 KB Output is correct
6 Correct 236 ms 36852 KB Output is correct
7 Correct 1714 ms 35428 KB Output is correct
8 Correct 1229 ms 37316 KB Output is correct
9 Execution timed out 3069 ms 34356 KB Time limit exceeded
10 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 6 ms 9676 KB Output is correct
4 Correct 7 ms 9724 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 8 ms 9676 KB Output is correct
7 Correct 6 ms 9724 KB Output is correct
8 Correct 6 ms 9720 KB Output is correct
9 Correct 6 ms 9676 KB Output is correct
10 Correct 6 ms 9720 KB Output is correct
11 Correct 8 ms 9856 KB Output is correct
12 Correct 8 ms 9804 KB Output is correct
13 Correct 8 ms 9804 KB Output is correct
14 Correct 8 ms 9932 KB Output is correct
15 Correct 8 ms 9932 KB Output is correct
16 Correct 10 ms 9852 KB Output is correct
17 Correct 9 ms 9932 KB Output is correct
18 Correct 9 ms 9932 KB Output is correct
19 Correct 9 ms 9972 KB Output is correct
20 Correct 9 ms 9956 KB Output is correct
21 Correct 9 ms 9856 KB Output is correct
22 Correct 8 ms 9992 KB Output is correct
23 Correct 8 ms 9932 KB Output is correct
24 Correct 8 ms 9856 KB Output is correct
25 Correct 8 ms 9932 KB Output is correct
26 Correct 9 ms 9984 KB Output is correct
27 Correct 9 ms 9916 KB Output is correct
28 Correct 9 ms 9860 KB Output is correct
29 Correct 9 ms 9932 KB Output is correct
30 Correct 9 ms 9932 KB Output is correct
31 Correct 525 ms 36580 KB Output is correct
32 Correct 200 ms 36832 KB Output is correct
33 Correct 519 ms 36388 KB Output is correct
34 Correct 203 ms 36884 KB Output is correct
35 Correct 971 ms 34940 KB Output is correct
36 Correct 236 ms 36852 KB Output is correct
37 Correct 1714 ms 35428 KB Output is correct
38 Correct 1229 ms 37316 KB Output is correct
39 Execution timed out 3069 ms 34356 KB Time limit exceeded
40 Halted 0 ms 0 KB -