QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751514 | #5659. Watching Cowflix | _8_8_ | 16.666667 | 86ms | 61160kb | C++23 | 4.9kb | 2024-11-15 19:13:36 | 2024-11-15 19:13:38 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int)2e5 + 1, MOD = (int)1e9 + 7;
int n, dp[N][2], up[N][20], tin[N], tout[N], timer, b = 20, par[N], w[N], dep[N];
bool ok[N];
set<int> g[N];
vector<pair<int, int>> e[N];
int root;
void build(int v, int pr) {
tin[v] = ++timer;
up[v][0] = pr;
for(int i = 1; i < b; i++) {
up[v][i] = up[up[v][i - 1]][i - 1];
}
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[u] <= tout[v]);
}
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];
}
bool cmp(int x, int y) {
return tin[x] < tin[y];
}
set<int> cur;
int p[N];
vector<int> er[N];
int get(int v) {
if(p[v] == v) return v;
int f = get(p[v]);
return p[v] = f;
}
void make() {
vector<int> t, t1;
for(int i = 1; i <= n; i++) {
if(ok[i]) {
t.push_back(i);
}
}
t1 = t;
sort(t.begin(), t.end(), cmp);
for(int i = 1; i < (int)t.size(); i++) {
t1.push_back(lca(t[i], t[i - 1]));
}
sort(t1.begin(), t1.end());
t1.resize(unique(t1.begin(), t1.end()) - t1.begin());
sort(t1.begin(), t1.end(), cmp);
vector<int> st;
for(int i:t1) {
cur.insert(i);
p[i] = i;
while(!st.empty() && !is(st.back(), i)) {
st.pop_back();
}
if(!st.empty()) {
e[st.back()].push_back({i, dep[i] - dep[st.back()] - 1});
par[i] = st.back();
w[i] = dep[i] - dep[st.back()] - 1;
}
st.push_back(i);
}
}
void prec() {
vector<int> u(n + 1), d(n + 1, 0);
function<void(int)> go = [&](int v){
d[v] = (ok[v] ? 0 : (int)1e9);
for(auto [to, w] : e[v]) {
go(to);
d[v] = min(d[to] + w + 1, d[v]);
}
};
function<void(int)> recalc = [&](int v) {
multiset<int> vals;
vals.insert(u[v]);
for(auto [to, w] : e[v]) {
vals.insert(d[to] + w);
}
for(auto [to, w] : e[v]) {
if(ok[to]) {
u[to] = 0;
recalc(to);
} else {
vals.erase(vals.find(d[to] + w));
u[to] = (*vals.begin()) + 1 + w;
vals.insert(d[to] + w);
}
}
};
go(root);
recalc(root);
for(int i:cur) if(!ok[i]) {
int mn = u[i] - 1, mn1 = (int)1e9;
for(auto [to, f] : e[i]) {
if(d[to] + f < mn) {
mn1 = mn;
mn = d[to] + f;
} else if(d[to] + f < mn1) {
mn1 = d[to] + f;
}
}
if(mn == u[i] - 1 || mn1 == u[i] - 1) er[mn + mn1 + 1].push_back(i);
else {
assert(mn1 < u[i] - 1);
er[u[i] + mn].push_back(i);
}
}
}
void remove(int i) {
p[i] = par[i];
cur.erase(i);
}
void solve(int v, int k) {
dp[v][1] = 1 + k;
for(auto [to, w] : e[v]) {
solve(to, k);
dp[v][0] += min(dp[to][0], dp[to][1]);
dp[v][1] += min(dp[to][0], min(dp[to][1], dp[to][1] + w - k));
}
if(ok[v]) {
dp[v][0] = (int)1e9;
}
}
void test() {
cin >> n;
for(int i = 1; i <= n; i++) {
char x;cin >> x;
if(x == '1') {
ok[i] = 1;
root = i;
}
}
for(int i = 1; i <= n - 1; i++) {
int u, v;
cin >> u >> v;
g[u].insert(v);
g[v].insert(u);
}
// root = 6;
build(root, root);
make();
prec();
for(int i = 1; i <= n; i++) {
e[i].clear();
}
int col = 0;
for(int k = 1; k <= n; k++) {
vector<int> rr;
for(int j:er[k]) {
remove(j);
col += w[j] + 1;
}
// for(int f:cur) if(f != root) {
// int t = get(par[f]);
// if(ok[f] && ok[t] && w[f] <= k) {
// rr.push_back(f);
// col += w[f] + 1;
// }
// }
// for(int f : rr) {
// remove(f);
// }
for(int f:cur) {
if(f == root) continue;
int t = get(par[f]);
e[t].push_back({f, w[f]});
}
solve(root, k);
cout << min(dp[root][0], dp[root][1]) + col << '\n';
for(int f:cur) {
e[f].clear();
dp[f][0] = dp[f][1] = 0;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
test();
}
詳細信息
Pretests
Final Tests
Test #1:
score: 4.16667
Accepted
time: 0ms
memory: 19976kb
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: 20056kb
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: 44ms
memory: 21200kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000010000000...
output:
798 966 1070 1140 1199 1250 1293 1335 1375 1415 1454 1492 1529 1566 1603 1639 1674 1708 1742 1775 1807 1839 1871 1902 1932 1961 1990 2018 2046 2073 2099 2125 2150 2174 2197 2220 2242 2264 2286 2308 2329 2349 2368 2386 2404 2422 2439 2456 2472 2487 2502 2516 2529 2541 2552 2563 2573 2583 2593 2603 26...
result:
wrong answer 1st numbers differ - expected: '711', found: '798'
Test #4:
score: 0
Wrong Answer
time: 65ms
memory: 23040kb
input:
5000 0100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000...
output:
1126 1386 1493 1562 1622 1677 1727 1775 1821 1865 1908 1950 1989 2027 2064 2100 2136 2171 2206 2240 2273 2305 2336 2366 2395 2424 2452 2479 2506 2532 2557 2581 2604 2626 2648 2670 2691 2711 2730 2749 2768 2786 2804 2821 2837 2853 2869 2884 2898 2911 2923 2934 2944 2953 2962 2971 2980 2988 2995 3001 ...
result:
wrong answer 1st numbers differ - expected: '890', found: '1126'
Test #5:
score: 0
Wrong Answer
time: 53ms
memory: 25124kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000000000000000000000000...
output:
901 1121 1237 1315 1374 1424 1473 1517 1559 1600 1640 1679 1717 1754 1790 1826 1861 1895 1928 1960 1992 2024 2056 2088 2119 2150 2181 2211 2241 2270 2298 2325 2351 2376 2401 2425 2448 2471 2493 2515 2536 2556 2575 2594 2612 2629 2645 2660 2674 2688 2701 2713 2724 2734 2743 2751 2759 2766 2772 2777 2...
result:
wrong answer 1st numbers differ - expected: '794', found: '901'
Test #6:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
30804 41189 48182 51855 52223 52591 52958 53324 53690 54055 54419 54782 55144 55505 55865 56224 56582 56939 57295 57650 58004 58357 58709 59061 59412 59762 60111 60459 60806 61152 61497 61841 62184 62526 62867 63207 63546 63884 64222 64559 64895 65230 65564 65898 66231 66563 66894 67224 67554 67883 ...
result:
Test #7:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
30644 41023 48123 51892 52268 52643 53017 53390 53763 54135 54506 54876 55245 55613 55980 56346 56712 57077 57441 57804 58166 58528 58889 59249 59608 59966 60323 60679 61034 61388 61741 62093 62445 62796 63146 63495 63843 64190 64536 64881 65225 65568 65910 66251 66591 66931 67270 67608 67945 68281 ...
result:
Test #8:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
30736 41111 48167 51868 52239 52609 52978 53346 53714 54081 54447 54812 55176 55539 55902 56264 56625 56985 57344 57702 58060 58417 58774 59130 59485 59839 60192 60544 60895 61245 61594 61942 62289 62635 62980 63324 63668 64011 64353 64695 65036 65377 65717 66057 66396 66734 67071 67408 67744 68079 ...
result:
Test #9:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
13996 16250 17294 17952 18426 18828 19167 19477 19756 20017 20261 20493 20720 20942 21155 21363 21567 21770 21968 22163 22355 22545 22733 22919 23102 23282 23461 23639 23816 23992 24166 24340 24513 24684 24855 25026 25196 25366 25535 25703 25871 26038 26204 26370 26535 26700 26865 27029 27192 27355 ...
result:
Test #10:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
20444 25437 26994 27708 28165 28504 28786 29028 29247 29453 29652 29849 30045 30237 30427 30616 30805 30993 31179 31364 31549 31733 31916 32099 32281 32462 32642 32821 33000 33178 33355 33531 33706 33880 34053 34225 34396 34566 34735 34904 35073 35242 35410 35578 35745 35912 36078 36243 36408 36573 ...
result:
Test #11:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16233 20508 22400 23466 24105 24494 24798 25033 25239 25433 25621 25807 25992 26177 26362 26546 26730 26914 27097 27280 27463 27646 27828 28010 28191 28372 28552 28732 28912 29091 29269 29447 29624 29801 29977 30152 30327 30502 30677 30851 31024 31196 31368 31539 31710 31881 32051 32220 32388 32556 ...
result:
Test #12:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
13965 16080 17102 17746 18245 18685 19068 19417 19741 20046 20333 20609 20872 21125 21365 21598 21829 22058 22286 22510 22733 22952 23170 23385 23599 23811 24021 24231 24440 24647 24853 25059 25263 25467 25670 25872 26073 26273 26472 26671 26870 27069 27268 27466 27663 27860 28056 28251 28445 28639 ...
result:
Test #13:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
20530 25534 27118 27885 28346 28681 28955 29198 29425 29639 29842 30043 30240 30434 30627 30820 31013 31205 31397 31588 31778 31967 32155 32343 32530 32716 32902 33087 33271 33455 33638 33820 34001 34181 34361 34540 34718 34895 35071 35246 35421 35595 35769 35943 36117 36290 36462 36633 36803 36972 ...
result:
Test #14:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16227 20643 22721 23866 24594 25061 25390 25661 25899 26124 26341 26556 26770 26983 27195 27406 27616 27826 28035 28243 28450 28657 28863 29068 29273 29477 29680 29882 30083 30283 30482 30680 30877 31073 31269 31465 31661 31856 32050 32243 32436 32628 32819 33010 33200 33390 33579 33767 33954 34141 ...
result:
Test #15:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14058 16256 17307 17875 18340 18742 19076 19385 19669 19940 20199 20444 20681 20907 21123 21334 21541 21744 21944 22142 22337 22530 22719 22906 23091 23274 23457 23638 23819 23998 24176 24353 24529 24705 24881 25057 25232 25406 25579 25751 25922 26092 26262 26432 26601 26769 26937 27104 27271 27437 ...
result:
Test #16:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
20566 25564 27200 27961 28436 28759 29024 29254 29473 29679 29880 30077 30269 30461 30650 30838 31024 31210 31396 31582 31767 31951 32134 32316 32497 32677 32856 33034 33212 33389 33566 33743 33920 34096 34272 34447 34621 34794 34966 35137 35308 35479 35649 35818 35986 36153 36319 36484 36648 36811 ...
result:
Test #17:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16225 20549 22615 23789 24500 24972 25322 25603 25858 26103 26345 26586 26827 27067 27307 27546 27785 28023 28260 28496 28731 28966 29200 29433 29666 29898 30129 30359 30589 30818 31047 31275 31502 31728 31953 32177 32401 32624 32846 33067 33287 33507 33726 33945 34163 34381 34598 34814 35029 35243 ...
result:
Test #18:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14142 16275 17297 17888 18376 18774 19135 19458 19751 20019 20271 20511 20743 20963 21176 21386 21589 21787 21984 22180 22374 22566 22758 22948 23137 23323 23508 23691 23872 24053 24231 24407 24582 24757 24930 25102 25273 25443 25613 25783 25953 26122 26290 26457 26623 26788 26952 27116 27279 27441 ...
result:
Test #19:
score: 4.16667
Accepted
time: 44ms
memory: 40548kb
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:
40802 50710 53678 55060 55881 56457 56905 57282 57610 57913 58208 58490 58766 59039 59309 59576 59842 60107 60372 60637 60902 61166 61429 61691 61953 62214 62474 62733 62991 63248 63504 63759 64013 64267 64520 64772 65023 65273 65522 65771 66019 66266 66513 66760 67006 67251 67495 67738 67980 68221 ...
result:
Test #21:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
32557 40944 44744 46843 48043 48792 49303 49684 50004 50297 50578 50856 51132 51407 51680 51953 52225 52496 52767 53038 53309 53579 53849 54119 54389 54659 54928 55197 55465 55732 55998 56263 56528 56792 57055 57317 57579 57841 58103 58364 58624 58884 59143 59401 59658 59915 60171 60426 60681 60936 ...
result:
Test #22:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
27795 32061 33870 35038 35865 36549 37143 37680 38161 38615 39020 39396 39758 40108 40444 40771 41089 41397 41698 41992 42281 42565 42847 43128 43407 43684 43958 44231 44503 44773 45040 45306 45571 45835 46099 46362 46625 46887 47149 47410 47671 47931 48190 48449 48707 48965 49222 49478 49734 49989 ...
result:
Test #23:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
40836 50904 53882 55303 56125 56697 57133 57498 57826 58136 58428 58711 58986 59257 59526 59795 60064 60333 60601 60868 61134 61399 61663 61927 62191 62455 62718 62980 63242 63503 63764 64024 64283 64542 64800 65057 65314 65570 65826 66081 66335 66588 66840 67091 67341 67590 67838 68085 68332 68578 ...
result:
Test #24:
score: 4.16667
Accepted
time: 86ms
memory: 61160kb
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:
ok 200000 numbers