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"
#define edge pair<int, int>
#define to first
#define cost second
using namespace std;
const int MAX_N = 123456;
vector<edge> adj[MAX_N]; vector<int> R;
bool visited[MAX_N];
int maxdist, point;
edge parent[MAX_N];
void dfs1(int u, int par, int val) {
int i, mx = 0;
if(val > maxdist) {
maxdist = val;
point = u;
}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(v.to == par) continue;
dfs1(v.to, u, v.cost + val);
}
}
void dfs2(int u, int par, int val) {
visited[u] = 1;
int i, mx = 0;
if(val > maxdist) {
maxdist = val;
point = u;
}
for(i = 0; i < (int) adj[u].size(); i++) {
edge v = adj[u][i];
if(v.to == par) continue;
parent[v.to] = edge(u, v.cost);
dfs2(v.to, u, val+v.cost);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
int i, ans = 0, j;
for(i = 0; i < M; i++) {
adj[A[i]].push_back(edge(B[i], T[i]));
adj[B[i]].push_back(edge(A[i], T[i]));
}
for(i = 0; i < N; i++) {
if(visited[i]) continue;
maxdist = -1; dfs1(i, i, 0); int one = point;
maxdist = -1; dfs2(one, one, 0); int two = point;
ans = max(ans, maxdist);
int able = maxdist/2, d = 0;
for(j = two; j != one; j = parent[i].to) {
if(d + parent[j].cost > able) {
d = min(d + parent[j].cost, maxdist-d);
break;
} d += parent[j].cost;
} R.push_back(d);
} sort(R.begin(), R.end(), greater<int>());
if(R.size() > 1) ans = max(ans, R[0]+L+R[1]);
if(R.size() > 2) ans = max(ans, R[1]+L+L+R[2]);
return ans;
}
Compilation message (stderr)
dreaming.cpp: In function 'void dfs1(int, int, int)':
dreaming.cpp:16:12: warning: unused variable 'mx' [-Wunused-variable]
int i, mx = 0;
^~
dreaming.cpp: In function 'void dfs2(int, int, int)':
dreaming.cpp:33:12: warning: unused variable 'mx' [-Wunused-variable]
int i, mx = 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... |