mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-11-27 17:16:28 +00:00
parent
9019192e62
commit
6d17789bed
114
S2OJ/1687/1687.cpp
Normal file
114
S2OJ/1687/1687.cpp
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using std::cin;
|
||||||
|
using std::cout;
|
||||||
|
const char endl = '\n';
|
||||||
|
|
||||||
|
const int N = 2e5 + 5;
|
||||||
|
|
||||||
|
int n, k, a[N], c[N];
|
||||||
|
int cnt, id[N], fa[N], dep[N], siz[N], son[N], top[N];
|
||||||
|
std::vector<int> g[N], color[N];
|
||||||
|
std::set<int> ans;
|
||||||
|
|
||||||
|
void dfs1(int u, int f) {
|
||||||
|
siz[u] = 1;
|
||||||
|
fa[u] = f;
|
||||||
|
dep[u] = dep[f] + 1;
|
||||||
|
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (v == f) continue;
|
||||||
|
|
||||||
|
dfs1(v, u);
|
||||||
|
siz[u] += siz[v];
|
||||||
|
if (siz[v] > siz[son[u]]) son[u] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dfs2(int u, int t) {
|
||||||
|
id[u] = ++cnt;
|
||||||
|
top[u] = t;
|
||||||
|
|
||||||
|
if (!son[u]) return;
|
||||||
|
dfs2(son[u], t);
|
||||||
|
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (v == fa[u] || v == son[u]) continue;
|
||||||
|
dfs2(v, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dfs3(int u, int f) {
|
||||||
|
for (int v : g[u]) {
|
||||||
|
if (v == f) continue;
|
||||||
|
dfs3(v, u);
|
||||||
|
c[u] += c[v];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c[u] == 1) ans.emplace(a[u]);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
cin >> n >> k;
|
||||||
|
|
||||||
|
for (int i = 1; i <= n; i++) {
|
||||||
|
cin >> a[i];
|
||||||
|
|
||||||
|
color[a[i]].emplace_back(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1, x, y; i < n; i++) {
|
||||||
|
cin >> x >> y;
|
||||||
|
|
||||||
|
g[x].emplace_back(y);
|
||||||
|
g[y].emplace_back(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs1(1, 0);
|
||||||
|
dfs2(1, 1);
|
||||||
|
|
||||||
|
for (int i = 1; i <= k; i++) {
|
||||||
|
std::sort(color[i].begin(), color[i].end(), [&](int x, int y) -> bool { return id[x] < id[y]; });
|
||||||
|
|
||||||
|
int u = 0, p = 0;
|
||||||
|
|
||||||
|
for (int v : color[i]) {
|
||||||
|
if (!u) {
|
||||||
|
u = p = v;
|
||||||
|
c[v]++;
|
||||||
|
} else {
|
||||||
|
p = lca(u, v);
|
||||||
|
c[v]++, c[p]--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c[fa[p]]--;
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs3(1, 0);
|
||||||
|
|
||||||
|
cout << ans.size() << endl;
|
||||||
|
|
||||||
|
for (int x : ans) {
|
||||||
|
cout << x << ' ';
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
S2OJ/1687/data/color1.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color1.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color10.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color10.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color10.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color10.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color11.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color11.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color11.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color11.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color12.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color12.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color12.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color12.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color13.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color13.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color13.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color13.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color14.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color14.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color14.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color14.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color15.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color15.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color15.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color15.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color16.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color16.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color16.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color16.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color17.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color17.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color17.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color17.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color18.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color18.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color18.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color18.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color19.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color19.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color19.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color19.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color2.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color2.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color20.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color20.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color20.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color20.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color21.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color21.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color21.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color21.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color22.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color22.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color22.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color22.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color23.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color23.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color23.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color23.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color24.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color24.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color24.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color24.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color25.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color25.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color25.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color25.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color26.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color26.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color26.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color26.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color27.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color27.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color27.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color27.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color28.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color28.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color28.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color28.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color29.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color29.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color29.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color29.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color3.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color3.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color30.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color30.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color30.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color30.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color31.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color31.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color31.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color31.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color32.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color32.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color32.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color32.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color33.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color33.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color33.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color33.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color34.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color34.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color34.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color34.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color35.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color35.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color35.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color35.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color36.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color36.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color36.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color36.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color4.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color4.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color5.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color5.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color5.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color5.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color6.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color6.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color6.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color6.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color7.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color7.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color7.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color7.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color8.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color8.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color8.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color8.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color9.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color9.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/color9.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/color9.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color1.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color1.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color1.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color1.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color2.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color2.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color2.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color2.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color3.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color3.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color3.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color3.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color4.ans
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color4.ans
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/ex_color4.in
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/ex_color4.in
(Stored with Git LFS)
Normal file
Binary file not shown.
BIN
S2OJ/1687/data/problem.conf
(Stored with Git LFS)
Normal file
BIN
S2OJ/1687/data/problem.conf
(Stored with Git LFS)
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user