Submission #1039915

# Submission time Handle Problem Language Result Execution time Memory
1039915 2024-07-31T12:20:07 Z ymm Capital City (JOI20_capital_city) C++17
100 / 100
701 ms 255980 KB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 200'010;
const int lg = 20;

struct snode;
struct gnode;

struct gnode {
	gnode *par;
	gnode *anc[lg];
	vector<gnode *> adj;
	gnode *hchild, *hroot, *hleaf;
	snode *sroot;
	int st, ft, ht;
	int col;

	int sz() { return ft - st; }
	int hsz() { return hleaf->ht - hroot->ht + 1; }
} graph[N];

struct snode {
	vector<snode *> adj;
	vector<snode *> adjt;
	int weight;
	int vis;

	static void add_edge(snode *v, snode *u) {
		v->adj.push_back(u);
		u->adjt.push_back(v);
	}
} pool[(1<<28) / sizeof(gnode)];

vector<snode *> all_snodes;

snode *new_snode()
{
	static int p = 0;
	all_snodes.push_back(&pool[p]);
	return &pool[p++];
}

void dfsg1(gnode *v, gnode *p, int &t, int h)
{
	v->par = p;
	v->st = t++;
	v->ht = h;

	v->anc[0] = v->par? v->par: v;
	Loop (i,0,lg-1)
		v->anc[i + 1] = v->anc[i]->anc[i];

	for (auto u : v->adj)
		if (u != p)
			dfsg1(u, v, t, h + 1);
	v->ft = t++;
	for (auto u : v->adj) {
		if (u == p)
			continue;
		if (v->hchild == 0 || v->hchild->sz() < u->sz())
			v->hchild = u;
	}
}

gnode *lca(gnode *v, gnode *u)
{
	if (v->ht < u->ht)
		swap(v, u);
	int diff = v->ht - u->ht;
	Loop (i,0,lg)
		if ((diff >> i) & 1)
			v = v->anc[i];
	if (v == u)
		return v;
	LoopR (i,0,lg) {
		if (v->anc[i] != u->anc[i]) {
			v = v->anc[i];
			u = u->anc[i];
		}
	}
	return v->anc[0];
}

gnode gn[N];
snode col_snode[N];
int n, k;

void init_col_snode()
{
	Loop (i,0,k) {
		col_snode[i].weight = 1;
		all_snodes.push_back(&col_snode[i]);
	}
}

snode *make_seg(snode **ptr, int l, int r)
{
	if (r - l == 1)
		return ptr[l];
	snode *sn = new_snode();
	snode::add_edge(sn, make_seg(ptr, l, (l + r)/2));
	snode::add_edge(sn, make_seg(ptr, (l + r)/2, r));
	return sn;
}

snode *make_seg(gnode *u, gnode *d)
{
	vector<snode *> vec(d->ht - u->ht + 1);
	gnode *v = d;
	Loop (i,0,vec.size()) {
		vec.end()[-i-1] = &col_snode[v->col];
		v = v->par;
	}
	return make_seg(vec.data() - u->ht, u->ht, d->ht + 1);
}

template<typename Fn>
void seg_apply(snode *v, Fn fn, int l, int r, int b, int e)
{
	if (l <= b && e <= r) {
		fn(v);
		return;
	}
	if (r <= b || e <= l)
		return;
	seg_apply(v->adj[0], fn, l, r, b, (b+e)/2);
	seg_apply(v->adj[1], fn, l, r, (b+e)/2, e);
}

template<typename Fn>
void seg_apply(gnode *v, Fn fn, int l, int r)
{
	//cerr << v - graph << ' ' << l << ' ' << r << '\n';
	seg_apply(v->sroot, fn, l, r, v->ht, v->hleaf->ht + 1);
}

void dfsg2(gnode *v, gnode *p, gnode *hroot)
{
	for (gnode *u : v->adj) {
		if (u == p)
			continue;
		dfsg2(u, v, u == v->hchild? hroot: u);
	}
	v->hleaf = v->hchild? v->hchild->hleaf: v;
	v->hroot = hroot;
	if (v == hroot)
		v->sroot = make_seg(v, v->hleaf);
}

