QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#827299 | #3561. Capital City | RedDiamond# | 0 | 151ms | 45744kb | C++20 | 3.0kb | 2024-12-22 21:11:35 | 2024-12-22 21:11:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
int n, k;
vector<vector<int>> g, occ;
vector<int> c, vis, sz, par, dep, in;
vector<int> l, r;
int tt = 0;
int centr = -1;
int ans = 1e9;
void build(int u, int p = 0) {
l[u] = ++tt;
for (auto v : g[u]) {
if (v != p) {
build(v, u);
}
}
r[u] = tt;
}
void dfs(int u, int size, int p) {
sz[u] = 1;
par[u] = p;
bool good = 1;
for (auto v : g[u]) {
if (v != p && !vis[v]) {
dfs(v, size, u);
sz[u] += sz[v];
good &= (sz[v] <= size / 2);
}
}
good &= (size - sz[u] <= size / 2);
if (good == 1) {
centr = u;
}
}
vector<ll> f;
void add(int i, int x) {
for (; i <= n; i += i & -i) {
f[i] += x;
}
}
void add(int l, int r, int x) {
add(l, +x);
add(r + 1, -x);
}
int get(int i) {
int sum = 0;
for (; i >= 1; i -= i & -i) {
sum += f[i];
}
return sum;
}
void update_ans(int centr) {
queue<int> q;
for (auto x : occ[c[centr]]) {
if (get(l[x]) > 0) {
return;
}
q.push(x);
}
in[c[centr]] = 1;
vector<int> revert;
revert.push_back(c[centr]);
int total = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
if (c[u] != c[par[u]] && in[c[par[u]]] == 0) {
in[c[par[u]]] = 1;
revert.push_back(c[par[u]]);
total += 1;
for (auto x : occ[c[par[u]]]) {
if (get(l[x]) > 0) {
return;
}
q.push(x);
}
}
}
for (auto i : revert) {
in[i] = 0;
}
ans = min(ans, total - 1);
}
void solve(int u, int p = 0) {
centr = -1;
dfs(u, 0, u);
dfs(u, sz[u], u);
par[centr] = centr;
update_ans(centr);
vis[centr] = 1;
dep[centr] = dep[p] + 1;
par[centr] = p;
add(l[centr], r[centr], +1);
int c = centr;
for (auto v : g[u]) {
if (!vis[v]) {
solve(v, c);
}
}
add(l[centr], r[centr], -1);
}
int32_t main() {
if (1) {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n >> k;
g.resize(n + 1);
for (int i = 1; i < n; i += 1) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
c.resize(n + 1);
occ.resize(k + 1);
in.resize(k + 1);
for (int i = 1; i <= n; i += 1) {
cin >> c[i];
occ[c[i]].push_back(i);
}
l.resize(n + 1);
r.resize(n + 1);
f.resize(n + 1);
build(1);
vis.resize(n + 1);
dep.resize(n + 1);
par.resize(n + 1);
sz.resize(n + 1);
solve(1);
cout << ans << "\n";
return 0;
}
詳細信息
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 1
Accepted
time: 1ms
memory: 3536kb
input:
20 3 16 10 10 3 18 2 4 5 8 6 11 12 2 14 1 2 6 3 1 11 1 4 7 20 3 2 9 7 3 13 15 19 5 7 17 6 12 15 2 2 1 1 1 2 2 1 3 3 1 3 1 3 2 2 1 2 2 3
output:
2
result:
ok single line: '2'
Test #2:
score: 1
Accepted
time: 0ms
memory: 3824kb
input:
20 3 13 1 5 17 14 1 15 2 19 17 7 9 4 6 12 5 15 18 1 2 16 20 3 4 11 8 2 7 9 16 5 1 3 2 5 8 7 10 1 2 3 2 1 3 3 3 2 3 3 3 3 2 2 1 3 1 2 3
output:
2
result:
ok single line: '2'
Test #3:
score: 1
Accepted
time: 0ms
memory: 3540kb
input:
20 3 7 6 9 13 12 11 16 6 20 8 14 17 2 3 9 11 4 2 2 1 12 14 15 8 18 16 9 19 10 4 2 9 8 3 4 5 5 6 2 2 2 3 2 3 3 1 2 2 1 2 1 1 1 2 3 2 3 3
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Wrong Answer
time: 0ms
memory: 3628kb
input:
20 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 10 9 8 5 6 7 1 2 3 4 5 6 7 1 2 3 4 8 9 10
output:
9
result:
wrong answer 1st lines differ - expected: '6', found: '9'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #31:
score: 0
Wrong Answer
time: 151ms
memory: 45744kb
input:
200000 100000 185785 19011 19011 181550 181550 117972 117972 192238 192238 137685 137685 10126 10126 193657 193657 130856 130856 119980 119980 37122 37122 24497 24497 162102 162102 104298 104298 61332 61332 103789 103789 71060 71060 54044 54044 12075 12075 55296 55296 70106 70106 27512 27512 190160 ...
output:
0
result:
wrong answer 1st lines differ - expected: '4', found: '0'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%