Submission #217527

# Submission time Handle Problem Language Result Execution time Memory
217527 2020-03-30T07:03:03 Z BThero Capital City (JOI20_capital_city) C++17
11 / 100
3000 ms 213024 KB
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>

#define __DBG 1
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (__DBG) cerr << #x << " = " << x << endl;

using namespace std;
//using namespace __gnu_pbds;

//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = (int)2e5 + 5;
const int K = 18;

vector<int> adj[MAXN], adj2[MAXN], adj3[MAXN], ord;
int tin[MAXN], tout[MAXN], timer;
int par[MAXN][K];
pii e[MAXN];
int n, m, nn;
int col[MAXN], col2[MAXN], sz[MAXN];
int boss[MAXN];
bool used[MAXN], leaf[MAXN];

void dfs(int v, int pr) {
	par[v][0] = pr;
	
	for (int i = 1; i < K; ++i) {
		par[v][i] = par[par[v][i - 1]][i - 1];
	}
	
	tin[v] = ++timer;
	
	for (int to : adj[v]) {
		if (to != pr) {
			dfs(to, v);
		}
	}
	
	tout[v] = timer;
}

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

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

void dfs2(int v) {
	used[v] = 1;
	
	for (int to : adj2[v]) {
		if (!used[to]) {
			dfs2(to);
		}
	}
	
	ord.pb(v);
}

void dfs3(int v) {
	col2[v] = nn;
	++sz[nn];
	used[v] = 1;
	
	for (int to : adj3[v]) {
		if (!used[to]) {
			dfs3(to);
		}
	}
}

void addEdge(int u, int v) {
	//printf("! %d %d\n", u, v);
	adj2[u].pb(v);
	adj3[v].pb(u);
}

void solve() {
	scanf("%d %d", &n, &m);
	
	for (int i = 1; i < n; ++i) {
		int u, v;
		scanf("%d %d", &u, &v);
		e[i] = mp(u, v);
		adj[u].pb(v);
		adj[v].pb(u);
	}
	
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &col[i]);
	}
	
	dfs(1, 1);
	
	for (int i = 1; i <= n; ++i) {
		if (boss[col[i]] == 0) {
			boss[col[i]] = i;
		}
		else {
			boss[col[i]] = lca(boss[col[i]], i);
		}
	}
	
	for (int i = 1; i <= m; ++i) {
		for (int j = 1; j <= n; ++j) {
			used[j] = 0;
		}
		
		for (int j = 1; j <= n; ++j) {
			if (col[j] == i) {
				for (int x = j;; x = par[x][0]) {
					used[x] = 1;
					
					if (x == boss[i]) {
						break;
					}
				}
			}
		}
		
		for (int j = 1; j <= n; ++j) {
			if (used[j]) {
				addEdge(i, col[j]);
			}
		}
	}
	
	for (int i = 1; i <= m; ++i) {
		used[i] = 0;
	}
	
	for (int i = 1; i <= m; ++i) {
		if (!used[i]) {
			dfs2(i);
		}
	}
	
	for (int i = 1; i <= m; ++i) {
		used[i] = 0;
	}
	
	for (int i = 1; i <= m; ++i) {
		int v = ord[m - i];
		
		if (!used[v]) {
			++nn;
			dfs3(v);
		}
	}
	
	for (int i = 1; i <= nn; ++i) {
		leaf[i] = 1;
	}
	
	for (int i = 1; i <= m; ++i) {
		for (int to : adj2[i]) {
			int a = col2[i];
			int b = col2[to];
			
			if (a != b) {
				leaf[a] = 0;
			}
		}
	}
	
	int ans = m;
	
	for (int i = 1; i <= nn; ++i) {
		if (leaf[i]) {
			ans = min(ans, sz[i]);
		}
	}
	
	printf("%d\n", ans - 1);
}

int main() {
	int tt = 1;
	
	while (tt--) {
		solve();
	}

	return 0;
}

Compilation message

capital_city.cpp: In function 'void solve()':
capital_city.cpp:106:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &u, &v);
   ~~~~~^~~~~~~~~~~~~~~~~
capital_city.cpp:117:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &col[i]);
   ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 15 ms 14464 KB Output is correct
7 Correct 12 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 15 ms 14464 KB Output is correct
7 Correct 12 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 14 ms 14720 KB Output is correct
12 Correct 14 ms 14848 KB Output is correct
13 Correct 14 ms 14848 KB Output is correct
14 Correct 15 ms 14848 KB Output is correct
15 Correct 18 ms 15104 KB Output is correct
16 Correct 21 ms 15744 KB Output is correct
17 Correct 24 ms 16756 KB Output is correct
18 Correct 28 ms 16876 KB Output is correct
19 Correct 24 ms 16884 KB Output is correct
20 Correct 24 ms 16888 KB Output is correct
21 Correct 25 ms 16884 KB Output is correct
22 Correct 39 ms 25464 KB Output is correct
23 Correct 49 ms 24824 KB Output is correct
24 Correct 17 ms 14848 KB Output is correct
25 Correct 18 ms 14976 KB Output is correct
26 Correct 18 ms 14720 KB Output is correct
27 Correct 19 ms 14976 KB Output is correct
28 Correct 18 ms 15360 KB Output is correct
29 Correct 20 ms 15360 KB Output is correct
30 Correct 19 ms 15232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 3071 ms 213024 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 12 ms 14464 KB Output is correct
2 Correct 12 ms 14464 KB Output is correct
3 Correct 12 ms 14464 KB Output is correct
4 Correct 12 ms 14464 KB Output is correct
5 Correct 12 ms 14464 KB Output is correct
6 Correct 15 ms 14464 KB Output is correct
7 Correct 12 ms 14464 KB Output is correct
8 Correct 12 ms 14464 KB Output is correct
9 Correct 12 ms 14464 KB Output is correct
10 Correct 12 ms 14464 KB Output is correct
11 Correct 14 ms 14720 KB Output is correct
12 Correct 14 ms 14848 KB Output is correct
13 Correct 14 ms 14848 KB Output is correct
14 Correct 15 ms 14848 KB Output is correct
15 Correct 18 ms 15104 KB Output is correct
16 Correct 21 ms 15744 KB Output is correct
17 Correct 24 ms 16756 KB Output is correct
18 Correct 28 ms 16876 KB Output is correct
19 Correct 24 ms 16884 KB Output is correct
20 Correct 24 ms 16888 KB Output is correct
21 Correct 25 ms 16884 KB Output is correct
22 Correct 39 ms 25464 KB Output is correct
23 Correct 49 ms 24824 KB Output is correct
24 Correct 17 ms 14848 KB Output is correct
25 Correct 18 ms 14976 KB Output is correct
26 Correct 18 ms 14720 KB Output is correct
27 Correct 19 ms 14976 KB Output is correct
28 Correct 18 ms 15360 KB Output is correct
29 Correct 20 ms 15360 KB Output is correct
30 Correct 19 ms 15232 KB Output is correct
31 Execution timed out 3071 ms 213024 KB Time limit exceeded
32 Halted 0 ms 0 KB -