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 MAXN = 1e5 + 10;
int marc[MAXN], dist[MAXN], longDist[2][MAXN];
int aux;
vector<int> ar[MAXN];
vector<int> wg[MAXN];
vector<pair<int, int> > group;
int dfs1(int cur);
int dfs2(int cur);
pair<int, int> dfs3(int cur);
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++){
ar[A[i]].push_back(B[i]);
ar[B[i]].push_back(A[i]);
wg[A[i]].push_back(T[i]);
wg[B[i]].push_back(T[i]);
}
int ret = 0;
for(int i = 0; i < N; i++){
if(marc[i] == 3) continue;
dist[i] = 0;
aux = 0;
int side1 = dfs1(i);
longDist[0][side1] = 0;
int side2 = dfs2(side1);
// ret = max(ret, longDist[0][side2]);
// printf("group: %d ends = %d and %d with %d nodes\n", group.size(), side1, side2, aux);
longDist[1][side2] = 0;
group.push_back(dfs3(side2));
}
// printf("%d\n", group.size());
// for(int i = 0; i < group.size(); i++)
// printf("%d ", group[i]);
// printf("\n");
for(int i = 1; i < group.size(); i++){
ar[group[i].second].push_back(group[i - 1].second);
ar[group[i - 1].second].push_back(group[i].second);
wg[group[i].second].push_back(L);
wg[group[i - 1].second].push_back(L);
if(max(group[i].first, group[i - 1].first + L) < max(group[i - 1].first, group[i].first + L)){
group[i].first = max(group[i].first, group[i - 1].first + L);
group[i].second = group[i].first;
}
else{
group[i].first = max(group[i - 1].first, group[i].first + L);
group[i].second = group[i - 1].first;
}
}
int side1 = dfs1(0);
longDist[0][side1] = 0;
int side2 = dfs2(side1);
// ret = max(ret, longDist[0][side2]);
// printf("group: %d ends = %d and %d with %d nodes\n", group.size(), side1, side2, aux);
return longDist[0][side2];
}
int dfs1(int cur){
marc[cur] = 1;
aux++;
int ret = cur;
int auxRet = dist[cur];
for(int i = 0; i < ar[cur].size(); i++){
int viz = ar[cur][i];
int len = wg[cur][i];
if(marc[viz] == 1) continue;
dist[viz] = dist[cur] + len;
int dfsVal = dfs1(viz);
if(dist[dfsVal] > auxRet) ret = dfsVal, auxRet = dist[dfsVal];
}
return ret;
}
int dfs2(int cur){
marc[cur] = 2;
int ret = cur;
int auxRet = longDist[0][cur];
for(int i = 0; i < ar[cur].size(); i++){
int viz = ar[cur][i];
int len = wg[cur][i];
if(marc[viz] == 2) continue;
longDist[0][viz] = longDist[0][cur] + len;
int dfsVal = dfs2(viz);
if(longDist[0][dfsVal] > auxRet) ret = dfsVal, auxRet = longDist[0][dfsVal];
}
return ret;
}
pair<int, int> dfs3(int cur){
marc[cur] = 3;
int ret = cur;
int auxRet = max(longDist[0][cur], longDist[1][cur]);
for(int i = 0; i < ar[cur].size(); i++){
int viz = ar[cur][i];
int len = wg[cur][i];
if(marc[viz] == 3) continue;
longDist[1][viz] = longDist[1][cur] + len;
pair<int, int> dfsVal = dfs3(viz);
if(dfsVal.first < auxRet) ret = dfsVal.second, auxRet = dfsVal.first;
}
// printf("%d max(%d %d) -> %d\n", cur, longDist[0][cur], longDist[1][cur], ret);
return make_pair(auxRet, ret);
}
Compilation message (stderr)
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:42:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 1; i < group.size(); i++){
~~^~~~~~~~~~~~~~
dreaming.cpp:25:6: warning: unused variable 'ret' [-Wunused-variable]
int ret = 0;
^~~
dreaming.cpp: In function 'int dfs1(int)':
dreaming.cpp:69:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ar[cur].size(); i++){
~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int dfs2(int)':
dreaming.cpp:84:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ar[cur].size(); i++){
~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'std::pair<int, int> dfs3(int)':
dreaming.cpp:99:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < ar[cur].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... |