#include <algorithm>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <cassert>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<30;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-8;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
const int M=1000000;
int n,deg[M],par[M];
bool vis[M];
vp g[M];
ll res,c[M],d[M];
ll dfs(int v,int pre=-1){
vl a(2,0);
for(auto p:g[v]){
int u=p.first;
if(!deg[u]&&u!=pre) a.push_back(dfs(u,v)+p.second);
}
sort(a.rbegin(),a.rend());
res=max(res,a[0]+a[1]);
return a[0];
}
ll f(int v){
res=0;
vi a,b;
vis[v]=1;
queue<int> q;
q.push(v);
while(!q.empty()){
v=q.front();q.pop();
a.push_back(v);
for(auto p:g[v]){
int u=p.first;
if(!vis[u]){
vis[u]=1;
q.push(u);
}
}
}
for(auto i:a) if(deg[i]==1) q.push(i);
while(!q.empty()){
v=q.front();q.pop();
deg[v]--;
for(auto p:g[v]){
int u=p.first;
if(deg[u]){
par[v]=u;
deg[u]--;
if(deg[u]==1) q.push(u);
}
}
}
int len=0;
for(auto i:a) if(deg[i]){
v=i;
len++;
}
int now=v,pre=-1,pw=-1;
ll t=0;
do{
b.push_back(now);
c[now]=t;
vp tmp;
for(auto p:g[now]){
int u=p.first;
tmp.push_back(p);
if(u!=pre){
pre=now;
pw=p.second;
now=u;
t+=p.second;
break;
}
}
if(len==2&&b.size()==2){
if(tmp[0].second==pw) swap(tmp[0],tmp[1]);
now=v;
t+=tmp[0].second;
}
}while(now!=v);
ll res=0,mx=-INF;
for(auto i:b) d[i]=dfs(i);
for(auto i:b){
res=max(res,c[i]+d[i]+mx);
mx=max(mx,-c[i]+d[i]);
}
mx=-INF;
for(auto i:b){
res=max(res,t-c[i]+d[i]+mx);
mx=max(mx,c[i]+d[i]);
}
return res;
}
int main(){
fill(par,par+M,-1);
scanf("%d",&n);
for(int u=0;u<n;u++){
int v,w;
scanf("%d%d",&v,&w);
v--;
g[u].push_back({v,w});
g[v].push_back({u,w});
deg[u]++,deg[v]++;
}
ll sum=0;
for(int i=0;i<n;i++) if(!vis[i]) sum+=f(i);
printf("%lld\n",sum);
}
Compilation message
islands.cpp: In function 'int main()':
islands.cpp:129:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
129 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
islands.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
132 | scanf("%d%d",&v,&w);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
27776 KB |
Output is correct |
2 |
Runtime error |
709 ms |
131072 KB |
Execution killed with signal 9 |
3 |
Correct |
18 ms |
27776 KB |
Output is correct |
4 |
Incorrect |
17 ms |
27776 KB |
Output isn't correct |
5 |
Correct |
19 ms |
27776 KB |
Output is correct |
6 |
Correct |
18 ms |
27776 KB |
Output is correct |
7 |
Runtime error |
707 ms |
131076 KB |
Execution killed with signal 9 |
8 |
Runtime error |
714 ms |
131076 KB |
Execution killed with signal 9 |
9 |
Incorrect |
18 ms |
27776 KB |
Output isn't correct |
10 |
Incorrect |
19 ms |
27776 KB |
Output isn't correct |
11 |
Correct |
20 ms |
27776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
704 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
708 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
714 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
743 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
814 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
50328 KB |
Output is correct |
2 |
Runtime error |
922 ms |
131076 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
606 ms |
95748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
544 ms |
131076 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |