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 <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef pair <int,int> ii;
vector <ii> adj[100010];
int ans = 1e9, p[100010], endpoint1, endpoint2, ep3, ep4, cen1, cen2, mx[10];
bool mk[100010];
int pf1[100010], pf2[100010];
int dfs(int v, int par, int len, int& mx, int& mxver)
{
mk[v] = true;
if(len > mx)
{
mx = len;
mxver = v;
}
for(int i = 0; i < adj[v].size(); i++)
{
int u = adj[v][i].first;
if(u != par)
dfs(u,v,len+adj[v][i].second,mx,mxver);
}
return mx;
}
int recoverdiam(int v, int par, int dest, int len)
{
p[v] = par;
if(v == dest)
return len;
for(int i = 0; i < adj[v].size(); i++)
{
if(adj[v][i].first != par)
recoverdiam(adj[v][i].first,v,dest, len + adj[v][i].second);
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0;i < M; i++)
{
int v = A[i], u = B[i], len = T[i];
adj[v].pb(ii(u,len));
adj[u].pb(ii(v,len));
}
vector <int> diam1, diam2;
dfs(0,0,0,mx[0],endpoint1);
dfs(endpoint1,endpoint1,0,mx[1],endpoint2);
recoverdiam(endpoint1,endpoint1,endpoint2,0);
int ver= endpoint2, par = p[endpoint2];
while(ver != endpoint1)
{
diam1.pb(ver);
ver = par;
par = p[ver];
}
diam1.pb(endpoint1);
for(int i = 0; i < N; i++)
{
if(!mk[i])
{
dfs(i,i,0,mx[2],ep3);
dfs(ep3,ep3,0,mx[3],ep4);
recoverdiam(ep3,ep3,ep4,0);
int ver= ep4, par = p[ep4];
while(ver != ep3)
{
diam2.pb(ver);
ver = par;
par = p[ver];
}
diam2.pb(ep3);
break;
}
}
for(int i = 0; i < diam1.size() - 1; i++)
for(int j = 0; j < adj[diam1[i]].size(); j++)
if(adj[diam1[i]][j].first == diam1[i+1])
pf1[i+1] = pf1[i] + adj[diam1[i]][j].second;
for(int i = 0; i < diam2.size()-1; i++)
for(int j= 0;j < adj[diam2[i]].size(); j++)
if(adj[diam2[i]][j].first == diam2[i+1])
pf2[i+1] = pf2[i] + adj[diam2[i]][j].second;
int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver;
for(int i = 0; i < diam1.size(); i++)
if(abs(pf1[diam1.size()-1] - 2*pf1[i]) < mn1)
{
mn1 = abs(pf1[diam1.size()-1] - 2*pf1[i]);
mn1ver = diam1[i];
}
for(int i = 0; i < diam2.size();i++)
if(abs(pf2[diam2.size()-1] - 2*pf2[i]) < mn2)
{
mn2 = abs(pf2[diam2.size()-1] - 2*pf2[i]);
mn2ver = diam2[i];
}
//----------------------------------------
adj[mn1ver].pb(ii(mn2ver,L));
adj[mn2ver].pb(ii(mn1ver,L));
int final1, final2;
dfs(0,0,0,mx[4],final1);
return dfs(final1,final1,0,mx[5],final2);
}
Compilation message (stderr)
dreaming.cpp: In function 'int dfs(int, int, int, int&, int&)':
dreaming.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[v].size(); i++)
~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'int recoverdiam(int, int, int, int)':
dreaming.cpp:34:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[v].size(); i++)
~~^~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:84:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < diam1.size() - 1; i++)
~~^~~~~~~~~~~~~~~~~~
dreaming.cpp:85:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 0; j < adj[diam1[i]].size(); j++)
~~^~~~~~~~~~~~~~~~~~~~~~
dreaming.cpp:89:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < diam2.size()-1; i++)
~~^~~~~~~~~~~~~~~~
dreaming.cpp:90:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j= 0;j < adj[diam2[i]].size(); j++)
~~^~~~~~~~~~~~~~~~~~~~~~
dreaming.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < diam1.size(); i++)
~~^~~~~~~~~~~~~~
dreaming.cpp:102:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < diam2.size();i++)
~~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int recoverdiam(int, int, int, int)':
dreaming.cpp:39:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:94:37: warning: 'mn2ver' may be used uninitialized in this function [-Wmaybe-uninitialized]
int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver;
^~~~~~
dreaming.cpp:94:29: warning: 'mn1ver' may be used uninitialized in this function [-Wmaybe-uninitialized]
int mn1 = 1e9, mn2 = 1e9, mn1ver, mn2ver;
^~~~~~
# | 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... |