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;
const int N = 1e5 + 10;
vector<int> adj[N], cost[N], t;
int vis[N], ans, r, f[N], g[N];
void dfs(int u, int par) {
vis[u] = 1;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
dfs(v, u);
f[u] = max(f[u], f[v] + c);
}
}
void dfs2(int u, int par, int D) {
int m1 = D, m2 = -1e9;
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
m2 = max(m2, f[v] + c);
if(m2 > m1) swap(m1, m2);
}
g[u] = m1;
ans = max(ans, g[u]);
r = min(r, g[u]);
for(int i = 0; i < adj[u].size(); i++) {
int v = adj[u][i], c = cost[u][i];
if(v == par) continue;
dfs2(v, u, (f[v] + c == m1 ? m2 : m1) + c);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++) {
int u = A[i], v = B[i], c = T[i];
adj[u].push_back(v);
adj[v].push_back(u);
cost[u].push_back(c);
cost[v].push_back(c);
}
vector<int> R;
for(int i = 0; i < N; i++) if(!vis[i]) {
r = N;
dfs(i, -1);
dfs2(i, -1, 0);
R.push_back(r);
}
sort(R.begin(), R.end(), greater<int>());
if(R.size() > 1) ans = max(ans, R[0] + R[1] + L);
if(R.size() > 2) ans = max(ans, R[1] + R[2] + 2*L);
return ans;
}
Compilation message (stderr)
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:11:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int, int)':
dreaming.cpp:20:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
dreaming.cpp:29:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++) {
~~^~~~~~~~~~~~~~~
# | 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... |