이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define N 1000001
vector<pair<int, long long> > adj[N];
bool in[N];
vector<int> s;
long long dp[N][3];
vector<int> cyc;
bool inCyc[N];
void dfs(int v, int p){
long long b1 = 0, b2 = 0;
for(auto xx : adj[v]){
int x = xx.first;
long long c = xx.second;
if(inCyc[x] || p == x) continue;
dfs(x, v);
long long up;
dp[v][0] += dp[x][2];
up = dp[x][1] - dp[x][2] + c;
if(b1 < up){
b2 = b1;
b1 = up;
} else b2 = max(b2, up);
}
dp[v][1] = dp[v][0] + b1;
dp[v][2] = dp[v][0] + b1 + b2;
}
long long ans = 0;
unordered_map<int, unordered_map<int, int> > lol;
void solve(){
for(int x : s){
if(inCyc[x]) {
cyc.push_back(x);
dfs(x, 0);
}
}
long long dp[2][2][3][3];
memset(dp, -0x3f, sizeof dp);
dp[0][0][0][0] = ::dp[cyc[0]][0];
dp[0][0][1][1] = ::dp[cyc[0]][1];
dp[0][0][2][2] = ::dp[cyc[0]][2];
int st = 0;
for(int i = 1; i < cyc.size(); i++){
st = i & 1;
memset(dp[st], -0x3f, sizeof dp[st]);
for(int t = 0; t <= 2; t++){
for(int j = 0; j <= 2; j++){
for(int l = 0; l <= 2; l++){
dp[st][1][t][j] = max(dp[st][1][t][j], dp[st ^ 1][0][t][l] + ::dp[cyc[i]][j]);
dp[st][1][t][j] = max(dp[st][1][t][j], dp[st ^ 1][1][t][l] + ::dp[cyc[i]][j]);
}
}
if(i == 1 && t == 2) break;
for(int j = 1; j <= 2; j++){
for(int l = 0; l < 2; l++){
if(i == 1){
dp[st][1][t + 1][j] = max(dp[st][1][t + 1][j], dp[st ^ 1][1][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]);
dp[st][0][t + 1][j] = max(dp[st][0][1 + t][j], dp[st ^ 1][0][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]);
} else {
dp[st][1][t][j] = max(dp[st][1][t][j], dp[st ^ 1][1][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]);
dp[st][0][t][j] = max(dp[st][0][t][j], dp[st ^ 1][0][t][l] + ::dp[cyc[i]][j - 1] + lol[cyc[i]][cyc[i - 1]]);
}
}
}
}
}
long long res = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
res = max(res, dp[st][0][i][j]);
res = max(res, dp[st][1][i][j]);
}
}
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
res = max(res, dp[st][1][i][j] + lol[cyc[0]][cyc.back()]);
}
}
ans += res;
}
int trav(int i, int p){
s.push_back(i);
in[i] = 1;
int res = 0;
bool ok = 0;
for(auto xx : adj[i]){
int x = xx.first;
if(p == x && ok == 0){
ok = 1;
continue;
}
if(in[x]){
res = x;
continue;
}
int t = trav(x, i);
if(res < t)res = t;
}
if(res) inCyc[i] = 1;
if(res == i) return 0;
return res;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; i++){
int l, e;
cin>>e>>l;
adj[e].push_back({i, l});
adj[i].push_back({e, l});
lol[e][i] = max(lol[e][i], l);
lol[i][e] = max(lol[i][e], l);
}
for(int i = 1; i <= n; i++){
if(in[i]) continue;
s.clear();
cyc.clear();
trav(i, 0);
solve();
}
cout<<ans;
}
컴파일 시 표준 에러 (stderr) 메시지
islands.cpp: In function 'void solve()':
islands.cpp:50:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < cyc.size(); i++){
~~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |