QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#884123 | #6298. Coloring | AMATSUKAZE | WA | 11ms | 17536kb | C++20 | 5.0kb | 2025-02-05 21:26:29 | 2025-02-05 21:26:37 |
Judging History
answer
#include<bits/stdc++.h>
#define rep(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define all(v) begin(v),end(v)
using namespace std;
using ll = long long;
bool chmin(auto &a, auto b){ return a > b ? a = b, 1 : 0; }
bool chmax(auto &a, auto b){ return a < b ? a = b, 1 : 0; }
int main(){
cin.tie(0)->sync_with_stdio(0);
int n, st; cin >> n >> st;
st--;
vector<ll> w(n);
rep(i,0,n) cin >> w[i];
vector ikeru(n, vector<int>(0));
vector<ll> p(n);
rep(i,0,n) cin >> p[i];
vector<int> deg(n);
vector<int> a(n);
rep(i,0,n) {
cin >> a[i];
a[i]--;
ikeru[a[i]].push_back(i);
deg[a[i]]++;
}
vector<int> mada;
rep(i,0,n) {
if (deg[i] == 0) {
mada.push_back(i);
}
}
vector<bool> is_cycle(n, true);
while(!mada.empty()){
int i = mada.back();
mada.pop_back();
is_cycle[i] = false;
deg[a[i]]--;
if(deg[a[i]] == 0) {
mada.push_back(a[i]);
}
}
vector child_tree(n, vector<int>(0));
vector dp(n, vector<ll>(1)); // total profit of each subtree (i), alternating white and black (j) times.
auto dfs = [&](auto self, int i) -> void {
vector<ll> tmp(2);
tmp[0] = 0;
tmp[1] = w[i];
for (int j: ikeru[i]) {
self(self, j);
int sx = (int)tmp.size();
int sy = (int)dp[j].size();
vector<ll> ntmp(max(sx, sy + 1), -2e18);
rep(x,0,sx) {
rep(y,0,sy) {
if ((x+y)%2 == 0) {
chmax(ntmp[max(x, y)], tmp[x] + dp[j][y]);
}else{
chmax(ntmp[max(x, y + 1)], tmp[x] + dp[j][y]);
}
}
}
swap(ntmp, tmp);
}
rep(x,0,(int)tmp.size()) {
tmp[x] -= p[i] * x;
}
swap(dp[i], tmp);
};
rep(i,0,n) {
if (is_cycle[i]) {
for (int j: ikeru[i]) {
if (is_cycle[j]) continue;
dfs(dfs, j);
child_tree[i].push_back(j);
}
}
}
ll ans = -4e18;
if (!is_cycle[st]) {
chmax(ans, p[st]);
chmax(ans, dp[st][1] + p[st]);
if ((int)dp[st].size() >= 3) {
chmax(ans, dp[st][2] + p[st]);
}
cout << ans << '\n';
return 0;
}
rep(i,0,n) {
if (is_cycle[i]) {
for (int j: child_tree[i]) {
rep(k,0,(int)dp[j].size()-1) {
chmax(dp[j][k+1], dp[j][k]);
}
}
}
}
vector<int> cycle_list;
int now = st;
do {
cycle_list.push_back(now);
for (int j: ikeru[now]) {
if (is_cycle[j]) {
now = j;
break;
}
}
} while(now != st);
// cycle junkai
int m = (int)cycle_list.size();
auto get = [&](int i, int j) -> ll {
ll ret = 0;
for (int x: child_tree[i]) {
ret += dp[x][min(j,(int)dp[x].size()-1)];
}
return ret;
};
if (m == 2) {
int x = cycle_list[0];
int y = cycle_list[1];
chmax(ans, get(x, 2) - p[x]);
chmax(ans, get(x, 1) + w[x]);
chmax(ans, get(x, 1) + get(y, 1) + w[x] + w[y] - p[y]);
cout << ans << endl;
return 0;
}
vector<ll> tmp(m+1);
int sz = 1<<13;
vector<ll> seg(2*sz, -3e18);
auto seg_set = [&](int i) -> void {
seg[i+sz] = tmp[i];
i += sz;
i >>= 1;
while(i > 0) {
seg[i] = max(seg[i*2], seg[i*2+1]);
i >>= 1;
}
};
ll geta = 0;
seg_set(0);
rep(i,0,m) {
if (i > 0) geta -= p[cycle_list[i]];
ll tar = get(cycle_list[i], 1) + w[cycle_list[i]];
geta += tar;
tmp[i+1] = tmp[i] + get(cycle_list[i], 2) - p[cycle_list[i]] - tar;
seg_set(i+1);
/*
rep(j,0,m+1) {
cout << tmp[j] + geta << ' ';
}
cout << endl;
*/
chmax(ans, seg[1] + geta);
}
vector alls(n+4, vector<ll>(m+1));
rep(i,0,n+4){
rep(j,0,m) {
alls[i][j+1] += alls[i][j];
alls[i][j+1] += get(cycle_list[j], 2*i) - p[cycle_list[j]] * (2*i) + (j==0?p[cycle_list[j]]:0);
}
}
rep(i,0,(n+1)*m) {
ll tar1 = get(cycle_list[i%m], 2*(i/m)+3) - get(cycle_list[i%m], 2*(i/m)+2) + w[cycle_list[i%m]];
geta += tar1;
geta -= p[cycle_list[i%m]];
tmp[i%(m+1)] = alls[(i/m)+2][i%m+1] - alls[(i/m)+2][0] + alls[(i/m)+1][m] - alls[(i/m)+1][i%m+1] - geta;
seg_set(i%(m+1));
/*
rep(j,0,m+1) {
cout << tmp[j]+geta << ' ';
}
cout << endl;
*/
chmax(ans, seg[1] + geta);
}
cout << ans << endl;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3712kb
input:
3 1 -1 -1 2 1 0 0 3 1 2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3712kb
input:
10 8 36175808 53666444 14885614 -14507677 -92588511 52375931 -87106420 -7180697 -158326918 98234152 17550389 45695943 55459378 18577244 93218347 64719200 84319188 34410268 20911746 49221094 8 1 2 2 8 8 4 7 8 4
output:
35343360
result:
ok 1 number(s): "35343360"
Test #3:
score: 0
Accepted
time: 9ms
memory: 17536kb
input:
5000 1451 531302480 400140870 -664321146 -376787089 -440627168 -672055995 924309614 2764785 -225700856 880835131 -435550509 162278080 -635253658 251803267 -499868931 213283508 603121701 -603347266 541062018 -502078443 -585620031 486788884 864390909 -670529282 -63580194 512939729 691685896 481123612 ...
output:
83045140866
result:
ok 1 number(s): "83045140866"
Test #4:
score: 0
Accepted
time: 11ms
memory: 17536kb
input:
5000 325 790437050 -881570876 262369906 -462921420 -706598183 -486649546 -226864203 505745549 30451944 124046215 968419787 -21612898 145640891 11293206 830678227 214238313 -277762746 363570356 -123386912 -428728586 -928118626 44181027 -201770288 -776436064 -758985629 -330862963 -543373739 -904928126...
output:
484763000532
result:
ok 1 number(s): "484763000532"
Test #5:
score: 0
Accepted
time: 0ms
memory: 4352kb
input:
5000 4607 680975399 657968174 934047594 549055751 -677601906 596210389 865855282 82355240 -761574106 735853519 -869885284 249543935 992464614 770783145 530083741 -846596899 657436500 -165262643 664242609 -355378729 -934180733 970169392 170553447 808713917 -790827552 927447834 -353592386 697328858 -9...
output:
115329035
result:
ok 1 number(s): "115329035"
Test #6:
score: 0
Accepted
time: 1ms
memory: 4352kb
input:
5000 1889 571513748 -139398179 237129062 -340222790 -648605629 -484432867 -94780943 585336004 -861292487 -347660823 -402754561 -477474972 207884555 -235305792 -565925744 -552584412 963481326 261922222 541534795 -987061581 -276679328 822528827 507932827 -914620698 -486232986 -113967286 205280230 -121...
output:
1212882154486
result:
ok 1 number(s): "1212882154486"
Test #7:
score: -100
Wrong Answer
time: 2ms
memory: 4480kb
input:
5000 3754 830648318 210762768 -908806750 -426357121 -914576644 593993710 102368241 -456912987 666043575 -254435419 304220058 410438717 349675568 -994795731 896735039 -553539217 -343155079 -432210729 -713794273 913711724 -987774144 748517191 -845312206 20527478 -181638420 636923229 720531586 -9135341...
output:
6627360
result:
wrong answer 1st numbers differ - expected: '-6627360', found: '6627360'