QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#751263 | #5659. Watching Cowflix | _8_8_ | 25 | 76ms | 73564kb | C++23 | 4.8kb | 2024-11-15 17:47:00 | 2024-11-15 17:47:00 |
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});
// cout << st.back() << ' ' << i <<
par[i] = st.back();
w[i] = dep[i] - dep[st.back()];
}
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;
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;
}
}
er[mn + mn1 + 1].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++) {
for(int j:er[k]) {
remove(j);
}
vector<int> rr;
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];
}
}
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]});
// cout << t << ' ' << f << "x\n";
}
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: 20024kb
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: 24048kb
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: 45ms
memory: 24864kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000010000000...
output:
921 1162 1290 1379 1450 1512 1565 1609 1650 1690 1729 1768 1805 1842 1879 1915 1950 1984 2018 2051 2083 2115 2147 2178 2208 2237 2266 2294 2322 2349 2375 2401 2426 2450 2473 2496 2518 2540 2562 2584 2605 2625 2644 2662 2680 2698 2715 2732 2748 2763 2778 2792 2805 2817 2828 2839 2849 2859 2869 2879 2...
result:
wrong answer 1st numbers differ - expected: '711', found: '921'
Test #4:
score: 0
Wrong Answer
time: 65ms
memory: 20980kb
input:
5000 0100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000...
output:
895 1310 1629 1746 1819 1879 1934 1983 2030 2075 2118 2160 2201 2239 2276 2312 2348 2383 2418 2452 2485 2517 2548 2578 2607 2636 2664 2691 2718 2744 2769 2793 2816 2838 2860 2882 2903 2923 2942 2961 2980 2998 3016 3033 3049 3065 3081 3096 3110 3123 3135 3146 3156 3165 3174 3183 3192 3200 3207 3213 3...
result:
wrong answer 1st numbers differ - expected: '890', found: '895'
Test #5:
score: 0
Wrong Answer
time: 31ms
memory: 21376kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000000000000000000000000...
output:
811 1113 1331 1421 1476 1527 1577 1624 1667 1708 1748 1787 1825 1862 1898 1934 1969 2003 2036 2068 2100 2132 2164 2196 2227 2258 2289 2319 2349 2378 2406 2433 2459 2484 2509 2533 2556 2579 2601 2623 2644 2664 2683 2702 2720 2737 2753 2768 2782 2796 2809 2821 2832 2842 2851 2859 2867 2874 2880 2885 2...
result:
wrong answer 1st numbers differ - expected: '794', found: '811'
Test #6:
score: 4.16667
Accepted
time: 69ms
memory: 72796kb
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:
ok 200000 numbers
Test #7:
score: 4.16667
Accepted
time: 55ms
memory: 72820kb
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:
ok 200000 numbers
Test #8:
score: 4.16667
Accepted
time: 70ms
memory: 73564kb
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:
ok 200000 numbers
Test #9:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16490 20215 21872 22833 23448 23935 24350 24695 25009 25292 25556 25802 26034 26261 26483 26696 26905 27109 27311 27508 27702 27894 28083 28270 28455 28637 28816 28995 29172 29348 29523 29697 29870 30042 30213 30384 30554 30724 30893 31061 31229 31396 31562 31728 31893 32058 32223 32387 32550 32713 ...
result:
Test #10:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16071 23587 29667 31671 32468 32939 33280 33563 33806 34026 34231 34429 34626 34821 35012 35202 35391 35579 35766 35952 36137 36321 36504 36687 36869 37050 37230 37409 37588 37766 37943 38119 38294 38468 38641 38813 38984 39154 39323 39492 39661 39830 39998 40166 40333 40500 40666 40831 40996 41161 ...
result:
Test #11:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14891 20445 24689 26079 26228 26423 26721 26995 27208 27408 27599 27786 27971 28156 28341 28525 28709 28893 29076 29259 29442 29625 29807 29989 30170 30351 30531 30711 30891 31070 31248 31426 31603 31780 31956 32131 32306 32481 32656 32830 33003 33175 33347 33518 33689 33860 34030 34199 34367 34535 ...
result:
Test #12:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16474 19143 20671 21645 22298 22829 23278 23681 24033 24360 24666 24953 25230 25495 25749 25990 26222 26452 26681 26908 27132 27354 27572 27789 28003 28216 28427 28637 28846 29054 29261 29467 29672 29876 30079 30281 30482 30683 30882 31081 31280 31479 31678 31876 32073 32270 32466 32661 32855 33049 ...
result:
Test #13:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16153 23666 29728 31686 32516 32988 33333 33609 33853 34079 34293 34495 34695 34891 35084 35277 35470 35662 35854 36045 36235 36424 36612 36800 36987 37173 37359 37544 37728 37912 38095 38277 38458 38638 38818 38997 39175 39352 39528 39703 39878 40052 40226 40400 40574 40747 40919 41090 41260 41429 ...
result:
Test #14:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14975 20565 24948 26462 26328 26521 26850 27133 27380 27609 27828 28043 28257 28470 28682 28893 29103 29313 29522 29730 29937 30144 30350 30555 30760 30964 31167 31369 31570 31770 31969 32167 32364 32560 32756 32952 33148 33343 33537 33730 33923 34115 34306 34497 34687 34877 35066 35254 35441 35628 ...
result:
Test #15:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
12207 15151 16721 17641 18233 18672 19059 19408 19722 20013 20288 20549 20794 21031 21257 21472 21682 21888 22091 22291 22488 22682 22874 23062 23248 23432 23615 23797 23978 24158 24336 24513 24689 24865 25041 25217 25392 25566 25739 25911 26082 26252 26422 26592 26761 26929 27097 27264 27431 27597 ...
result:
Test #16:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
16264 23832 29978 31990 32844 33333 33668 33936 34166 34385 34590 34791 34987 35179 35370 35558 35745 35931 36117 36303 36488 36672 36855 37037 37218 37398 37577 37755 37933 38110 38287 38464 38641 38817 38993 39168 39342 39515 39687 39858 40029 40200 40370 40539 40707 40874 41040 41205 41369 41532 ...
result:
Test #17:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
14958 20585 24944 26459 26329 26527 26858 27154 27415 27662 27905 28146 28387 28627 28867 29106 29345 29583 29820 30056 30291 30526 30760 30993 31226 31458 31689 31919 32149 32378 32607 32835 33062 33288 33513 33737 33961 34184 34406 34627 34847 35067 35286 35505 35723 35941 36158 36374 36589 36803 ...
result:
Test #18:
score: 0
Time Limit Exceeded
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
12410 15489 17031 17943 18534 19023 19420 19779 20109 20410 20680 20932 21172 21404 21623 21836 22045 22247 22444 22640 22835 23028 23220 23411 23602 23791 23976 24160 24342 24523 24703 24880 25055 25230 25404 25576 25747 25917 26087 26257 26427 26596 26764 26931 27097 27262 27426 27590 27753 27915 ...
result:
Test #19:
score: 4.16667
Accepted
time: 36ms
memory: 43144kb
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:
32378 47438 59333 63130 64667 65539 66130 66586 66964 67293 67595 67889 68170 68445 68718 68987 69254 69520 69785 70050 70315 70579 70842 71104 71366 71627 71887 72146 72404 72661 72917 73172 73426 73680 73933 74185 74436 74686 74935 75184 75432 75679 75926 76173 76419 76664 76908 77151 77393 77634 ...
result:
Test #21:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
29630 40691 49259 52109 53163 52866 53470 53928 54288 54595 54884 55163 55440 55715 55988 56261 56533 56804 57075 57346 57617 57887 58157 58427 58697 58967 59236 59505 59773 60040 60306 60571 60836 61100 61363 61625 61887 62149 62411 62672 62932 63192 63451 63709 63966 64223 64479 64734 64989 65244 ...
result:
Test #22:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
32609 39910 43030 44735 45862 46710 47424 48040 48595 49092 49552 49964 50341 50705 51057 51393 51720 52039 52346 52648 52941 53229 53512 53793 54073 54352 54628 54901 55173 55444 55714 55980 56245 56509 56773 57036 57299 57561 57823 58084 58345 58605 58864 59123 59381 59639 59896 60152 60408 60663 ...
result:
Test #23:
score: 0
Time Limit Exceeded
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
32385 47496 59629 63564 65119 65981 66568 67004 67371 67700 68009 68301 68584 68858 69128 69397 69666 69935 70204 70471 70737 71002 71266 71530 71794 72058 72321 72583 72845 73106 73367 73627 73886 74145 74403 74660 74917 75173 75429 75684 75938 76191 76443 76694 76944 77193 77441 77688 77935 78181 ...
result:
Test #24:
score: 0
Wrong Answer
time: 76ms
memory: 56456kb
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 5925th numbers differ - expected: '17778', found: '17777'