#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
#define DIM 100010
#define INF 2000000000
#include "dreaming.h"
using namespace std;
ifstream fin ("dreaming.in");
ofstream fout ("dreaming.out");
int nr,n,m,l,i;
int dp_down[DIM]; /// cel mai lung drum in subarborele lui i
int dp_up[DIM]; /// cel mai lung drum din i in afara subarborelui lui i
int s[DIM],viz[DIM],v[DIM],a[DIM],b[DIM],cost[DIM];
vector <pair<int,int> > L[DIM];
vector <int> comp[DIM];
void dfs (int nod, int tata){
viz[nod] = 1;
comp[nr].push_back(nod);
for (int i=0;i<L[nod].size();i++){
int vecin = L[nod][i].first;
if (vecin != tata){
dfs (vecin,nod);
s[nod] = max (s[nod],s[vecin]+L[nod][i].second);
}}
int maxim = 0, maxim2 = 0;
for (int i=0;i<L[nod].size();i++){
int vecin = L[nod][i].first, cost = L[nod][i].second;
if (vecin == tata)
continue;
if (s[vecin]+cost > maxim){
maxim2 = maxim;
maxim = s[vecin]+cost;
} else {
if (s[vecin]+cost > maxim2)
maxim2 = s[vecin]+cost;
}}
dp_down[nod] = maxim+maxim2;
}
void dfs2 (int nod, int tata, int cost){
dp_up[nod] = max (dp_up[nod],dp_up[tata]+cost);
int maxim = 0, maxim2 = 0, p = 0;
for (int i=0;i<L[nod].size();i++){
int fiu = L[nod][i].first, cst = L[nod][i].second;
if (fiu == tata)
continue;
if (s[fiu]+cst > maxim){
maxim2 = maxim;
maxim = s[fiu]+cst, p = fiu;
} else
if (s[fiu]+cst > maxim2)
maxim2 = s[fiu]+cst;
}
for (int i=0;i<L[nod].size();i++){
int fiu = L[nod][i].first;
if (fiu == tata)
continue;
if (fiu == p)
dp_up[fiu] = max (dp_up[fiu],maxim2+L[nod][i].second);
else dp_up[fiu] = max (dp_up[fiu],maxim+L[nod][i].second);
}
for (int i=0;i<L[nod].size();i++){
int vecin = L[nod][i].first;
if (vecin != tata)
dfs2 (vecin,nod,L[nod][i].second);
}}
int travelTime (int n, int m, int l, int a[], int b[], int cost[]){
for(int i = 0; i <= n; i++){
v[i].clear();
}
for(int i = 0; i < m; i++){
int x = a[i], y = b[i], z = cost[i];
x++, y++;
v[x].push_back({y, z});
v[y].push_back({x, z});
}
memset(viz,0,sizeof(viz));
memset(dp_up,0,sizeof(dp_up));
memset(dp_down,0,sizeof(dp_down));
memset(s,0,sizeof(s));
for(int i = 1; i <= n; i++){
if(viz[i])continue;
nr++;
dfs(i, 0);
dfs2(i, 0, 0);
}
int ans = 0;
for(int i = 1;i <= nr; i++){
int mn = inf;
for(auto nod : c[i]){
int dist_max = max(dp_up[nod] + s[nod], dp_down[nod]);
ans = max(ans, dist_max);
int dist = max(s[nod], dp_up[nod]);
mn = min(mn,dist);
}
path[i] = mn;
}
sort(path + 1, path + nr + 1);
if(nr >= 2)ans = max(ans, path[nr] + path[nr - 1] + l);
if(nr >= 3)ans = max(ans, path[nr - 1] + path[nr - 2] + 2*l);
return ans;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:20:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for (int i=0;i<L[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int i=0;i<L[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int, int)':
dreaming.cpp:44:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i=0;i<L[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i=0;i<L[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp:64:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | for (int i=0;i<L[nod].size();i++){
| ~^~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:73:14: error: request for member 'clear' in 'v[i]', which is of non-class type 'int'
73 | v[i].clear();
| ^~~~~
dreaming.cpp:79:14: error: request for member 'push_back' in 'v[x]', which is of non-class type 'int'
79 | v[x].push_back({y, z});
| ^~~~~~~~~
dreaming.cpp:80:14: error: request for member 'push_back' in 'v[y]', which is of non-class type 'int'
80 | v[y].push_back({x, z});
| ^~~~~~~~~
dreaming.cpp:98:18: error: 'inf' was not declared in this scope; did you mean 'int'?
98 | int mn = inf;
| ^~~
| int
dreaming.cpp:99:24: error: 'c' was not declared in this scope
99 | for(auto nod : c[i]){
| ^
dreaming.cpp:105:9: error: 'path' was not declared in this scope
105 | path[i] = mn;
| ^~~~
dreaming.cpp:108:10: error: 'path' was not declared in this scope
108 | sort(path + 1, path + nr + 1);
| ^~~~