# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
120273 |
2019-06-24T06:22:45 Z |
nvmdava |
Islands (IOI08_islands) |
C++17 |
|
712 ms |
131072 KB |
#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][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][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;
}
Compilation message
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 |
1 |
Correct |
23 ms |
23808 KB |
Output is correct |
2 |
Incorrect |
22 ms |
23936 KB |
Output isn't correct |
3 |
Incorrect |
22 ms |
23928 KB |
Output isn't correct |
4 |
Correct |
23 ms |
23800 KB |
Output is correct |
5 |
Correct |
22 ms |
23808 KB |
Output is correct |
6 |
Correct |
22 ms |
23808 KB |
Output is correct |
7 |
Incorrect |
21 ms |
23808 KB |
Output isn't correct |
8 |
Incorrect |
22 ms |
23844 KB |
Output isn't correct |
9 |
Incorrect |
23 ms |
23800 KB |
Output isn't correct |
10 |
Correct |
22 ms |
23936 KB |
Output is correct |
11 |
Correct |
22 ms |
23936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
24192 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
24312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
60 ms |
28416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
148 ms |
39720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
379 ms |
82668 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
712 ms |
131072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
414 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
425 ms |
131072 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |