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 <stdio.h>
#include <stdlib.h>
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
#define MAX_N 100005
using ii = pair<int,int>;
vector<ii>adj[MAX_N];
vector<bool>vis(MAX_N);
int dist[MAX_N][2];
vector<int>nodes;
void dfs(int u, int p, int d, bool bit){
nodes.eb(u);
vis[u]=1;
dist[u][bit]=d;
for(ii x:adj[u]){
int v=x.fi;
int t=x.se;
if(v==p)continue;
dfs(v,u,d+t,bit);
}
return;
}
int travelTime(int n, int m, int k, int a[], int b[], int t[]) {
for(int i=0; i<m; i++){
adj[a[i]].eb(b[i],t[i]);
adj[b[i]].eb(a[i],t[i]);
}
int ans = 0;
vector<int>answer;
for(int i=0; i<n; i++){
if(vis[i])continue;
nodes=vector<int>();
dfs(i,-1,0,0);
int u=i; //farthestleaf
for(int x:nodes){
if(dist[x][0]>dist[u][0])u=x;
}
nodes=vector<int>();
dfs(u,-1,0,0);
int v=u; //farthest leaf
for(int x:nodes){
if(dist[x][0]>dist[v][0])v=x;
}
nodes=vector<int>();
dfs(v,-1,0,1);
//current max
ans = max(ans,dist[u][1]);
//current min
int res = 1e9;
for(int x:nodes){
res = min(res,max(dist[x][0],dist[x][1]));
}
//cerr<<i<<" "<<dist[u][1]<<" "<<res<<"\n";
answer.eb(res);
}
sort(all(answer),greater<int>());
if(sz(answer)>=2)ans = max(ans,answer[0]+answer[1]+k);
if(sz(answer)>=3)ans = max(ans,answer[1]+answer[2]+2*k);
return ans;
}
/*
int main() {
int N, M, L, i,res;
res = scanf("%d%d%d", &N, &M, &L);
int A[N],B[N],T[N];
cerr<<N<<" "<<M<<" "<<L<<" checking \n";
for (i = 0; i < M; i++)
res = scanf("%d%d%d", &A[i], &B[i], &T[i]);
int answer = travelTime(N, M, L, A, B, T);
printf("%d\n", answer);
return 0;
}
// */
# | 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... |