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 "dreaming.h"
#include <vector>
#include <iostream>
using namespace std;
struct Edge{
int to;
int cost;
};
int const NMAX = 1e5;
vector <Edge> g[1 + NMAX];
int below[1 + NMAX];
int above[1 + NMAX];
int visit[1 + NMAX];
void computeBelow(int node, int parent) {
visit[node] = true;
for(int i = 0;i < g[node].size();i++) {
int to = g[node][i].to, cost = g[node][i].cost;
if(to != parent) {
computeBelow(to, node);
below[node] = max(below[node], below[to] + cost);
}
}
}
int diameter = 0;
int computeAbove(int node, int parent) {
int ans = max(above[node], below[node]);
//cerr << node << ' ' << above[node] << ' ' << below[node] << '\n';
diameter = max(diameter, ans);
visit[node] = true;
int best1 = above[node], best2 = 0;
for(int i = 0;i < g[node].size();i++) {
int to = g[node][i].to, cost = g[node][i].cost;
if(to != parent) {
if(best1 < below[to] + cost) {
best2 = best1;
best1 = below[to] + cost;
}else if(best2 < below[to] + cost) {
best2 = below[to] + cost;
}
}
}
for(int i = 0;i < g[node].size();i++) {
int to = g[node][i].to, cost = g[node][i].cost;
if(to != parent) {
if(below[to] + cost == best1) {
above[to] = best2 + cost;
}else {
above[to] = best1 + cost;
}
ans = min(ans, computeAbove(to, node));
}
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0;i < M;i++) {
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
for(int i = 0;i < N;i++) {
if(!visit[i]) {
computeBelow(i, i);
}
}
for(int i = 0;i < N;i++) {
visit[i] = 0;
}
vector <int> len;
int core = 0, ans = 0;
for(int i = 0;i < N;i++) {
if(!visit[i]) {
diameter = 0;
int temp = computeAbove(i, i);
ans = max(ans, diameter);
//cerr << temp << '\n';
len.push_back(temp);
if(len[core] < temp) {
core = len.size()-1;
}
}
}
//cerr << '\n';
int best1 = 0, best2 = 0;
for(int i = 0;i < len.size();i++) {
int temp;
if(i == core) {
temp = len[i];
}else {
temp = len[i] + L;
}
if(best1 < temp) {
best2 = best1;
best1 = temp;
}else if(best2 < temp) {
best2 = temp;
}
}
ans = max(ans, best1 + best2);
return ans;
}
Compilation message (stderr)
dreaming.cpp: In function 'void computeBelow(int, int)':
dreaming.cpp:20:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i = 0;i < g[node].size();i++) {
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int computeAbove(int, int)':
dreaming.cpp:37:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for(int i = 0;i < g[node].size();i++) {
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp:48:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(int i = 0;i < g[node].size();i++) {
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:92:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int i = 0;i < len.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... |