Submission #537412

# Submission time Handle Problem Language Result Execution time Memory
537412 2022-03-15T05:12:47 Z fhvirus Capital City (JOI20_capital_city) C++17
100 / 100
643 ms 134368 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int, int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) LKJ("\033[1;32m[ " + string(#args) + " ]\033[0m", args)
template <class I> void LKJ(I&&x) { cerr << x << endl; }
template <class I, class...T> void LKJ(I&&x, T&&...t) { cerr << x << ", "; LKJ(t...); }
template <class I> void OI(I a, I b) { while (a != b) cerr << *a << " \n"[(a = next(a)) == b]; }
#else
#define debug(...) 0
#define OI(...) 0
#endif

const int kN = 200002;
const int kL = 18;

int N, K, C[kN];
vector<int> adj[kN], town[kN];
int cityLCA[kN];

namespace LCA {

int dep[kN], anc[kL][kN];
void dfs(int u, int p) {
	anc[0][u] = p;
	for (const int& v: adj[u]) {
		if (v == p) continue;
		dep[v] = dep[u] + 1;
		dfs(v, u);
	}
}
void init() {
	dfs(1, 1);
	for (int l = 1; l < kL; ++l)
		for (int i = 1; i <= N; ++i)
			anc[l][i] = anc[l - 1][anc[l - 1][i]];
}
int getLCA(int u, int v) {
	if (dep[u] < dep[v]) swap(u, v);
	for (int d = dep[u] - dep[v], l = kL - 1; l >= 0; --l)
		if (d >> l & 1) u = anc[l][u];
	if (u == v) return u;
	for (int l = kL - 1; l >= 0; --l)
		if (anc[l][u] != anc[l][v]) {
			u = anc[l][u];
			v = anc[l][v];
		}
	return anc[0][u];
}

} // namespace LCA

namespace HLD {

int siz[kN], in[kN], ou[kN], tot;
int par[kN], head[kN], at[kN];

void dfs_siz(int u, int p) {
	par[u] = p;
	siz[u] = 1;
	for (int &v: adj[u]) {
		if (v == p) continue;
		dfs_siz(v, u);
		siz[u] += siz[v];
		if (siz[v] > siz[adj[u][0]])
			swap(v, adj[u][0]);
	}
}
void dfs_chain(int u, int h) {
	in[u] = ++tot;
	head[u] = h;
	for (const int& v: adj[u]) {
		if (v == par[u]) continue;
		dfs_chain(v, (v == adj[u][0] ? h : v));
	}
	ou[u] = tot;
}
void init() {
	dfs_siz(1, 1);
	dfs_chain(1, 1);
	for (int i = 1; i <= N; ++i)
		at[in[i]] = i;
}
vector<pii> get(int u, int v) {
	// go from u to v, v is par of u
	vector<pii> ans;
	while (head[u] != head[v]) {
		ans.pb(in[head[u]], in[u]);
		u = par[head[u]];
	}
	ans.pb(in[v], in[u]);
	return ans;
}

} // namespace HLD

struct SGT {
	int n, tot; vector<int> val;
	vector<vector<int>> adj;
	void init(int i, int l, int r) {
		if (l == r) {
			val[i] = C[HLD::at[l]];
			debug(l, r, val[i]);
			return;
		}
		val[i] = ++tot;
		debug(l, r, val[i]);
		int m = (l + r) / 2;
		init(i * 2, l, m);
		init(i * 2 + 1, m + 1, r);
	}
	void build(int i, int l, int r) {
		if (l == r) {
			return;
		}
		adj[val[i]].pb(val[i * 2]);
		adj[val[i]].pb(val[i * 2 + 1]);
		int m = (l + r) / 2;
		build(i * 2, l, m);
		build(i * 2 + 1, m + 1, r);
	}
	SGT (int _n): n(_n), val(n * 4 + 4), tot(K) {
		init(1, 1, n);
		adj.assign(tot + 1, vector<int>());
		build(1, 1, n);
	}
	void add(int i, int l, int r, int qv, int ql, int qr) {
		if (ql <= l and r <= qr) {
			adj[qv].pb(val[i]);
			return;
		}
		int m = (l + r) / 2;
		if (ql <= m) add(i * 2, l, m, qv, ql, qr);
		if (m < qr) add(i * 2 + 1, m + 1, r, qv, ql, qr);
	}
	void add(int qv, int ql, int qr) { add(1, 1, n, qv, ql, qr); }
};

struct SCC {
	int n, tot, sccnt, top;
	vector<vector<int>> adj;
	vector<int> pre, low;
	vector<int> ins, stk;
	vector<int> scc;
	vector<set<int>> has;
	SCC (const vector<vector<int>>& init):
		n(init.size()), tot(0), sccnt(0), top(0), adj(init), pre(n, 0), low(n, 0),
		ins(n, 0), stk(n, 0), scc(n, 0), has(n) {}
	void tarjan(int u) {
		low[u] = pre[u] = ++tot;
		ins[u] = true; stk[++top] = u;
		for (const int& v: adj[u]) {
			if (pre[v] == 0) {
				tarjan(v);
				low[u] = min(low[u], low[v]);
			} else if (ins[v])
				low[u] = min(low[u], pre[v]);
		}
		if (low[u] == pre[u]) {
			++sccnt;
			int v;
			do {
				v = stk[top--];
				scc[v] = sccnt;
				ins[v] = false;
				if (v <= K)
					has[sccnt].insert(v);
			} while (v != u);
		}
	}
	int solve() {
		for (int i = 1; i < n; ++i)
			if (pre[i] == 0) tarjan(i);

		vector<int> oudeg(n, 0);
		for (int i = 1; i < n; ++i)
			for (const int& j: adj[i])
				if (scc[i] != scc[j])
					++oudeg[scc[i]];
		int ans = n + n;
		for (int i = 1; i <= sccnt; ++i)
			if (oudeg[i] == 0)
				ans = min(ans, (int) has[i].size());
		return ans;
	}
};

signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> N >> K;
	for (int A, B, i = 1; i < N; ++i) {
		cin >> A >> B;
		adj[A].pb(B);
		adj[B].pb(A);
	}
	for (int i = 1; i <= N; ++i) {
		cin >> C[i];
		town[C[i]].pb(i);
	}

	LCA::init();

	for (int i = 1; i <= K; ++i) {
		cityLCA[i] = town[i][0];
		for (int j = 1; j < town[i].size(); ++j)
			cityLCA[i] = LCA::getLCA(cityLCA[i], town[i][j]);
		//debug(i, cityLCA[i]);
	}

	HLD::init();
	for (int i = 1; i <= N; ++i) {
		debug(i, HLD::at[i]);
	}

	SGT sgt(N);

	for (int i = 1; i <= N; ++i) {
		int c = C[i];
		int u = HLD::at[i];
		int v = HLD::at[cityLCA[c]];
		debug(i, c, u, v);
		vector<pii> rs = HLD::get(i, cityLCA[c]);
		for (const auto& [l, r]: rs) {
			sgt.add(c, l, r);
			debug(l, r);
		}
	}

	for (int i = 1; i <= sgt.tot; ++i) {
		debug(i);
		auto &v = sgt.adj[i];
		sort(AI(v));
		v.erase(unique(AI(v)), end(v));
		OI(AI(sgt.adj[i]));
	}

	SCC scc(sgt.adj);
	cout << scc.solve() - 1 << '\n';

	return 0;
}

Compilation message

capital_city.cpp: In member function 'void SGT::init(int, int, int)':
capital_city.cpp:14:20: warning: statement has no effect [-Wunused-value]
   14 | #define debug(...) 0
      |                    ^
capital_city.cpp:107:4: note: in expansion of macro 'debug'
  107 |    debug(l, r, val[i]);
      |    ^~~~~
capital_city.cpp:14:20: warning: statement has no effect [-Wunused-value]
   14 | #define debug(...) 0
      |                    ^
capital_city.cpp:111:3: note: in expansion of macro 'debug'
  111 |   debug(l, r, val[i]);
      |   ^~~~~
capital_city.cpp: In constructor 'SGT::SGT(int)':
capital_city.cpp:102:26: warning: 'SGT::val' will be initialized after [-Wreorder]
  102 |  int n, tot; vector<int> val;
      |                          ^~~
capital_city.cpp:102:9: warning:   'int SGT::tot' [-Wreorder]
  102 |  int n, tot; vector<int> val;
      |         ^~~
capital_city.cpp:126:2: warning:   when initialized here [-Wreorder]
  126 |  SGT (int _n): n(_n), val(n * 4 + 4), tot(K) {
      |  ^~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:210:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  210 |   for (int j = 1; j < town[i].size(); ++j)
      |                   ~~^~~~~~~~~~~~~~~~
capital_city.cpp:14:20: warning: statement has no effect [-Wunused-value]
   14 | #define debug(...) 0
      |                    ^
capital_city.cpp:217:3: note: in expansion of macro 'debug'
  217 |   debug(i, HLD::at[i]);
      |   ^~~~~
capital_city.cpp:14:20: warning: statement has no effect [-Wunused-value]
   14 | #define debug(...) 0
      |                    ^
capital_city.cpp:226:3: note: in expansion of macro 'debug'
  226 |   debug(i, c, u, v);
      |   ^~~~~
capital_city.cpp:14:20: warning: statement has no effect [-Wunused-value]
   14 | #define debug(...) 0
      |                    ^
capital_city.cpp:230:4: note: in expansion of macro 'debug'
  230 |    debug(l, r);
      |    ^~~~~
capital_city.cpp:224:7: warning: unused variable 'u' [-Wunused-variable]
  224 |   int u = HLD::at[i];
      |       ^
capital_city.cpp:225:7: warning: unused variable 'v' [-Wunused-variable]
  225 |   int v = HLD::at[cityLCA[c]];
      |       ^
capital_city.cpp:14:20: warning: statement has no effect [-Wunused-value]
   14 | #define debug(...) 0
      |                    ^
capital_city.cpp:235:3: note: in expansion of macro 'debug'
  235 |   debug(i);
      |   ^~~~~
capital_city.cpp:15:17: warning: statement has no effect [-Wunused-value]
   15 | #define OI(...) 0
      |                 ^
capital_city.cpp:239:3: note: in expansion of macro 'OI'
  239 |   OI(AI(sgt.adj[i]));
      |   ^~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9844 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9844 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 9 ms 10684 KB Output is correct
12 Correct 10 ms 10708 KB Output is correct
13 Correct 8 ms 10640 KB Output is correct
14 Correct 9 ms 10580 KB Output is correct
15 Correct 9 ms 10812 KB Output is correct
16 Correct 9 ms 10808 KB Output is correct
17 Correct 8 ms 10708 KB Output is correct
18 Correct 8 ms 10836 KB Output is correct
19 Correct 8 ms 10764 KB Output is correct
20 Correct 8 ms 10836 KB Output is correct
21 Correct 10 ms 10836 KB Output is correct
22 Correct 8 ms 10964 KB Output is correct
23 Correct 8 ms 10972 KB Output is correct
24 Correct 8 ms 10812 KB Output is correct
25 Correct 9 ms 10768 KB Output is correct
26 Correct 8 ms 10800 KB Output is correct
27 Correct 9 ms 10764 KB Output is correct
28 Correct 10 ms 10844 KB Output is correct
29 Correct 11 ms 10836 KB Output is correct
30 Correct 11 ms 10768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 578 ms 130352 KB Output is correct
2 Correct 278 ms 130712 KB Output is correct
3 Correct 551 ms 130084 KB Output is correct
4 Correct 276 ms 130504 KB Output is correct
5 Correct 561 ms 125840 KB Output is correct
6 Correct 280 ms 130300 KB Output is correct
7 Correct 634 ms 123992 KB Output is correct
8 Correct 271 ms 124452 KB Output is correct
9 Correct 465 ms 106592 KB Output is correct
10 Correct 446 ms 104636 KB Output is correct
11 Correct 442 ms 106888 KB Output is correct
12 Correct 455 ms 109388 KB Output is correct
13 Correct 463 ms 103648 KB Output is correct
14 Correct 423 ms 109568 KB Output is correct
15 Correct 499 ms 109988 KB Output is correct
16 Correct 481 ms 105900 KB Output is correct
17 Correct 467 ms 106504 KB Output is correct
18 Correct 469 ms 106032 KB Output is correct
19 Correct 472 ms 109384 KB Output is correct
20 Correct 429 ms 110048 KB Output is correct
21 Correct 5 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9812 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 5 ms 9812 KB Output is correct
4 Correct 7 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 5 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9844 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 9 ms 10684 KB Output is correct
12 Correct 10 ms 10708 KB Output is correct
13 Correct 8 ms 10640 KB Output is correct
14 Correct 9 ms 10580 KB Output is correct
15 Correct 9 ms 10812 KB Output is correct
16 Correct 9 ms 10808 KB Output is correct
17 Correct 8 ms 10708 KB Output is correct
18 Correct 8 ms 10836 KB Output is correct
19 Correct 8 ms 10764 KB Output is correct
20 Correct 8 ms 10836 KB Output is correct
21 Correct 10 ms 10836 KB Output is correct
22 Correct 8 ms 10964 KB Output is correct
23 Correct 8 ms 10972 KB Output is correct
24 Correct 8 ms 10812 KB Output is correct
25 Correct 9 ms 10768 KB Output is correct
26 Correct 8 ms 10800 KB Output is correct
27 Correct 9 ms 10764 KB Output is correct
28 Correct 10 ms 10844 KB Output is correct
29 Correct 11 ms 10836 KB Output is correct
30 Correct 11 ms 10768 KB Output is correct
31 Correct 578 ms 130352 KB Output is correct
32 Correct 278 ms 130712 KB Output is correct
33 Correct 551 ms 130084 KB Output is correct
34 Correct 276 ms 130504 KB Output is correct
35 Correct 561 ms 125840 KB Output is correct
36 Correct 280 ms 130300 KB Output is correct
37 Correct 634 ms 123992 KB Output is correct
38 Correct 271 ms 124452 KB Output is correct
39 Correct 465 ms 106592 KB Output is correct
40 Correct 446 ms 104636 KB Output is correct
41 Correct 442 ms 106888 KB Output is correct
42 Correct 455 ms 109388 KB Output is correct
43 Correct 463 ms 103648 KB Output is correct
44 Correct 423 ms 109568 KB Output is correct
45 Correct 499 ms 109988 KB Output is correct
46 Correct 481 ms 105900 KB Output is correct
47 Correct 467 ms 106504 KB Output is correct
48 Correct 469 ms 106032 KB Output is correct
49 Correct 472 ms 109384 KB Output is correct
50 Correct 429 ms 110048 KB Output is correct
51 Correct 5 ms 9812 KB Output is correct
52 Correct 556 ms 98544 KB Output is correct
53 Correct 594 ms 99512 KB Output is correct
54 Correct 592 ms 99572 KB Output is correct
55 Correct 578 ms 99140 KB Output is correct
56 Correct 610 ms 99444 KB Output is correct
57 Correct 590 ms 99532 KB Output is correct
58 Correct 550 ms 110616 KB Output is correct
59 Correct 519 ms 110632 KB Output is correct
60 Correct 605 ms 119528 KB Output is correct
61 Correct 643 ms 120564 KB Output is correct
62 Correct 278 ms 134368 KB Output is correct
63 Correct 290 ms 134232 KB Output is correct
64 Correct 279 ms 130096 KB Output is correct
65 Correct 283 ms 133960 KB Output is correct
66 Correct 494 ms 113684 KB Output is correct
67 Correct 409 ms 108664 KB Output is correct
68 Correct 455 ms 113720 KB Output is correct
69 Correct 467 ms 113708 KB Output is correct
70 Correct 481 ms 113556 KB Output is correct
71 Correct 456 ms 113672 KB Output is correct
72 Correct 504 ms 113768 KB Output is correct
73 Correct 403 ms 108452 KB Output is correct
74 Correct 484 ms 113712 KB Output is correct
75 Correct 472 ms 113676 KB Output is correct
76 Correct 408 ms 110968 KB Output is correct
77 Correct 436 ms 109148 KB Output is correct
78 Correct 456 ms 109632 KB Output is correct
79 Correct 451 ms 106072 KB Output is correct
80 Correct 443 ms 113140 KB Output is correct
81 Correct 447 ms 110580 KB Output is correct
82 Correct 470 ms 110612 KB Output is correct
83 Correct 488 ms 107948 KB Output is correct
84 Correct 424 ms 112668 KB Output is correct
85 Correct 478 ms 111072 KB Output is correct
86 Correct 459 ms 106664 KB Output is correct
87 Correct 437 ms 109060 KB Output is correct
88 Correct 472 ms 110612 KB Output is correct
89 Correct 492 ms 107684 KB Output is correct
90 Correct 509 ms 107632 KB Output is correct
91 Correct 445 ms 108764 KB Output is correct
92 Correct 486 ms 108800 KB Output is correct
93 Correct 490 ms 107888 KB Output is correct
94 Correct 459 ms 107700 KB Output is correct
95 Correct 458 ms 108092 KB Output is correct
96 Correct 485 ms 106952 KB Output is correct
97 Correct 497 ms 111000 KB Output is correct