QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#119790 | #5659. Watching Cowflix | pandapythoner# | 8.333333 | 0ms | 3608kb | C++14 | 4.0kb | 2023-07-05 19:28:57 | 2024-07-04 00:18:58 |
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 <= 1e3);
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: 3608kb
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: 3532kb
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
Runtime Error
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000010000000...
output:
result:
Test #4:
score: 0
Runtime Error
input:
5000 0100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000...
output:
result:
Test #5:
score: 0
Runtime Error
input:
5000 0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000001000000000000000001000000000000000000000000000000000000000000000000...
output:
result:
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...