template<typename Fn>
void path_apply(gnode *u, gnode *d, Fn fn)
{
	while (d->hroot->ht > u->ht) {
		gnode *h = d->hroot;
		seg_apply(h, fn, h->ht, d->ht + 1);
		d = h->par;
	}
	gnode *h = d->hroot;
	assert(h == u->hroot);
	seg_apply(h, fn, u->ht, d->ht + 1);
}

void path_add_col_to(gnode *u, gnode *d, snode *col_node)
{
	//cerr << col_node - col_snode << " => " << u - graph << " --- " << d - graph << '\n';
	path_apply(u, d, [=](snode *sn) {
		//cerr << col_node - col_snode << " -> " << sn - col_snode << '\n';
		snode::add_edge(col_node, sn);
	});
	// while (d && d->ht >= u->ht) {
	// 	snode::add_edge(col_node, &col_snode[d->col]);
	// 	//cerr << col_node - col_snode << " -> " << d->col << '\n';
	// 	d = d->par;
	// }
}

void scc_dfs1(snode *v, vector<snode *> &vec)
{
	v->vis = 1;
	for (snode *u : v->adj) {
		if (!u->vis)
			scc_dfs1(u, vec);
	}
	vec.push_back(v);
}

int scc_dfs2(snode *v, int viscol)
{
	v->vis = viscol;
	int ans = v->weight;
	for (snode *u : v->adjt) {
		if (u->vis == -1)
			ans += scc_dfs2(u, viscol);
	}
	return ans;
}

int scc_solve()
{
	vector<snode *> vec;
	for (snode *sn : all_snodes)
		sn->vis = 0;
	for (snode *sn : all_snodes) {
		if (!sn->vis)
			scc_dfs1(sn, vec);
	}
	reverse(vec.begin(), vec.end());
	for (snode *sn : all_snodes)
		sn->vis = -1;
	vector<int> scc_weight;
	for (snode *sn : vec) {
		if (sn->vis != -1)
			continue;
		int x = scc_dfs2(sn, scc_weight.size());
		scc_weight.push_back(x);
	}
	vector<bool> scc_ok(scc_weight.size(), true);
	//Loop (i,0,k)
	//	cerr << i << ": " << col_snode[i].vis << '\n';
	for (snode *v : all_snodes) for (snode *u : v->adj) {
		if (v->vis != u->vis)
			scc_ok[v->vis] = false;
	}
	int ans = INT_MAX;
	Loop (i,0,scc_weight.size()) {
		if (!scc_ok[i])
			continue;
		assert(scc_weight[i]);
		ans = min(ans, scc_weight[i]);
	}
	return ans;
}

vector<gnode *> gnodes_of_col[N];

void make_virtuals()
{
	auto cmp = [](gnode *v, gnode *u) { return v->st < u->st; };
	Loop (col,0,k) {
		vector<gnode *> vec = gnodes_of_col[col];
		sort(vec.begin(), vec.end(), cmp);
		if (vec.empty())
			continue;

		int cnt = vec.size();
		Loop (i,0,cnt-1)
			vec.push_back(lca(vec[i], vec[i + 1]));
		sort(vec.begin(), vec.end(), cmp);
		vec.resize(unique(vec.begin(), vec.end()) - vec.begin());

		vector<gnode *> st;
		for (gnode *v : vec) {
			while (st.size() && st.back()->ft <= v->st)
				st.pop_back();
			if (st.size())
				path_add_col_to(st.back(), v, &col_snode[col]);
			st.push_back(v);
		}
	}
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n >> k;
	Loop (i,0,n-1) {
		int v, u;
		cin >> v >> u;
		--v; --u;
		graph[v].adj.push_back(&graph[u]);
		graph[u].adj.push_back(&graph[v]);
	}
	Loop (i,0,n) {
		gnode *v = &graph[i];
		cin >> v->col;
		v->col--;
		gnodes_of_col[v->col].push_back(v);
	}
	init_col_snode();
	int dummy = 0;
	dfsg1(&graph[0], nullptr, dummy, 0);
	dfsg2(&graph[0], nullptr, &graph[0]);
	make_virtuals();
	int ans = scc_solve();
	cout << ans-1 << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 65 ms 171344 KB Output is correct
2 Correct 60 ms 171088 KB Output is correct
3 Correct 53 ms 171312 KB Output is correct
4 Correct 51 ms 171064 KB Output is correct
5 Correct 52 ms 171152 KB Output is correct
6 Correct 51 ms 171088 KB Output is correct
7 Correct 56 ms 171088 KB Output is correct
8 Correct 52 ms 171088 KB Output is correct
9 Correct 53 ms 171100 KB Output is correct
10 Correct 54 ms 171088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 171344 KB Output is correct
2 Correct 60 ms 171088 KB Output is correct
3 Correct 53 ms 171312 KB Output is correct
4 Correct 51 ms 171064 KB Output is correct
5 Correct 52 ms 171152 KB Output is correct
6 Correct 51 ms 171088 KB Output is correct
7 Correct 56 ms 171088 KB Output is correct
8 Correct 52 ms 171088 KB Output is correct
9 Correct 53 ms 171100 KB Output is correct
10 Correct 54 ms 171088 KB Output is correct
11 Correct 56 ms 171600 KB Output is correct
12 Correct 54 ms 171604 KB Output is correct
13 Correct 58 ms 171688 KB Output is correct
14 Correct 57 ms 171764 KB Output is correct
15 Correct 55 ms 171576 KB Output is correct
16 Correct 55 ms 171776 KB Output is correct
17 Correct 64 ms 171564 KB Output is correct
18 Correct 55 ms 171604 KB Output is correct
19 Correct 56 ms 171604 KB Output is correct
20 Correct 59 ms 171772 KB Output is correct
21 Correct 59 ms 171600 KB Output is correct
22 Correct 53 ms 171860 KB Output is correct
23 Correct 56 ms 171936 KB Output is correct
24 Correct 57 ms 171604 KB Output is correct
25 Correct 53 ms 171752 KB Output is correct
26 Correct 55 ms 171600 KB Output is correct
27 Correct 56 ms 171604 KB Output is correct
28 Correct 55 ms 171604 KB Output is correct
29 Correct 58 ms 171604 KB Output is correct
30 Correct 55 ms 171604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 686 ms 255664 KB Output is correct
2 Correct 300 ms 255900 KB Output is correct
3 Correct 701 ms 255980 KB Output is correct
4 Correct 307 ms 255896 KB Output is correct
5 Correct 678 ms 252564 KB Output is correct
6 Correct 316 ms 255804 KB Output is correct
7 Correct 620 ms 249644 KB Output is correct
8 Correct 292 ms 249280 KB Output is correct
9 Correct 420 ms 220064 KB Output is correct
10 Correct 437 ms 217500 KB Output is correct
11 Correct 440 ms 220544 KB Output is correct
12 Correct 429 ms 222996 KB Output is correct
13 Correct 436 ms 218572 KB Output is correct
14 Correct 439 ms 222612 KB Output is correct
15 Correct 480 ms 224240 KB Output is correct
16 Correct 442 ms 219044 KB Output is correct
17 Correct 468 ms 219672 KB Output is correct
18 Correct 452 ms 218668 KB Output is correct
19 Correct 455 ms 223872 KB Output is correct
20 Correct 439 ms 222104 KB Output is correct
21 Correct 52 ms 171280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 171344 KB Output is correct
2 Correct 60 ms 171088 KB Output is correct
3 Correct 53 ms 171312 KB Output is correct
4 Correct 51 ms 171064 KB Output is correct
5 Correct 52 ms 171152 KB Output is correct
6 Correct 51 ms 171088 KB Output is correct
7 Correct 56 ms 171088 KB Output is correct
8 Correct 52 ms 171088 KB Output is correct
9 Correct 53 ms 171100 KB Output is correct
10 Correct 54 ms 171088 KB Output is correct
11 Correct 56 ms 171600 KB Output is correct
12 Correct 54 ms 171604 KB Output is correct
13 Correct 58 ms 171688 KB Output is correct
14 Correct 57 ms 171764 KB Output is correct
15 Correct 55 ms 171576 KB Output is correct
16 Correct 55 ms 171776 KB Output is correct
17 Correct 64 ms 171564 KB Output is correct
18 Correct 55 ms 171604 KB Output is correct
19 Correct 56 ms 171604 KB Output is correct
20 Correct 59 ms 171772 KB Output is correct
21 Correct 59 ms 171600 KB Output is correct
22 Correct 53 ms 171860 KB Output is correct
23 Correct 56 ms 171936 KB Output is correct
24 Correct 57 ms 171604 KB Output is correct
25 Correct 53 ms 171752 KB Output is correct
26 Correct 55 ms 171600 KB Output is correct
27 Correct 56 ms 171604 KB Output is correct
28 Correct 55 ms 171604 KB Output is correct
29 Correct 58 ms 171604 KB Output is correct
30 Correct 55 ms 171604 KB Output is correct
31 Correct 686 ms 255664 KB Output is correct
32 Correct 300 ms 255900 KB Output is correct
33 Correct 701 ms 255980 KB Output is correct
34 Correct 307 ms 255896 KB Output is correct
35 Correct 678 ms 252564 KB Output is correct
36 Correct 316 ms 255804 KB Output is correct
37 Correct 620 ms 249644 KB Output is correct
38 Correct 292 ms 249280 KB Output is correct
39 Correct 420 ms 220064 KB Output is correct
40 Correct 437 ms 217500 KB Output is correct
41 Correct 440 ms 220544 KB Output is correct
42 Correct 429 ms 222996 KB Output is correct
43 Correct 436 ms 218572 KB Output is correct
44 Correct 439 ms 222612 KB Output is correct
45 Correct 480 ms 224240 KB Output is correct
46 Correct 442 ms 219044 KB Output is correct
47 Correct 468 ms 219672 KB Output is correct
48 Correct 452 ms 218668 KB Output is correct
49 Correct 455 ms 223872 KB Output is correct
50 Correct 439 ms 222104 KB Output is correct
51 Correct 52 ms 171280 KB Output is correct
52 Correct 502 ms 222660 KB Output is correct
53 Correct 498 ms 223552 KB Output is correct
54 Correct 494 ms 223940 KB Output is correct
55 Correct 485 ms 223684 KB Output is correct
56 Correct 505 ms 223008 KB Output is correct
57 Correct 510 ms 223424 KB Output is correct
58 Correct 480 ms 216128 KB Output is correct
59 Correct 474 ms 215372 KB Output is correct
60 Correct 561 ms 227536 KB Output is correct
61 Correct 586 ms 232124 KB Output is correct
62 Correct 296 ms 255900 KB Output is correct
63 Correct 291 ms 255916 KB Output is correct
64 Correct 291 ms 250576 KB Output is correct
65 Correct 289 ms 255648 KB Output is correct
66 Correct 338 ms 224084 KB Output is correct
67 Correct 333 ms 217528 KB Output is correct
68 Correct 347 ms 223884 KB Output is correct
69 Correct 332 ms 223632 KB Output is correct
70 Correct 332 ms 223748 KB Output is correct
71 Correct 386 ms 224392 KB Output is correct
72 Correct 336 ms 224528 KB Output is correct
73 Correct 348 ms 223456 KB Output is correct
74 Correct 437 ms 223856 KB Output is correct
75 Correct 360 ms 223996 KB Output is correct
76 Correct 394 ms 207856 KB Output is correct
77 Correct 414 ms 207432 KB Output is correct
78 Correct 469 ms 219616 KB Output is correct
79 Correct 430 ms 218052 KB Output is correct
80 Correct 417 ms 221680 KB Output is correct
81 Correct 436 ms 220060 KB Output is correct
82 Correct 462 ms 221348 KB Output is correct
83 Correct 438 ms 218116 KB Output is correct
84 Correct 459 ms 224868 KB Output is correct
85 Correct 477 ms 221108 KB Output is correct
86 Correct 459 ms 217120 KB Output is correct
87 Correct 432 ms 218852 KB Output is correct
88 Correct 503 ms 221444 KB Output is correct
89 Correct 454 ms 214836 KB Output is correct
90 Correct 434 ms 212996 KB Output is correct
91 Correct 449 ms 215940 KB Output is correct
92 Correct 478 ms 217852 KB Output is correct
93 Correct 472 ms 215076 KB Output is correct
94 Correct 478 ms 214592 KB Output is correct
95 Correct 427 ms 215856 KB Output is correct
96 Correct 463 ms 218512 KB Output is correct
97 Correct 449 ms 217784 KB Output is correct