Submission #595745

# Submission time Handle Problem Language Result Execution time Memory
595745 2022-07-14T05:31:15 Z penguinhacker Capital City (JOI20_capital_city) C++17
11 / 100
2556 ms 524288 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ar array

const int mxN=2e5;
int n, k, c[mxN], d[mxN], anc[mxN][18], tin[mxN], t, who[20*mxN], cnt[20*mxN];
vector<int> oc[mxN], adj1[mxN], adj2[20*mxN], adj3[20*mxN], st;
bool vis[20*mxN], out[20*mxN];

void dfs1(int u=0) {
	tin[u]=t++;
	for (int i=1; i<18; ++i)
		anc[u][i]=anc[anc[u][i-1]][i-1];
	for (int v : adj1[u])
		if (v!=anc[u][0]) {
			d[v]=d[u]+1, anc[v][0]=u;
			dfs1(v);
		}
}

int lca(int u, int v) {
	if (d[u]>d[v])
		swap(u, v);
	for (int i=17; ~i; --i)
		if (d[v]-(1<<i)>=d[u])
			v=anc[v][i];
	if (u==v)
		return u;
	for (int i=17; ~i; --i)
		if (anc[u][i]!=anc[v][i])
			u=anc[u][i], v=anc[v][i];
	return anc[u][0];
}

void dfs2(int u) {
	vis[u]=1;
	for (int v : adj2[u])
		if (!vis[v])
			dfs2(v);
	st.push_back(u);
}

void dfs3(int u, int rt) {
	vis[u]=0, who[u]=rt;
	cnt[rt]+=u<k;
	for (int v : adj3[u])
		if (vis[v])
			dfs3(v, rt);
}

int cur_ans;

void dfs4(int u) {
	vis[u]=1;
	cur_ans+=u<k;
	for (int v : adj2[u])
		if (!vis[v])
			dfs4(v);
}

void ae(int u, int v) {
	//if (u<20) cout << u << " " << v << endl;
	adj2[u].push_back(v);
	adj3[v].push_back(u);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> k;
	for (int i=1; i<n; ++i) {
		int u, v;
		cin >> u >> v, --u, --v;
		adj1[u].push_back(v);
		adj1[v].push_back(u);
	}
	for (int i=0; i<n; ++i) {
		cin >> c[i], --c[i];
		oc[c[i]].push_back(i);
	}
	dfs1();
	for (int i=0; i<k; ++i) {
		sort(oc[i].begin(), oc[i].end(), [&](int a, int b) { return tin[a]<tin[b]; } );
		for (int j=1; j<oc[i].size(); ++j) {
			int u=oc[i][j-1], v=oc[i][j];
			int uv=lca(u, v);
			ae(i, c[uv]);
			for (int rep=0; rep<2; ++rep) {
				for (int j=17; ~j; --j)
					if (d[u]-(1<<j)>=d[uv]) {
						ae(i, j*n+u+k);
						u=anc[u][j];
					}
				swap(u, v);
			}
		}
	}
	for (int i=0; i<n; ++i) {
		ae(i+k, c[i]);
		for (int j=1; j<18; ++j) {
			ae(j*n+i+k, (j-1)*n+i+k);
			ae(j*n+i+k, (j-1)*n+anc[i][j-1]+k);
		}
	}
	for (int i=0; i<k; ++i)
		if (!vis[i])
			dfs2(i);
	reverse(st.begin(), st.end());
	for (int i : st)
		if (vis[i])
			dfs3(i, i);
	for (int i : st)
		for (int j : adj2[i])
			if (who[i]!=who[j])
				out[who[i]]=1;
	int ans=69696969;
	for (int i=0; i<k; ++i)
		if (!out[who[i]])
			ans=min(ans, cnt[who[i]]-1);
	cout << ans;
	return 0;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:86:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |   for (int j=1; j<oc[i].size(); ++j) {
      |                 ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 90 ms 197600 KB Output is correct
2 Correct 95 ms 197572 KB Output is correct
3 Correct 107 ms 197724 KB Output is correct
4 Correct 97 ms 197640 KB Output is correct
5 Correct 90 ms 197580 KB Output is correct
6 Correct 90 ms 197556 KB Output is correct
7 Correct 87 ms 197608 KB Output is correct
8 Correct 101 ms 197592 KB Output is correct
9 Correct 106 ms 197532 KB Output is correct
10 Correct 89 ms 197504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 90 ms 197600 KB Output is correct
2 Correct 95 ms 197572 KB Output is correct
3 Correct 107 ms 197724 KB Output is correct
4 Correct 97 ms 197640 KB Output is correct
5 Correct 90 ms 197580 KB Output is correct
6 Correct 90 ms 197556 KB Output is correct
7 Correct 87 ms 197608 KB Output is correct
8 Correct 101 ms 197592 KB Output is correct
9 Correct 106 ms 197532 KB Output is correct
10 Correct 89 ms 197504 KB Output is correct
11 Correct 100 ms 200356 KB Output is correct
12 Correct 99 ms 200416 KB Output is correct
13 Correct 112 ms 200436 KB Output is correct
14 Correct 117 ms 200324 KB Output is correct
15 Correct 102 ms 200396 KB Output is correct
16 Correct 108 ms 200600 KB Output is correct
17 Correct 102 ms 200468 KB Output is correct
18 Correct 132 ms 200528 KB Output is correct
19 Correct 101 ms 200604 KB Output is correct
20 Correct 104 ms 200512 KB Output is correct
21 Correct 102 ms 200548 KB Output is correct
22 Correct 114 ms 200768 KB Output is correct
23 Correct 124 ms 200988 KB Output is correct
24 Correct 111 ms 200352 KB Output is correct
25 Correct 101 ms 200392 KB Output is correct
26 Correct 101 ms 200432 KB Output is correct
27 Correct 112 ms 200508 KB Output is correct
28 Correct 108 ms 200544 KB Output is correct
29 Correct 102 ms 200560 KB Output is correct
30 Correct 102 ms 200468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 2556 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 90 ms 197600 KB Output is correct
2 Correct 95 ms 197572 KB Output is correct
3 Correct 107 ms 197724 KB Output is correct
4 Correct 97 ms 197640 KB Output is correct
5 Correct 90 ms 197580 KB Output is correct
6 Correct 90 ms 197556 KB Output is correct
7 Correct 87 ms 197608 KB Output is correct
8 Correct 101 ms 197592 KB Output is correct
9 Correct 106 ms 197532 KB Output is correct
10 Correct 89 ms 197504 KB Output is correct
11 Correct 100 ms 200356 KB Output is correct
12 Correct 99 ms 200416 KB Output is correct
13 Correct 112 ms 200436 KB Output is correct
14 Correct 117 ms 200324 KB Output is correct
15 Correct 102 ms 200396 KB Output is correct
16 Correct 108 ms 200600 KB Output is correct
17 Correct 102 ms 200468 KB Output is correct
18 Correct 132 ms 200528 KB Output is correct
19 Correct 101 ms 200604 KB Output is correct
20 Correct 104 ms 200512 KB Output is correct
21 Correct 102 ms 200548 KB Output is correct
22 Correct 114 ms 200768 KB Output is correct
23 Correct 124 ms 200988 KB Output is correct
24 Correct 111 ms 200352 KB Output is correct
25 Correct 101 ms 200392 KB Output is correct
26 Correct 101 ms 200432 KB Output is correct
27 Correct 112 ms 200508 KB Output is correct
28 Correct 108 ms 200544 KB Output is correct
29 Correct 102 ms 200560 KB Output is correct
30 Correct 102 ms 200468 KB Output is correct
31 Runtime error 2556 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -