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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
#include "dreaming.h"
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0);
const int MAXN = 1e5 + 5;
ll n, m, l;
vector<pair<ll, ll>> adjList[MAXN];
bool vis[MAXN];
pair<ll, ll> best[MAXN][2];
vector<ll> nodes;
ll mx = 1e12, idx = 0;
ll ans = 0;
void dfs(int src, int p) {
vis[src] = true;
for (auto i : adjList[src]) {
if (i.first == p) {
continue;
}
dfs(i.first, src);
if(best[i.first][0].first + i.second > best[src][0].first){
best[src][1] = best[src][0];
best[src][0] = best[i.first][0];
best[src][0].first += i.second;
best[src][0].second = i.first;
}
else if (best[i.first][0].first + i.second > best[src][1].first) {
best[src][1] = best[i.first][0];
best[src][1].first += i.second;
best[src][1].second = i.first;
}
}
}
void cnt(int src, int p, int dist) {
if (p != -1) {
if (best[p][0].second != src) {
if (best[p][0].first + dist > best[src][0].first) {
best[src][1] = best[src][0];
best[src][0] = best[p][0];
best[src][0].first += dist;
best[src][0].second = p;
}
else if (best[p][0].first + dist > best[src][1].first) {
best[src][1] = best[p][0];
best[src][1].first += dist;
best[src][1].second = p;
}
}
else {
if (best[p][1].first + dist > best[src][0].first) {
best[src][1] = best[src][0];
best[src][0] = best[p][1];
best[src][0].first += dist;
best[src][0].second = p;
}
else if (best[p][1].first + dist > best[src][1].first) {
best[src][1] = best[p][1];
best[src][1].first += dist;
best[src][1].second = p;
}
}
}
for (auto i : adjList[src]) {
if (i.first == p) {
continue;
}
cnt(i.first, src, i.second);
}
}
void add(int src, int p) {
mx = min(mx, best[src][0].first);
ans = max(ans, best[src][0].first + best[src][1].first);
if (mx == best[src][0].first) {
idx = src;
}
for (auto i : adjList[src]) {
if (i.first == p) {
continue;
}
add(i.first, src);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n = N; m = M; l = L;
for (int i = 0; i < m; i++) {
adjList[A[i]].push_back({ B[i], T[i] });
adjList[B[i]].push_back({ A[i], T[i] });
}
for (int i = 0; i < n; i++) {
if (!vis[i]) {
mx = 1e12;
idx = 0;
dfs(i, -1);
cnt(i, -1, 0);
add(i, -1);
nodes.push_back(mx);
}
}
//for (int i = 0; i < n; i++) {
// cout << i << " " << best[i]->first << endl;
//}
//cout << ans << endl;
sort(nodes.begin(), nodes.end());
reverse(nodes.begin(), nodes.end());
ll mxD = nodes[0];
for (int i = 1; i < nodes.size(); i++) {
ans = max(ans, nodes[i] + mxD + l);
mxD = max(mxD, nodes[i] + l);
}
return ans;
}
Compilation message (stderr)
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:123:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for (int i = 1; i < nodes.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... |