/**
* author: AgentPengin ( Độc cô cầu bại )
* created: 23.12.2022 10:08:02
* too lazy to update time
**/
#include "dreaming.h"
#include<bits/stdc++.h>
#define EL '\n'
#define fi first
#define se second
#define NAME "TASK"
#define ll long long
#define lcm(a,b) (a/gcd(a,b))*b
#define db(val) "["#val" = " << (val) << "] "
#define bend(v) (v).begin(),(v).end()
#define sz(v) (int)(v).size()
#define ex exit(0)
#define int ll
using namespace std;
const ll mod = 1e9 + 7;
const int inf = 0x1FFFFFFF;
const int MAXN = 1e5 + 5;
struct Edges {
int u,v,c;
};
int n,m,l,f[MAXN],ans = 0;
vector<pair<int,int>> adj[MAXN];
bool visited[MAXN];
vector<int> root;
pair<int,int> res;
vector<pair<int,int>> centroid;
vector<Edges> edges;
void dfs(int u,int p = 0) {
visited[u] = true;
if (p > 0) {
pair<int,int> idx;
for (auto x : adj[u]) {
if (x.fi == p) {
idx = x;
break;
}
}
adj[u].erase(find(bend(adj[u]),idx));
}
for (auto x : adj[u]) {
int v = x.fi;
int c = x.se;
if (v == p) continue;
dfs(v,u);
f[u] = max(f[u],f[v] + c);
}
}
void solve(int u,int p,int depth) {
res = min(res,make_pair(max(depth,f[u]),u));
ans = max(ans,depth + f[u]);
// for (auto x : adj[u]) {
// int v = x.fi;
// int c = x.se;
//
// }
vector<int> prefix(sz(adj[u]),0),suffix(sz(adj[u]),0);
for (int i = 0;i < sz(adj[u]);i++) {
int v = adj[u][i].fi;
int c = adj[u][i].se;
if (i == 0) prefix[i] = f[v] + c;
else prefix[i] = max(prefix[i - 1],f[v] + c);
}
for (int i = sz(adj[u]) - 1;i >= 0;i--) {
int v = adj[u][i].fi;
int c = adj[u][i].se;
if (i == sz(adj[u]) - 1) suffix[i] = f[v] + c;
else suffix[i] = max(suffix[i + 1],f[v] + c);
}
for (int i = 0;i < sz(adj[u]);i++) {
int mx = 0;
int v = adj[u][i].fi;
int c = adj[u][i].se;
if (i > 0) mx = max(mx,prefix[i - 1]);
if (i < sz(adj[u]) - 1) mx = max(mx,suffix[i + 1]);
solve(v,u,max(depth,mx) + c);
}
}
pair<int,int> find_centroid(int Root) {
ans = max(ans,f[Root]);
res = make_pair(f[Root],Root);
solve(Root,0,0);
return res;
}
int travelTime(int N,int M,int L,int A[],int B[],int C[]) {
n = N; m = M; l = L;
for (int i = 0;i < m;i++) {
A[i]++,B[i]++;
adj[A[i]].push_back({B[i],C[i]});
adj[B[i]].push_back({A[i],C[i]});
edges.push_back({A[i],B[i],C[i]});
}
// solve()
for (int i = 1;i <= n;i++) {
if (!visited[i]) {
dfs(i,0);
root.push_back(i);
}
}
for (auto x : root) {
centroid.push_back(find_centroid(x));
}
sort(bend(centroid),greater<pair<int,int>>());
for (int i = 1;i <= n;i++) adj[i].clear();
for (int i = 1;i < sz(centroid);i++) {
edges.push_back({centroid[i].se,centroid[0].se,l});
}
for (auto x : edges) {
adj[x.u].push_back({x.v,x.c});
adj[x.v].push_back({x.u,x.c});
}
memset(visited,0,sizeof visited);
dfs(1,0);
pair<int,int> t = find_centroid(1);
return ans;
}
Compilation message
dreaming.cpp: In function 'long long int travelTime(long long int, long long int, long long int, long long int*, long long int*, long long int*)':
dreaming.cpp:129:19: warning: variable 't' set but not used [-Wunused-but-set-variable]
129 | pair<int,int> t = find_centroid(1);
| ^
/usr/bin/ld: /tmp/ccGIcy9r.o: in function `main':
grader.c:(.text.startup+0xd1): undefined reference to `travelTime'
collect2: error: ld returned 1 exit status