QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#746122 | #5659. Watching Cowflix | _8_8_# | 12.5 | 2817ms | 58688kb | C++23 | 4.0kb | 2024-11-14 13:31:51 | 2024-11-14 13:31:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int)2e5 + 12, MOD = (int)1e9 + 7, b = 19;
vector<int> g[N], f[N];
bool ok[N], blocked[N];
int n, r[N], s[N], up[N][20], dep[N], tin[N], tout[N], timer;
void build(int v, int pr) {
up[v][0] = pr;
for(int i = 1; i < b; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
tin[v] = ++timer;
for(int to:g[v]) {
if(to != pr) {
dep[to] = dep[v] + 1;
build(to, v);
}
}
tout[v] = ++timer;
}
bool is(int v, int u) {
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v, int u) {
if(is(v, u)) return v;
if(is(u, v)) return u;
for(int i = b - 1; i >= 0; i--) {
if(!is(up[v][i], u)) {
v = up[v][i];
}
}
return up[v][0];
}
int get(int v, int u) {
return dep[v] + dep[u] - 2 * dep[lca(u, v)];
}
void dfs(int v, int pr = -1) {
s[v] = 1;
for(int to:g[v]) {
if(to == pr || blocked[to]) continue;
dfs(to, v);
s[v] += s[to];
}
}
int find(int v, int pr, int total) {
for(int to:g[v]) {
if(to != pr && !blocked[to] && s[to] > total / 2) {
return find(to, v, total);
}
}
return v;
}
void decompose(int v, int pr = 0) {
dfs(v);
v = find(v, -1, s[v]);
blocked[v] = 1;
r[v] = pr;
for(int to:g[v]) {
if(!blocked[to]) {
decompose(to, v);
}
}
}
set<pair<int, int>> st[N];
int dp[N], c;
void make(int v) {
auto [val, i] = (*st[v].begin());
st[v].erase(st[v].begin());
while(!st[v].empty()) {
auto [x, j] = (*st[v].begin());
if(i == j) {
st[v].erase(st[v].begin());
continue;
} else {
break;
}
}
st[v].insert({val, i});
}
void add(int v, int val) {
int x = v;
while(v) {
st[v].insert({get(x, v), val});
make(v);
v = r[v];
}
}
void er(int v, int val) {
int x = v;
while(v) {
st[v].erase({get(v, x), val});
v = r[v];
}
}
void test() {
cin >> n;
for(int i = 1; i <= n; ++i) {
char x;cin >> x;
if(x == '1') {
ok[i] = 1;
c++;
}
f[i].push_back(i);
}
if(c == 1) {
for(int i = 1; i <= n; i++) {
cout << 1 + i << '\n';
}
return;
}
for(int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dep[1] = 1;
build(1, 1);
decompose(1);
dp[c] = c;
for(int i = 1; i <= n; i++) {
if(ok[i]) {
add(i, i);
}
}
auto merge = [&](int x, int y) {
if((int)f[x].size() > (int)f[y].size()) {
swap(x, y);
}
for(int i:f[x]) {
er(i, x);
add(i, y);
f[y].push_back(i);
}
f[x].clear();
};
for(int i = c - 1; i >= 1; i--) {
dp[i] = dp[i + 1];
array<int, 3> val = {(int)1e9, (int)1e9, (int)1e9};
for(int j = 1; j <= n; j++) {
if(st[j].size() >= 2) {
auto it = (st[j].begin());
int x = (*it).second, u = (*it).first - 1;
it++;
int y = (*it).second;
u += (*it).first;
val = min(val, {u, x, y});
}
}
merge(val[1], val[2]);
dp[i] += val[0];
}
// for(int i = 1; i <= c; i++) {
// cout << i << ' ' << dp[i] << '\n';
// }
for(int k = 1; k <= n; k++) {
ll res = (int)1e9;
for(int j = 1; j <= c; j++) {
res = min(res, j * 1ll * k + dp[j]);
}
cout << res << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 4.16667
Accepted
time: 6ms
memory: 20400kb
input:
5 10001 1 2 2 3 3 4 4 5
output:
4 6 8 9 10
result:
ok 5 number(s): "4 6 8 9 10"
Test #2:
score: 4.16667
Accepted
time: 0ms
memory: 18464kb
input:
7 0001010 7 4 5 6 7 2 5 1 6 3 2 5
output:
4 6 8 9 10 11 12
result:
ok 7 numbers
Test #3:
score: 0
Wrong Answer
time: 3ms
memory: 22768kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000010000000...
output:
921 1149 1270 1374 1464 1549 1627 1696 1757 1810 1859 1907 1949 1989 2029 2067 2104 2140 2176 2211 2245 2279 2313 2345 2375 2404 2433 2461 2489 2516 2542 2568 2593 2617 2640 2663 2685 2707 2729 2751 2772 2792 2811 2829 2847 2865 2882 2899 2915 2930 2945 2959 2972 2984 2995 3006 3016 3026 3036 3046 3...
result:
wrong answer 1st numbers differ - expected: '711', found: '921'
Test #4:
score: 0
Wrong Answer
time: 12ms
memory: 22972kb
input:
5000 0100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000...
output:
896 1275 1581 1822 1986 2110 2196 2269 2335 2388 2435 2477 2518 2558 2596 2633 2669 2704 2739 2773 2806 2838 2869 2899 2928 2957 2985 3012 3039 3065 3090 3114 3137 3159 3181 3203 3224 3244 3263 3282 3301 3319 3337 3354 3370 3386 3402 3417 3431 3444 3456 3467 3477 3486 3495 3504 3513 3521 3528 3534 3...
result:
wrong answer 1st numbers differ - expected: '890', found: '896'
Test #5:
score: 0
Wrong Answer
time: 13ms
memory: 19088kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000000000000000000000000...
output:
811 1073 1272 1431 1571 1692 1794 1881 1955 2014 2064 2107 2148 2185 2221 2257 2292 2326 2359 2391 2423 2455 2487 2519 2550 2581 2612 2642 2672 2701 2729 2756 2782 2807 2832 2856 2879 2902 2924 2946 2967 2987 3006 3025 3043 3060 3076 3091 3105 3119 3132 3144 3155 3165 3174 3182 3190 3197 3203 3208 3...
result:
wrong answer 1st numbers differ - expected: '794', found: '811'
Test #6:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #9:
score: 0
Wrong Answer
time: 2277ms
memory: 43572kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16490 20070 21629 22871 23941 24873 25665 26347 26941 27463 27921 28332 28706 29044 29347 29627 29889 30143 30383 30612 30832 31046 31253 31453 31650 31840 32026 32211 32395 32578 32759 32938 33114 33287 33459 33631 33801 33971 34140 34308 34476 34643 34809 34975 35140 35305 35470 35634 35797 35960 ...
result:
wrong answer 1st numbers differ - expected: '12231', found: '16490'
Test #10:
score: 0
Wrong Answer
time: 2764ms
memory: 46972kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16071 22857 28333 32326 35032 36842 38050 38864 39441 39865 40191 40458 40689 40897 41097 41295 41489 41680 41869 42056 42242 42427 42611 42794 42976 43157 43337 43516 43695 43873 44050 44226 44401 44575 44748 44920 45091 45261 45430 45599 45768 45937 46105 46273 46440 46607 46773 46938 47103 47268 ...
result:
wrong answer 1st numbers differ - expected: '15993', found: '16071'
Test #11:
score: 0
Wrong Answer
time: 2683ms
memory: 44940kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14892 19416 22791 25557 27895 29956 31782 33380 34719 35763 36466 36954 37270 37510 37715 37905 38090 38274 38457 38640 38823 39006 39188 39370 39551 39732 39912 40092 40272 40451 40629 40807 40984 41161 41337 41512 41687 41862 42037 42211 42384 42556 42728 42899 43070 43241 43411 43580 43748 43916 ...
result:
wrong answer 1st numbers differ - expected: '14608', found: '14892'
Test #12:
score: 0
Wrong Answer
time: 2278ms
memory: 44104kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16478 20048 21589 22807 23868 24793 25598 26296 26907 27449 27930 28359 28750 29112 29439 29750 30039 30308 30570 30828 31077 31322 31555 31783 32007 32229 32449 32668 32885 33100 33312 33522 33730 33937 34142 34345 34547 34749 34949 35148 35347 35546 35745 35943 36140 36337 36533 36728 36922 37116 ...
result:
wrong answer 1st numbers differ - expected: '12198', found: '16478'
Test #13:
score: 0
Wrong Answer
time: 2735ms
memory: 47680kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16153 22969 28408 32404 35139 36956 38168 38969 39551 39998 40339 40624 40866 41085 41295 41499 41696 41891 42085 42278 42469 42659 42847 43035 43222 43408 43594 43779 43963 44147 44330 44512 44693 44873 45053 45232 45410 45587 45763 45938 46113 46287 46461 46635 46809 46982 47154 47325 47495 47664 ...
result:
wrong answer 1st numbers differ - expected: '16072', found: '16153'
Test #14:
score: 0
Wrong Answer
time: 2692ms
memory: 45660kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14976 19574 23042 25872 28296 30443 32370 34056 35457 36569 37366 37878 38227 38493 38725 38943 39155 39367 39577 39786 39993 40200 40406 40611 40816 41020 41223 41425 41626 41826 42025 42223 42420 42616 42812 43008 43204 43399 43593 43786 43979 44171 44362 44553 44743 44933 45122 45310 45497 45684 ...
result:
wrong answer 1st numbers differ - expected: '14721', found: '14976'
Test #15:
score: 0
Wrong Answer
time: 2255ms
memory: 43184kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16352 19894 21443 22689 23772 24697 25496 26179 26778 27298 27771 28196 28580 28937 29264 29566 29842 30097 30333 30563 30787 31005 31217 31424 31626 31823 32018 32211 32403 32588 32770 32950 33128 33306 33483 33660 33835 34009 34182 34354 34525 34695 34865 35035 35204 35372 35540 35707 35874 36040 ...
result:
wrong answer 1st numbers differ - expected: '12121', found: '16352'
Test #16:
score: 0
Wrong Answer
time: 2817ms
memory: 46996kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16264 23104 28624 32651 35407 37232 38440 39232 39789 40195 40511 40775 41014 41234 41441 41635 41828 42017 42205 42392 42578 42763 42947 43130 43312 43492 43671 43849 44027 44204 44381 44558 44735 44911 45087 45262 45436 45609 45781 45952 46123 46294 46464 46633 46801 46968 47134 47299 47463 47626 ...
result:
wrong answer 1st numbers differ - expected: '16192', found: '16264'
Test #17:
score: 0
Wrong Answer
time: 2640ms
memory: 47012kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14958 19571 23034 25840 28262 30391 32271 33941 35331 36399 37159 37682 38044 38323 38577 38823 39063 39301 39538 39774 40009 40244 40478 40711 40944 41176 41407 41637 41867 42096 42325 42553 42780 43006 43231 43455 43679 43902 44124 44345 44565 44785 45004 45223 45441 45659 45876 46092 46307 46521 ...
result:
wrong answer 1st numbers differ - expected: '14705', found: '14958'
Test #18:
score: 0
Wrong Answer
time: 2275ms
memory: 43632kb
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16624 20169 21687 22911 23974 24899 25692 26387 26994 27520 27983 28400 28768 29103 29422 29713 29984 30239 30479 30705 30922 31133 31340 31546 31747 31944 32137 32328 32515 32700 32883 33063 33240 33416 33591 33765 33936 34106 34276 34446 34616 34785 34953 35120 35286 35451 35615 35779 35942 36104 ...
result:
wrong answer 1st numbers differ - expected: '12282', found: '16624'
Test #19:
score: 4.16667
Accepted
time: 116ms
memory: 40572kb
input:
100000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 25...
result:
ok 100000 numbers
Test #20:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #21:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #22:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #23:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #24:
score: 0
Wrong Answer
time: 285ms
memory: 58688kb
input:
200000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150 153 156 159 162 165 168 171 174 177 180 183 186 189 192 195 198 201 204 207 210 213 216 219 222 225 228 231 234 237 240 243 246 249 252 25...
result:
wrong answer 18425th numbers differ - expected: '43404', found: '43405'