# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
684188 |
2023-01-20T15:43:46 Z |
starplat |
Islands (IOI08_islands) |
C++14 |
|
1062 ms |
131072 KB |
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,u,w,vis[1000005],no[1000005],is[1000005];
int pt,done,cycle[1000005],bck[1000005],mx,ans;
int node,dist,dp[1000005],par[2000005],we[2000005];
vector<pair<int,int>> g[1000005];
void dfs(int x,int p){
if (done) return;
if (vis[x]){
int cnt=0;
for (auto i:g[x]) cnt+=i.first==p;
if (cnt==2){
cycle[++pt]=x;
cycle[++pt]=p;
is[x]=true; is[p]=true;
}
else {
while (p!=x){
is[p]=true;
cycle[++pt]=p;
p=bck[p];
if (p==-1) break;
}
is[x]=true; cycle[++pt]=x;
}
done=true;
}
else {
vis[x]=1; bck[x]=p;
for (auto i:g[x]){
if (i.first!=p) dfs(i.first,x);
}
}
}
void mem(int x){
no[x]=1;
for (auto i:g[x]){
if (!no[i.first]) mem(i.first);
}
}
void dfs1(int x,int p,int d){
if (d>dist) {
node=x; dist=d;
}
for (auto i:g[x]) {
if (i.first!=p && !is[i.first]) dfs1(i.first,x,d+i.second);
}
}
void treedp(int x,int p){
for (auto i:g[x]){
if (is[i.first] || i.first==p) continue;
treedp(i.first,x);
}
for (auto i:g[x]){
if (is[i.first] || i.first==p) continue;
dp[x]=max(dp[x],dp[i.first]+i.second);
}
if (p==-1){
vector <int> q;
for (auto i:g[x]){
if (is[i.first] || i.first==p) continue;
q.push_back(dp[i.first]+i.second);
}
sort(q.begin(),q.end(),greater<int>());
if (q.size()>1){
mx=max(mx,q[0]+q[1]);
}
if (q.size()) mx=max(q[0],mx);
}
}
int weight(int x,int tar){
for (auto i:g[x]) if (i.first==tar) return i.second;
return 0;
}
void work(){
int sum=0;
priority_queue<pair<int,int>> q;
for (int i=2;i<=pt;i++) we[i]=we[i+pt]=weight(cycle[i-1],cycle[i]);
we[pt+1]=weight(cycle[pt],cycle[1]);
for (int i=1;i<=2*pt;i++) par[i]=par[i-1]+we[i];
for (int i=2;i<=pt;i++) q.push({par[i]+dp[cycle[i]],i});
for (int i=1;i<=pt;i++){
while (!q.empty() && q.top().second<=i) q.pop();
if (!q.empty()) mx=max(mx,q.top().first+dp[cycle[i]]-sum);
// cout<<i<<" "<<q.top().first+dp[cycle[i]]-sum<<"\n";
sum+=we[i+1]; q.push({par[pt+i]-par[i+1]+dp[cycle[i]]+sum,pt+i});
}
}
signed main(){
cin>>n;
for (int i=1;i<=n;i++){
cin>>u>>w;
g[i].push_back({u,w});
g[u].push_back({i,w});
bck[i]=-1;
}
for (int i=1;i<=n;i++){
if (no[i]) continue;
node=mx=dist=done=pt=0;
dfs(i,-1); mem(i);
if (done){
for (int j=1;j<=pt;j++){
treedp(cycle[j],-1);
for (auto k:g[cycle[j]]){
if (is[k.first]) continue;
node=dist=0;
dfs1(k.first,-1,0);
dfs1(node,-1,0);
mx=max(mx,dist);
}
}
if (pt==2){
int tmp=0;
for (auto j:g[cycle[1]]){
if (j.first==cycle[2]) tmp=max(tmp,j.second);
}
mx=max(mx,dp[cycle[1]]+dp[cycle[2]]+tmp);
}
else {
work();
reverse(cycle+1,cycle+1+pt);
work();
}
}
else {
dfs1(i,-1,0);
dfs1(node,-1,0);
mx=max(mx,dist);
}
// cout<<mx<<"\n";
ans+=mx;
}
cout<<ans<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
16 ms |
23764 KB |
Output is correct |
3 |
Correct |
13 ms |
23892 KB |
Output is correct |
4 |
Correct |
13 ms |
23840 KB |
Output is correct |
5 |
Correct |
14 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23848 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23800 KB |
Output is correct |
9 |
Correct |
15 ms |
23832 KB |
Output is correct |
10 |
Correct |
13 ms |
23792 KB |
Output is correct |
11 |
Correct |
15 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23944 KB |
Output is correct |
2 |
Correct |
14 ms |
23892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23968 KB |
Output is correct |
2 |
Correct |
16 ms |
24412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
25620 KB |
Output is correct |
2 |
Correct |
47 ms |
29300 KB |
Output is correct |
3 |
Correct |
40 ms |
25780 KB |
Output is correct |
4 |
Correct |
22 ms |
24824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
31736 KB |
Output is correct |
2 |
Correct |
94 ms |
34656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
45076 KB |
Output is correct |
2 |
Correct |
194 ms |
54272 KB |
Output is correct |
3 |
Correct |
253 ms |
72848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
386 ms |
60780 KB |
Output is correct |
2 |
Correct |
399 ms |
101208 KB |
Output is correct |
3 |
Correct |
574 ms |
109128 KB |
Output is correct |
4 |
Runtime error |
596 ms |
131072 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
867 ms |
110268 KB |
Output is correct |
2 |
Runtime error |
1062 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
815 ms |
131072 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |