#include <bits/stdc++.h>
using namespace std;
// THE ANSWER WAS THAT WE FIND THE REPRESENTATIVE FOR EACH COMPONENT
// THE LONGEST DISTANCE WILL EITHER BE C1 + C2 + L OR 2L + C2 + C3
// WHERE C[] ARE OUR COMPONENTS SORTED BY MIN MAX LENGTH TO LEAF (DESCENDING)
/* 1. PROCESS EACH COMPONENT {
STORE 2 MAX LENGTHS
KEEP AN ARRAY TO STORE THE PARENTS
POSSIBLE STORE MAX LENGTH IN PAIRS WHERE WE STORE THE LENGTH AND THE CHILD
}
*/
#define pi pair<int, int>
#define pb push_back
#define f first
#define s second
const int MAXN = 100010;
vector<pi> adj[MAXN];
bool vis[MAXN];
pi dist[MAXN][2];
pi par[MAXN];
vector<int> comp, totals;
const int INF = 1e7;
void dfs(int v, int p)
{
if (vis[v]) return;
comp.pb(v);
vis[v] = 1;
dist[v][0] = {-1, 0};
dist[v][1] = {-1, 0};
// we want to store the 2 longest distances from node v to a leaf
for (pi child : adj[v])
{
if (child.f == p) {
par[v] = {p, child.s};
continue;
}
dfs(child.f, v);
int prop = dist[child.f][0].s + child.s;
if (prop > dist[v][1].s)
{
if (prop >= dist[v][0].s)
{
// shift
pi temp = dist[v][0];
dist[v][0] = {child.f, prop};
dist[v][1] = temp;
} else {
// replace max2
dist[v][1] = {child.f, prop};
}
}
}
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
// returns the min max travel time
// init the adj list
for (int i = 0; i < M; i++)
{
adj[A[i]].pb({B[i], T[i]});
adj[B[i]].pb({A[i], T[i]});
}
// we can perform dfs on this for each component
for (int i = 0; i < N; i++)
{
if (!vis[i])
{
// perform DFS to find 2 Max Lengths
dfs(i, i);
// we now have all the downward max lengths.
// process each node in the component and query for the node with the min max dist
int imax = INF;
// cout << "starting node: " << i << "\n";
for (int j = 0; j < comp.size() ; j++)
{
int node = comp[j];
int len = dist[node][0].s;
int source = dist[node][0].f;
if (node != i)
{
// add parent distance
int d;
if (dist[par[node].f][0].f == node) {
d = dist[par[node].f][1].s + par[node].s;
} else {
d = dist[par[node].f][0].s + par[node].s;
}
if (d > dist[node][1].s)
{
if (d >= dist[node][0].s)
{
pi temp = dist[node][0];
dist[node][0] = {par[node].f, d};
dist[node][1] = temp;
} else {
dist[node][1] = {par[node].f, d};
}
}
len = dist[node][0].s;
}
imax = min(imax, len);
// print out the distances for each of our nodes
//cout << node << " : { " << dist[node][0].f << ", " << dist[node][0].s << "} {" << dist[node][1].f << "," << dist[node][1].s << "}\n";
}
totals.pb(imax);
comp.clear();
}
}
int val = totals.size();
sort(totals.begin(), totals.end(), greater<int>());
int ans;
//cout << "Totals size: " << to_string(totals.size()) << "\n";
//for (int i = 0; i < totals.size(); i++) cout << "t" << i << ": " << totals[i] << "\n";
if (totals.size() > 2) ans = max(L + totals[0] + totals[1], 2 * L + totals[1] + totals[2]);
else if (totals.size() == 2) ans = L + totals[0] + totals[1];
else ans = totals[0];
return ans;
}
void solve()
{
// take in input and call travelTime
int n, m, l;
cin >> n >> m >> l;
int A[m], B[m], T[m];
for (int i = 0; i < m; i++) cin >> A[i];
for (int i = 0; i < m; i++) cin >> B[i];
for (int i = 0; i < m; i++) cin >> T[i];
cout << travelTime(n, m, l, A, B, T) << "\n";
return;
}
int main()
{
auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
for(int i = 0; i < t; i++){
// cout << "Case #" << i + 1 << ": ";
solve();
}
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
// cout <<"Time taken : "<<duration.count() <<" milliseconds\n";
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:92:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = 0; j < comp.size() ; j++)
| ~~^~~~~~~~~~~~~
dreaming.cpp:97:21: warning: unused variable 'source' [-Wunused-variable]
97 | int source = dist[node][0].f;
| ^~~~~~
dreaming.cpp:137:9: warning: unused variable 'val' [-Wunused-variable]
137 | int val = totals.size();
| ^~~
/usr/bin/ld: /tmp/ccgQ3DTT.o: in function `main':
dreaming.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccVJCRSS.o:grader.c:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccVJCRSS.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status