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;
typedef long long ll;
#define maxn 100005
ll ret = 0;
vector<vector<pair<ll,ll> > > adj(maxn);
vector<ll> currcomponent;
vector<ll> length1(maxn,0);
vector<ll> last(maxn,-1);
vector<ll> length2(maxn,0);
void dfs(ll node,ll parent,ll weight){
currcomponent.push_back(node);
length1[node] = weight;
for(auto k:adj[node]){
if(k.first!=parent){
dfs(k.first,node,weight+k.second);
}
}
return;
}
void dfs2(ll node,ll parent,ll weight){
length2[node] = weight;
for(auto k:adj[node]){
if(k.first!=parent){
last[k.first] = node;
dfs2(k.first,node,weight+k.second);
}
}
return;
}
ll find(ll node,ll length){
ll currnode = node;
ll ans = INT_MAX;
while(last[currnode]!=-1){
ans = min(ans,max(abs(length-length2[currnode]),length2[currnode]));
currnode = last[currnode];
}
ret = max(ret,length);
return max(length-ans,ans);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(ll i=0;i<M;i++){
adj[A[i]].push_back({B[i],T[i]});
adj[B[i]].push_back({A[i],T[i]});
}
vector<ll> weights;
for(int i=0;i<N;i++){
if(length1[i]==0){
dfs(i,-1,0);
ll currmx = 0,currnode = 0;
if(currcomponent.size()==1){
weights.push_back(0);
currcomponent.clear();
goto cont;
}
for(int j=0;j<currcomponent.size();j++){
if(length1[currcomponent[j]]>currmx){
currmx = length1[currcomponent[j]];
currnode = currcomponent[j];
}
}
dfs2(currnode,-1,0);
currmx = 0,currnode=0;
for(int j=0;j<currcomponent.size();j++){
if(length2[currcomponent[j]]>currmx){
currmx = length2[currcomponent[j]];
currnode = currcomponent[j];
}
}
weights.push_back(find(currnode,currmx));
currcomponent.clear();
}
cont : ;
}
sort(weights.begin(),weights.end());
if(weights.size()>=2){
ret = max(ret,weights[weights.size()-1]+weights[weights.size()-2]+L);
}
if(weights.size()>=3){
ret = max(ret,weights[weights.size()-3]+weights[weights.size()-2]+2*L);
}
return ret;
}
Compilation message (stderr)
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:58:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<currcomponent.size();j++){
~^~~~~~~~~~~~~~~~~~~~~
dreaming.cpp:66:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<currcomponent.size();j++){
~^~~~~~~~~~~~~~~~~~~~~
# | 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... |