QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#883736 | #6298. Coloring | AMATSUKAZE# | WA | 8ms | 17792kb | C++20 | 4.6kb | 2025-02-05 18:33:31 | 2025-02-05 18:33:32 |
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 ((i+j)%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, dp[st][1] + 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;
};
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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3840kb
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: 1ms
memory: 3840kb
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: -100
Wrong Answer
time: 8ms
memory: 17792kb
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:
-1999999998501060745
result:
wrong answer 1st numbers differ - expected: '83045140866', found: '-1999999998501060745'