QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#227519 | #2766. Unique Cities | Camillus | 0 | 155ms | 26540kb | C++20 | 3.2kb | 2023-10-27 17:11:07 | 2023-10-27 17:11:07 |
Judging History
answer
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
vector<int> g[200500];
int c[200500];
int d[200500];
int find_deepest(int u, int p = -1) {
int res = u;
d[u] = 0;
for (int v : g[u]) {
if (v != p) {
int cur = find_deepest(v, u);
d[cur] += 1;
if (d[cur] > d[res]) {
res = cur;
}
}
}
return res;
}
int dp[200500];
void set_deep(int u, int p = -1) {
if (p == -1) {
dp[u] = d[u] = 0;
}
for (int v : g[u]) {
if (v != p) {
dp[v] = d[v] = d[u] + 1;
set_deep(v, u);
dp[u] = max(dp[u], dp[v]);
}
}
for (int &v : g[u]) {
if (v == p) {
swap(g[u].back(), v);
break;
}
}
sort(g[u].begin(), g[u].end() - (p != -1), [](int u, int v) {
return dp[u] > dp[v];
});
}
pair<int, int> ans[200500];
vector<int> path;
set<int> pos[200500];
int cnt = 0;
bool remove(int v) {
if (pos[c[v]].contains(d[v])) {
pos[c[v]].erase(d[v]);
if (pos[c[v]].empty()) {
cnt -= 1;
}
return true;
} else {
return false;
}
}
void add(int v) {
if (!pos[c[v]].contains(d[v])) {
pos[c[v]].insert(d[v]);
if (pos[c[v]].size() == 1) {
cnt += 1;
}
}
}
void dfs(int u, int p = -1) {
int mx1 = 0;
int mx2 = 0;
if (g[u].size() >= 1 && g[u][0] != p) {
mx1 = dp[g[u][0]] - d[u];
}
if (g[u].size() >= 2 && g[u][1] != p) {
mx2 = dp[g[u][1]] - d[u];
}
vector<int> removed;
for (int i = d[u] - 1; i >= d[u] - mx2 && i >= 0; i--) {
int v = path[i];
if (remove(v)) {
removed.push_back(v);
}
}
path.push_back(u);
add(u);
if (mx1 != 0) {
dfs(g[u].front(), u);
}
for (int i = min(d[u] - 1, d[u] - mx1); i >= d[u] - mx1 && i >= 0; i--) {
int v = path[i];
if (remove(v)) {
removed.push_back(v);
}
}
for (int v : g[u]) {
if (v != p && v != g[u].front()) {
dfs(v, u);
}
}
path.pop_back();
remove(u);
if (ans[u] == make_pair(0, 0)) {
ans[u] = make_pair(d[u], cnt);
} else if (d[u] > ans[u].first) {
ans[u] = make_pair(d[u], cnt);
}
for (int v : removed) {
add(v);
}
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr);
#endif
int n, m;
cin >> n >> m;
if (n == 2) {
cout << "1\n1\n";
return 0;
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for (int u = 1; u <= n; u++) {
cin >> c[u];
}
int d1 = find_deepest(1);
int d2 = find_deepest(d1);
set_deep(d1);
dfs(d1);
set_deep(d2);
dfs(d2);
for (int u = 1; u <= n; u++) {
cout << ans[u].second << '\n';
}
return 0;
}
详细
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 4
Accepted
time: 5ms
memory: 19488kb
input:
2 1 1 2 1 1
output:
1 1
result:
ok 2 lines
Test #2:
score: -4
Wrong Answer
time: 0ms
memory: 21504kb
input:
1842 848 740 1093 1299 922 801 1560 265 1664 1043 65 1430 427 80 233 4 1238 1623 1473 1569 274 953 1485 1259 649 1671 1409 246 542 742 1517 720 1120 1527 1328 1167 1531 1056 1130 673 1222 192 980 1393 913 446 688 135 23 1065 1787 978 1481 1765 1720 310 202 1406 1451 475 523 104 774 1531 829 169 396 ...
output:
1 1 1 0 2 1 2 0 3 1 2 1 0 3 0 1 0 2 0 0 4 2 1 2 4 0 0 2 1 1 3 2 4 1 0 1 2 3 0 2 2 2 2 1 4 0 2 2 1 3 0 2 1 0 1 1 1 2 2 1 2 2 1 2 1 3 4 1 2 2 0 0 1 0 0 0 2 1 3 1 4 3 2 2 2 0 1 3 1 2 1 1 3 2 1 1 1 3 3 1 2 1 2 2 1 1 2 4 2 3 1 0 0 4 0 1 1 2 3 2 3 0 5 2 1 5 0 1 2 2 0 2 0 2 1 2 4 1 2 0 3 1 3 1 3 3 1 2 0 3 ...
result:
wrong answer 5th lines differ - expected: '0', found: '2'
Subtask #2:
score: 0
Wrong Answer
Test #33:
score: 0
Wrong Answer
time: 88ms
memory: 25184kb
input:
115391 1 32067 50006 1710 5850 21016 66364 72998 34367 24783 10670 49950 93666 81835 81234 53480 68963 87357 43320 93905 30509 72528 92224 520 100511 54804 2917 58490 23858 93643 87530 90737 65205 60740 110812 9553 90266 70028 67222 108045 88982 35584 110286 53518 21733 108735 26404 108228 109796 92...
output:
1 1 0 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
wrong answer 20th lines differ - expected: '0', found: '1'
Subtask #3:
score: 0
Wrong Answer
Test #50:
score: 0
Wrong Answer
time: 155ms
memory: 26540kb
input:
157976 157976 20171 157173 44732 54119 107845 121149 109200 110309 82678 108206 89140 64200 36916 128759 3966 123760 92978 105716 43700 146126 14924 3429 107721 36095 94906 78173 97162 29552 119574 39605 25145 138492 16622 99431 60281 7949 76176 136644 75716 91518 127987 110605 77999 110960 101187 5...
output:
1 6 3 4 4 2 3 2 2 3 2 1 4 3 3 2 7 2 1 3 1 5 2 1 3 2 1 3 2 5 3 1 1 2 1 1 3 2 3 5 4 6 3 2 1 3 2 3 4 5 3 2 2 4 2 4 3 2 2 2 2 4 2 3 1 3 1 2 1 2 2 3 1 3 2 2 2 5 2 1 1 9 2 5 2 1 4 2 2 3 2 2 2 4 1 4 2 1 1 1 2 1 3 3 4 2 7 2 1 2 3 2 2 1 1 2 1 2 3 2 1 2 8 2 2 1 2 1 1 5 2 3 2 1 3 1 4 1 2 4 3 2 10 6 3 1 3 2 2 9...
result:
wrong answer 2nd lines differ - expected: '5', found: '6'
Subtask #4:
score: 0
Skipped
Dependency #1:
0%