0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-08 20:58:49 +00:00

#3280. 「JOISC 2020 Day4」首都城市

https://loj.ac/s/1696059
This commit is contained in:
Baoshuo Ren 2023-02-08 06:17:48 +08:00
parent 1ced40c0d9
commit 907ae36f5c
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
202 changed files with 811 additions and 0 deletions

208
LibreOJ/3280/3280.cpp Normal file
View File

@ -0,0 +1,208 @@
#include <iostream>
#include <stack>
#include <vector>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e6 + 5;
int n, k, c[N], cnt, ans = 0x3f3f3f3f;
int id[N], rid[N], fa[N], dep[N], siz[N], son[N], top[N];
std::vector<int> g[N], g2[N], pos[N];
int dfn[N], low[N], scc_cnt, scc_id[N];
int res_siz[N], deg[N];
std::stack<int> st;
bool vis[N];
struct node {
int l, r;
node(const int &_l = 0, const int &_r = 0)
: l(_l), r(_r) {}
} tr[N << 2];
void dfs1(int u, int f) {
fa[u] = f;
dep[u] = dep[f] + 1;
siz[u] = 1;
for (int v : g[u]) {
if (v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs2(int u, int t) {
static int cnt = 0;
rid[id[u] = ++cnt] = u;
top[u] = t;
if (son[u]) dfs2(son[u], t);
for (int v : g[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
void build(int u, int l, int r) {
tr[u] = node(l, r);
cnt++;
if (l == r) {
g2[k + u].emplace_back(c[rid[l]]);
return;
}
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
g2[k + u].emplace_back(k + (u << 1));
g2[k + u].emplace_back(k + (u << 1 | 1));
}
void modify(int u, int l, int r, int x) {
if (l <= tr[u].l && tr[u].r <= r) {
g2[x].emplace_back(k + u);
return;
}
int mid = (tr[u].l + tr[u].r) >> 1;
if (l <= mid) modify(u << 1, l, r, x);
if (r > mid) modify(u << 1 | 1, l, r, x);
}
void modify_path(int u, int v) {
int x = c[u];
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
modify(1, id[top[u]], id[u], x);
u = fa[top[u]];
}
if (dep[v] > dep[u]) std::swap(u, v);
modify(1, id[v], id[u], x);
}
void tarjan(int u) {
static int cnt = 0;
dfn[u] = low[u] = ++cnt;
st.emplace(u);
vis[u] = true;
for (int v : g2[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (vis[v]) {
low[u] = std::min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
scc_cnt++;
int v;
do {
v = st.top();
st.pop();
scc_id[v] = scc_cnt;
vis[v] = false;
} while (v != u);
}
}
int lca(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
u = fa[top[u]];
}
return dep[u] < dep[v] ? u : v;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> k;
for (int i = 1, x, y; i < n; i++) {
cin >> x >> y;
g[x].emplace_back(y);
g[y].emplace_back(x);
}
for (int i = 1; i <= n; i++) {
cin >> c[i];
pos[c[i]].emplace_back(i);
}
dfs1(1, 1);
dfs2(1, 1);
build(1, 1, n);
for (int i = 1; i <= k; i++) {
if (pos[i].size() <= 1) continue;
int p = pos[i][0];
for (int j = 1; j < pos[i].size(); j++) {
int l = lca(pos[i][0], pos[i][j]);
if (l != pos[i][j]) modify_path(pos[i][j], l);
if (dep[l] < dep[p]) p = l;
}
if (p != pos[i][0]) {
modify_path(pos[i][0], p);
}
}
for (int i = 1; i <= k + cnt; i++) {
if (!dfn[i]) tarjan(i);
}
for (int i = 1; i <= k; i++) {
res_siz[scc_id[i]]++;
}
for (int i = 1; i <= k + cnt; i++) {
for (int v : g2[i]) {
if (scc_id[i] != scc_id[v]) {
deg[scc_id[i]]++;
}
}
}
for (int i = 1; i <= k; i++) {
if (deg[scc_id[i]] == 0) {
ans = std::min(ans, res_siz[scc_id[i]] - 1);
}
}
cout << ans << endl;
return 0;
}

BIN
LibreOJ/3280/data/01-01.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-01.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-02.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-02.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-03.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-03.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-04.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-04.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-05.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-05.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-06.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-06.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-07.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-07.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-08.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-08.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-09.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-09.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/01-10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-01.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-01.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-02.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-02.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-03.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-03.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-04.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-04.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-05.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-05.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-06.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-06.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-07.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-07.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-08.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-08.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-09.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-09.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/02-20.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-01.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-01.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-02.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-02.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-03.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-03.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-04.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-04.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-05.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-05.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-06.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-06.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-07.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-07.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-08.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-08.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-09.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-09.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/3280/data/03-20.in (Stored with Git LFS) Normal file

Binary file not shown.

Some files were not shown because too many files have changed in this diff Show More