QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#119791 | #5659. Watching Cowflix | pandapythoner# | 20.833333 | 124ms | 4212kb | C++14 | 4.0kb | 2023-07-05 19:29:37 | 2024-07-04 00:19:00 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rnd(234);
const ll inf = 1e18;
int n;
vector<int> s;
vector<vector<int>> g;
vector<ll> rs;
vector<int> usd;
vector<ll> dp_clrd, dp_not_clrd;
int dfs_usdc;
vector<int> dfs_usd;
vector<int> prnt;
void dfs0(int v, int p, ll k){
prnt[v] = p;
dfs_usd[v] = dfs_usdc;
dp_clrd[v] = -1 - k;
dp_not_clrd[v] = 0;
for(auto to: g[v]){
if(dfs_usd[to] != dfs_usdc && to != p && usd[to] == 0){
dfs0(to, v, k);
dp_not_clrd[v] += max(dp_clrd[to], dp_not_clrd[to]);
if(dp_clrd[to] + k > dp_not_clrd[to]){
dp_clrd[v] += dp_clrd[to] + k;
} else{
dp_clrd[v] += dp_not_clrd[to];
}
}
if(usd[to] == 2){
dp_clrd[v] += k;
}
}
}
void dfs1(int v, int p, ll k, bool clrd, ll &dsm, ll &dcmps){
if(clrd){
usd[v] = 1;
dsm += -1 - k;
dcmps -= 1;
for(auto to: g[v]){
if(to != p && usd[to] == 0){
if(dp_clrd[to] + k > dp_not_clrd[to]){
dcmps += 1;
dsm += k;
dfs1(to, v, k, true, dsm, dcmps);
} else{
dfs1(to, v, k, false, dsm, dcmps);
}
}
if(usd[to] == 2){
dsm += k;
dcmps += 1;
}
}
} else{
for(auto to: g[v]){
if(to != p && usd[to] == 0){
if(dp_clrd[to] > dp_not_clrd[to]){
dfs1(to, v, k, true, dsm, dcmps);
} else{
dfs1(to, v, k, false, dsm, dcmps);
}
}
}
}
}
void solve_slow(){
rs.resize(n + 1);
usd.resize(n);
for(int i = 0; i < n; i += 1){
if(s[i] == 0){
usd[i] = 0;
} else{
usd[i] = 2;
}
}
dfs_usdc = 10;
dfs_usd.assign(n, 0);
prnt.resize(n);
dp_clrd.resize(n);
dp_not_clrd.resize(n);
ll sm = 0;
ll components = 0;
for(int i = 0; i < n; i += 1){
if(s[i] == 1){
sm += 1;
components += 1;
}
}
for(int v = 0; v < n; v += 1){
for(auto to: g[v]){
if(v < to && usd[v] == 2 && usd[to] == 2){
components -= 1;
}
}
}
for(int k = 1; k <= n; k += 1){
dfs_usdc += 1;
sm += components;
vector<int> not_usd;
not_usd.reserve(n);
for(int i = 0; i < n; i += 1){
if(usd[i] == 0){
not_usd.push_back(i);
}
}
for(auto v: not_usd){
if(dfs_usd[v] != dfs_usdc){
dfs0(v, -1, k);
ll dsm = 0;
ll dcmps = 0;
if(dp_clrd[v] > dp_not_clrd[v]){
dfs1(v, -1, k, true, dsm, dcmps);
} else{
dfs1(v, -1, k, false, dsm, dcmps);
}
sm -= dsm;
components -= dcmps;
}
}
for(auto v: not_usd){
if(usd[v] == 1){
usd[v] = 2;
}
}
rs[k] = sm;
}
}
int32_t main(){
if(1){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
cin >> n;
assert(n <= 1e4);
s.resize(n);
for(int i = 0; i < n; i += 1){
char c;
cin >> c;
s[i] = c - '0';
}
g.assign(n, vector<int>());
for(int i = 0; i < n - 1; i += 1){
int u, v;
cin >> u >> v;
--u;
--v;
g[u].push_back(v);
g[v].push_back(u);
}
solve_slow();
for(int k = 1; k <= n; k += 1){
cout << rs[k] << "\n";
}
return 0;
}
詳細信息
Test #1:
score: 4.16667
Accepted
time: 0ms
memory: 3624kb
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: 3488kb
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: 4.16667
Accepted
time: 116ms
memory: 4012kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000010000000...
output:
711 837 944 1040 1121 1192 1249 1303 1352 1398 1439 1479 1518 1557 1596 1634 1671 1707 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 261...
result:
ok 5000 numbers
Test #4:
score: 4.16667
Accepted
time: 106ms
memory: 4212kb
input:
5000 0100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000...
output:
890 1226 1433 1547 1619 1676 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 3...
result:
ok 5000 numbers
Test #5:
score: 4.16667
Accepted
time: 124ms
memory: 4128kb
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000000000000000000000000...
output:
794 1025 1178 1284 1364 1422 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:
ok 5000 numbers
Test #6:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #7:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #8:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #9:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #10:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #11:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #12:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #13:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #14:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #15:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #16:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #17:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #18:
score: 0
Runtime Error
input:
100000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #19:
score: 0
Runtime Error
input:
100000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #20:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #21:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #22:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #23:
score: 0
Runtime Error
input:
200000 01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #24:
score: 0
Runtime Error
input:
200000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...