This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include"dreaming.h"
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
const int nax=1e5+5;
vector<bool> vis(nax);
pair<pair<int,int>,pair<int,int>> dp[nax];
vector<pair<int,int>> adj[nax];
int res=0;
pair<int,int> mx={-1,-1};
void init(int x,int p){
dp[x]={{0,0},{0,0}};
vis[x]=true;
for(auto u:adj[x]){
if(u.fi==p) continue;
init(u.fi,x);
if(u.se+dp[u.fi].fi.fi>dp[x].fi.fi){
dp[x].se=dp[x].fi;
dp[x].fi.fi=dp[u.fi].fi.fi+u.se;
dp[x].fi.se=u.fi;
}else if(u.se+dp[u.fi].fi.fi>dp[x].se.fi){
dp[x].se.fi=dp[u.fi].fi.fi+u.se;
dp[x].se.se=u.fi;
}
}
return;
}
int ans;
void dfs(int x,int p){
res=max(res,dp[x].fi.fi+dp[x].se.fi);
ans=min(ans,dp[x].fi.fi);
for(auto u:adj[x]){
if(u.fi==p) continue;
pair<int,int> cur;
if(u.fi==dp[x].fi.se){
cur=dp[x].se;
cur.fi+=u.se;
cur.se=x;
}else{
cur=dp[x].fi;
cur.fi+=u.se;
cur.se=x;
}
if(cur.fi>dp[u.fi].fi.fi){
dp[u.fi].se=dp[u.fi].fi;
dp[u.fi].fi=cur;
}else if(cur.fi>dp[u.fi].se.fi){
dp[u.fi].se=cur;
}
dfs(u.fi,x);
}
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]){
for (int i = 0; i < M; ++i)
{
adj[A[i]].pb({B[i],T[i]});
adj[B[i]].pb({A[i],T[i]});
}
for (int i = 0; i < N; ++i)
{
if(vis[i]) continue;
init(i,-1);
ans=1e9;
dfs(i,-1);
if(mx.fi<ans){
mx.se=mx.fi;
mx.fi=ans;
}else if(mx.se<ans){
mx.se=ans;
}
}
if(M==N-1) return res;
res=max(res,mx.fi+mx.se+L);
return res;
}/*
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n,m,l;
cin>>n>>m>>l;
int a[m];
int b[m];
int t[m];
for (int i = 0; i < m; ++i)
{
cin>>a[i]>>b[i]>>t[i];
}
cout <<travelTime(n,m,l,a,b,t)<<endl;
}*/
# | 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... |