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 maxn 100005
int head[maxn];
long long len[maxn];
pair<int,long long> rec[maxn];
vector<pair<int,long long> > from[maxn];
vector<int> p[maxn];
vector<long long> a;
void dfs(int x,int last,int dist) {
int i;
len[x] = len[last] + dist;
rec[x] = {last, dist};
for(i=0;i<from[x].size();i++) if(from[x][i].first!=last) dfs(from[x][i].first,x,from[x][i].second);
}
int findhead(int x) {
if(head[x]==x) return x;
return head[x] = findhead(head[x]);
}
bool cmp(int x,int y) {
return x>y;
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]) {
int i,j,now;
long long sum,full,half,ans;
for(i=0;i<N;i++) head[i] = i;
for(i=0;i<M;i++) from[A[i]].push_back({B[i],T[i]}), from[B[i]].push_back({A[i],T[i]}), head[findhead(A[i])] = findhead(B[i]);
for(i=0;i<N;i++) p[findhead(i)].push_back(i);
ans = 0;
for(i=0;i<N;i++) {
if(p[i].size()==0) continue;
for(j=0;j<p[i].size();j++) len[p[i][j]] = 0;
//First
now = p[i][0];
dfs(now,-1,0);
//Second
for(j=0;j<p[i].size();j++) if(len[p[i][j]]>len[now]) now = p[i][j];
for(j=0;j<p[i].size();j++) len[p[i][j]] = 0;
dfs(now,-1,0);
//Track
for(j=0;j<p[i].size();j++) if(len[p[i][j]]>len[now]) now = p[i][j];
full = half = len[now]; sum = 0;
while(now!=-1) {
sum += rec[now].second;
now = rec[now].first;
half = min(half,max(sum,full-sum));
}
ans = max(ans,full);
a.push_back(half);
}
sort(&a[0],&a[a.size()],cmp);
if(a.size()>1) ans = max(ans,a[0]+a[1]+L);
if(a.size()>2) ans = max(ans,a[1]+a[2]+L+L);
return ans;
}
Compilation message (stderr)
dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:15:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<from[x].size();i++) if(from[x][i].first!=last) dfs(from[x][i].first,x,from[x][i].second);
~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:33:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<p[i].size();j++) len[p[i][j]] = 0;
~^~~~~~~~~~~~
dreaming.cpp:38:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<p[i].size();j++) if(len[p[i][j]]>len[now]) now = p[i][j];
~^~~~~~~~~~~~
dreaming.cpp:39:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<p[i].size();j++) len[p[i][j]] = 0;
~^~~~~~~~~~~~
dreaming.cpp:42:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=0;j<p[i].size();j++) if(len[p[i][j]]>len[now]) now = p[i][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... |