QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#441760 | #8530. Cowntact Tracing | Qwerty1232# | 4.347826 | 436ms | 21956kb | C++23 | 4.9kb | 2024-06-14 17:49:55 | 2024-06-14 17:49:56 |
Judging History
answer
#include <bits/stdc++.h>
int32_t main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::string s;
std::cin >> s;
std::vector<std::vector<int>> gr(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
gr[u].push_back(v);
gr[v].push_back(u);
}
std::vector<bool> used(n);
std::vector<int> roots;
for (int i = 0; i < n; i++) {
if (used[i]) {
continue;
}
if (s[i] == '0') {
gr[i].clear();
continue;
}
auto dfs = [&](auto dfs, int v, int f) {
if (s[v] == '0') {
return;
}
used[v] = true;
for (int t : gr[v]) {
if (t != f) {
dfs(dfs, t, v);
}
}
};
roots.push_back(i);
dfs(dfs, i, -1);
}
std::vector<int> diam(n, -1);
for (int u : roots) {
continue;
if (s[u] == '0') {
continue;
}
std::pair<int, int> max(0, u);
int cnt = 0;
for (int i = 0; i < 2; i++) {
cnt = 0;
auto dfs = [&](auto dfs, int v, int d, int f) {
if (s[v] == '0') {
return;
}
if (std::find_if(gr[v].begin(), gr[v].end(), [&](int t) { return s[t] == '0'; }) != gr[v].end()) {
cnt++;
max = std::max(max, {d, v});
}
for (int t : gr[v]) {
if (t != f) {
dfs(dfs, t, d + 1, v);
}
}
};
int u = max.second;
max = {0, u};
dfs(dfs, u, 0, -1);
}
if (cnt == 0) {
continue;
}
diam[u] = max.first;
}
std::vector<int> sz(n);
for (int u : roots) {
auto dfs = [&](auto dfs, int v, int f) {
if (s[v] == '0') {
return;
}
sz[v] = 1;
for (int& t : gr[v]) {
if (t != f && s[t] == '1') {
dfs(dfs, t, v);
sz[v] += sz[t];
} else {
t = -1;
}
}
std::erase(gr[v], -1);
std::sort(gr[v].begin(), gr[v].end(), [&](int a, int b) { return sz[a] > sz[b]; });
};
dfs(dfs, u, -1);
}
int q;
std::cin >> q;
for (int test = 0; test < q; test++) {
int d;
std::cin >> d;
for (int u : roots) {
if (diam[u] != -1 && 2 * d > diam[u]) {
d = -1;
break;
}
}
if (d == -1) {
std::cout << "-1\n";
continue;
}
std::vector<std::vector<int>> dp(n), dp2(n);
int ans_total = 0;
for (int root : roots) {
auto dfs = [&](auto dfs, int v) -> void {
if (gr[v].empty()) {
dp[v] = {0};
dp2[v] = {1};
return;
}
for (int t : gr[v]) {
dfs(dfs, t);
}
dp[v] = std::move(dp[gr[v][0]]);
dp2[v] = std::move(dp2[gr[v][0]]);
dp[v].push_back(dp2[v].rbegin()[std::min<int>(dp2[v].size() - 1, d)]);
dp2[v].push_back(dp[v].rbegin()[std::min<int>(dp[v].size() - 1, d)] + 1);
for (int i = 1; i < gr[v].size(); i++) {
int t = gr[v][i];
dp[t].push_back(dp2[t].rbegin()[std::min<int>(dp2[t].size() - 1, d)]);
dp2[t].push_back(dp[t].rbegin()[std::min<int>(dp[t].size() - 1, d)] + 1);
if (dp[v].size() < dp[t].size()) {
dp[v].swap(dp[t]);
dp2[v].swap(dp2[t]);
}
for (int i = 0; i <= std::min<int>(d, dp[t].size() - 1); i++) {
dp[v].rbegin()[i] = dp[v].rbegin()[i] + dp[t].rbegin()[i];
dp2[v].rbegin()[i] = std::min<int>({
dp2[v].rbegin()[i] + dp2[t].rbegin()[i],
dp2[v].rbegin()[i] + dp[t].rbegin()[std::min<int>(dp[t].size() - 1, d - i)],
dp[v].rbegin()[std::min<int>(dp[v].size() - 1, d - i)] + dp[t].rbegin()[i],
});
}
}
};
dfs(dfs, root);
int ans = dp2[root].rbegin()[std::min<int>(dp[root].size() - 1, d)];
ans_total += ans;
}
std::cout << ans_total << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 4.34783
Accepted
time: 0ms
memory: 3536kb
input:
5 11111 1 2 2 3 3 4 4 5 6 5 4 3 2 1 0
output:
1 1 1 1 2 5
result:
ok 6 lines
Test #2:
score: 0
Wrong Answer
time: 0ms
memory: 3540kb
input:
10 1111111111 1 2 2 3 2 4 2 5 2 6 6 7 7 8 8 9 9 10 11 0 1 2 3 4 5 6 7 8 9 10
output:
10 3 1 1 1 1 1 1 1 1 1
result:
wrong answer 3rd lines differ - expected: '2', found: '1'
Test #3:
score: 0
Wrong Answer
time: 0ms
memory: 3600kb
input:
5 11100 1 2 2 3 3 4 4 5 6 0 1 2 3 4 5
output:
3 1 1 1 1 1
result:
wrong answer 4th lines differ - expected: '-1', found: '1'
Test #4:
score: 0
Wrong Answer
time: 0ms
memory: 3560kb
input:
10 1101111111 6 4 6 7 4 3 4 2 7 9 9 5 5 1 2 8 2 10 11 2 9 5 0 3 6 10 8 7 1 4
output:
1 0 1 9 3 1 0 0 0 4 1
result:
wrong answer 1st lines differ - expected: '2', found: '1'
Test #5:
score: 0
Wrong Answer
time: 0ms
memory: 3544kb
input:
10 1101011111 7 8 8 2 2 10 10 9 9 4 8 1 10 6 6 3 3 5 11 4 8 6 0 5 3 7 9 10 1 2
output:
0 1 1 8 1 1 1 1 1 3 1
result:
wrong answer 1st lines differ - expected: '-1', found: '0'
Test #6:
score: 0
Wrong Answer
time: 370ms
memory: 20852kb
input:
100000 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
0 0 8 84 5 0 2 2 39 67 1 6 221 11 0 104 1 0 1 1
result:
wrong answer 1st lines differ - expected: '1', found: '0'
Test #7:
score: 0
Wrong Answer
time: 313ms
memory: 21956kb
input:
100000 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
4 4 102 2 5 310 14 9 137 4 4 4 5 4 17 6 5 4 2 1
result:
wrong answer 1st lines differ - expected: '3', found: '4'
Test #8:
score: 0
Wrong Answer
time: 150ms
memory: 16348kb
input:
100000 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
100000 33334 33333 33333 33333 33333 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
wrong answer 4th lines differ - expected: '1', found: '33333'
Test #9:
score: 0
Wrong Answer
time: 0ms
memory: 3888kb
input:
400 11111111101111110011101111111111011111111111111111111111111111111111111111110111111111111111111101111111011111111101111111111111110111111111110110101111111111111111111111111011111111111111110111111111111011111111111111111111110111101111101110111111010011011111111111011111101111110110111111111111...
output:
6 1 9 4 70 5 12 35 28 363 23 41 4 2 1 11 2 3 3 1
result:
wrong answer 1st lines differ - expected: '-1', found: '6'
Test #10:
score: 0
Wrong Answer
time: 1ms
memory: 3620kb
input:
400 11101111111111111111111110110111111111111111011111111111111011111111111111111010111111111110110111011111101111111111111101111111111010111111111111111111111111111111111111101111111111111110111111111111111111111111111111111111111111101011111111111111111110111101111111111111110111111111111111101111...
output:
0 0 62 2 35 0 0 2 144 72 2 27 0 1 0 3 374 2 1 2
result:
wrong answer 1st lines differ - expected: '-1', found: '0'
Test #11:
score: 0
Wrong Answer
time: 1ms
memory: 3824kb
input:
400 11010111101110111110111011010111101011011001111110101101000100011111110101011011011111011110111111111000011110101110011100111111101111101011101110111111111001100111111101101111011011101111111111011101110111111111111011111110111111000111111111111111111111010111101101111111111110111101101110011110...
output:
3 6 6 6 5 0 0 3 3 4 8 14 27 13 0 61 18 0 17 5
result:
wrong answer 2nd lines differ - expected: '7', found: '6'
Test #12:
score: 0
Wrong Answer
time: 11ms
memory: 4332kb
input:
6000 1110011111111111110111111111111111111011011111011110111111110111101011111111110111111110110111111111111111111111111111111111111111101111011111111111111111111011111111111111111111111111111111111111111111111110111111111111111111111111011111111111011111101101111111111111111111111111111111110111111...
output:
18 2 23 11 20 2 48 2 33 5433 20 13 2 54 23 15 2 2 2 26
result:
wrong answer 1st lines differ - expected: '19', found: '18'
Test #13:
score: 0
Wrong Answer
time: 13ms
memory: 4268kb
input:
6000 1111111110011111111101111111111101111111111110011111111011111111111111111110110110011011111110111110111111101111111011110111111111101111111111111001111101110111110111111111111111111111111111111010111111111111111101111111110111111111111111111011111111111111111111111111101111111111010111110111111...
output:
0 0 0 0 0 2 0 68 95 0 229 94 964 0 0 108 0 0 101 0
result:
wrong answer 1st lines differ - expected: '-1', found: '0'
Test #14:
score: 0
Wrong Answer
time: 6ms
memory: 4376kb
input:
6000 1101111110111110101111111111111111111110100111111111111111110111101111111111111111111111111111111111111001111111111111111111111111111111111011111111011110111111110111111111110011111111111111111111111111111111110111111111101111111111100111111111110111110111111011011111110101111111111101111001111...
output:
27 1 2 22 2 25 3 0 3 3 4 0 42 6 2 3 4 2 9 2
result:
wrong answer 1st lines differ - expected: '29', found: '27'
Test #15:
score: 0
Wrong Answer
time: 268ms
memory: 16752kb
input:
100000 11111111101111111101111011100111111011111101000111011001111010011010111110101001110111111110111111110011111111111111110110111110111111110100111011111111111011111101111111001111101101111011010110110011010111111011111111111011111011110110110111111011111001100100011011111111100011010110111011110...
output:
188 45 45 60 144 108 390 153 66 8 25 45 139 113 331 21 54 59 13856 54
result:
wrong answer 1st lines differ - expected: '189', found: '188'
Test #16:
score: 0
Wrong Answer
time: 335ms
memory: 17112kb
input:
100000 11111100010011111111001101110011001111000111010011101100101100011101111101010011111111000101111111111111011110011110110111101111101101111011110111001110111111101101111111111101011110101111111111011110111110111111111111011101111111110110111011110011111111101110111011110111111111110001111011111...
output:
1 12143 3 1 1 1 1 1 4 528 4 1 1 1 4 1 1 2848 1 1
result:
wrong answer 2nd lines differ - expected: '13015', found: '12143'
Test #17:
score: 0
Wrong Answer
time: 382ms
memory: 17468kb
input:
100000 11111111111111111111111111111110101111111011111111111011111111111101111111101111111111111101111111111111111111111111111111101101111111111111111111101111101001111111111110111111110111111110111110111111111111111111111011111101111011101011111111111111111111111111110111111101111111110111101111110...
output:
6 138 74 6 205 124 78 124 68 94 1040 122 163 6 600 107 70 160 131 83
result:
wrong answer 1st lines differ - expected: '-1', found: '6'
Test #18:
score: 0
Wrong Answer
time: 436ms
memory: 17704kb
input:
100000 11111111111111011111111111011101111111111111111111111111111101111001111111111010111110111111111110111111111011111111111011111111111111111111111111011111111111111111111111011111111111110011111110111111111110111111111101111111111111111111111111111101111111110111111101111101111111111111101111111...
output:
542 370 3 1 2 4 2 15509 1 1 3 1 1 8485 5 1 1 1 2 1
result:
wrong answer 1st lines differ - expected: '208', found: '542'
Test #19:
score: 0
Wrong Answer
time: 323ms
memory: 17568kb
input:
100000 11111111111111111111111111110111111111111001111111111111111111110111110111111111111111101111111111111111011110111111111111110011111111111110010111101100111111111111011111111111111111111011111110011111111101010111111111011111111110111111001111011111111111111111111111111111111111111111111111111...
output:
51 39 41 118 68 169 2 35 42 37 66 102 832 52 461 61 2 255 2 155
result:
wrong answer 1st lines differ - expected: '50', found: '51'
Test #20:
score: 0
Wrong Answer
time: 367ms
memory: 17564kb
input:
100000 11111111111111111110101111111111111011110101111111111011111111110111111111111111011111111111100111111101111111111111101101011111111111111111111111111111111111111111111111111011111110111111111111111111011111111111111111111111111111111111111111111101111111101111111111111111111011101111111111111...
output:
241 279 0 1 444 339 36527 1 0 1 1 1 251 3 1 0 2 15747 0 1
result:
wrong answer 1st lines differ - expected: '209', found: '241'
Test #21:
score: 0
Wrong Answer
time: 183ms
memory: 16176kb
input:
100000 00110111111011100111110111000111101111101001010100110101111001111111100011100110001111101100110110010011001011101011110111101011111111111110111101101111111111101110011111001111111111111101111111010111011010101111011110011111101101101010111011011111010110110010111100011010111101110010111110111...
output:
142 125 82 43 294 522 105 123 41 91 177 120 64 85 63 320 40 88 76 41
result:
wrong answer 1st lines differ - expected: '136', found: '142'
Test #22:
score: 0
Wrong Answer
time: 165ms
memory: 17580kb
input:
100000 11011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
99999 50000 49998 49998 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
wrong answer 2nd lines differ - expected: '49999', found: '50000'
Test #23:
score: 0
Wrong Answer
time: 82ms
memory: 15520kb
input:
99901 111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
output:
1 1 2 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 4936 33367
result:
wrong answer 1st lines differ - expected: '-1', found: '1'