# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
989479 |
2024-05-28T08:32:38 Z |
huutuan |
Islands (IOI08_islands) |
C++14 |
|
200 ms |
131072 KB |
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+10;
int n, vis[N], par[N], wpar[N], on_cycle[N];
vector<pair<int, int>> g[N];
int pf[N], f[N][2];
void dfs(int u){
vis[u]=1;
int mx1=-1e18, mx2=-1e18;
for (auto &e:g[u]){
int v=e.first, w=e.second;
if (on_cycle[v]) continue;
dfs(v);
int val=f[v][0]+w;
f[u][0]=max(f[u][0], val);
f[u][1]=max(f[u][1], f[v][1]);
if (mx1<val) mx2=mx1, mx1=val;
else mx2=max(mx2, mx1);
}
f[u][1]=max({f[u][1], f[u][0], mx1+mx2});
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i=1; i<=n; ++i){
int j, k; cin >> j >> k;
g[j].emplace_back(i, k);
par[i]=j;
wpar[i]=k;
}
vector<int> ans;
for (int i=1; i<=n; ++i) if (!vis[i]){
int cur=0;
vector<int> cycle;
int u=i;
while (1){
if (vis[u]) break;
vis[u]=1;
cycle.push_back(u);
u=par[u];
}
cycle.erase(cycle.begin(), find(cycle.begin(), cycle.end(), u));
for (int v:cycle) on_cycle[v]=1;
for (int v:cycle){
dfs(v);
cur=max(cur, f[v][1]);
}
for (int j=1; j<=(int)cycle.size(); ++j){
pf[j]=pf[j-1]+wpar[cycle[j-1]];
}
int sum=pf[(int)cycle.size()];
int mx1=-1e18, mx2=-1e18;
for (int j=0; j<(int)cycle.size(); ++j){
cur=max(cur, f[cycle[j]][0]+pf[j]+mx1);
cur=max(cur, f[cycle[j]][0]-pf[j]+mx2);
mx1=max(mx1, f[cycle[j]][0]-pf[j]);
mx2=max(mx2, f[cycle[j]][0]+sum+pf[j]);
}
ans.push_back(cur);
}
sort(ans.rbegin(), ans.rend());
ans.resize(2, 0);
cout << ans[0]+ans[1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
24664 KB |
Output is correct |
2 |
Incorrect |
8 ms |
24668 KB |
Output isn't correct |
3 |
Correct |
9 ms |
24604 KB |
Output is correct |
4 |
Correct |
8 ms |
24668 KB |
Output is correct |
5 |
Incorrect |
8 ms |
24668 KB |
Output isn't correct |
6 |
Incorrect |
8 ms |
24600 KB |
Output isn't correct |
7 |
Incorrect |
8 ms |
24668 KB |
Output isn't correct |
8 |
Incorrect |
9 ms |
24628 KB |
Output isn't correct |
9 |
Incorrect |
8 ms |
24508 KB |
Output isn't correct |
10 |
Incorrect |
9 ms |
24668 KB |
Output isn't correct |
11 |
Incorrect |
8 ms |
24736 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
24668 KB |
Output is correct |
2 |
Incorrect |
9 ms |
24648 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
24664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
12 ms |
25688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
28632 KB |
Output is correct |
2 |
Incorrect |
24 ms |
32184 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
39632 KB |
Output is correct |
2 |
Correct |
47 ms |
43936 KB |
Output is correct |
3 |
Correct |
55 ms |
44996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
77 ms |
54324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
145 ms |
79544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
200 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |