/*
ID: varunra2
LANG: C++
TASK: dreaming
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
vector<VII> adj;
vector<bool> vis;
int n, m, ext;
void init() {
adj.resize(n);
vis.assign(n, false);
}
void dfscomps(int u, VI& cmp) {
if (vis[u]) return;
vis[u] = true;
cmp.PB(u);
for (auto& x : adj[u]) {
dfscomps(x.x, cmp);
}
}
struct comp {
VI vals;
int n;
void init(int _n, VI& _vals) {
n = _n;
vals = _vals;
}
// we need to get the diameters
MPII d[3];
void dfsDist(int u, int v, int ind) {
for (auto& x : adj[u]) {
if (x.x == v) continue;
d[ind][x.x] = d[ind][u] + x.y;
dfsDist(x.x, u, ind);
}
}
int getFarthest(int u, int ind) {
dfsDist(u, -1, ind);
int ret = u;
for (auto& x : vals) {
if (d[ind][x] > d[ind][ret]) ret = x;
}
return ret;
}
int diam;
int end1, end2;
PII calc() {
end1 = getFarthest(vals[0], 0);
end2 = getFarthest(end1, 1);
diam = d[1][end2];
getFarthest(end2, 2);
int ret = 2 * INF;
int ind = vals[0];
for (auto& x : vals) {
if (ret > max(d[1][x], d[2][x])) {
ret = max(d[1][x], d[2][x]);
ind = x;
}
}
return MP(ret, ind);
}
int getDiameter() { return diam; }
void deb() {
debug("debugging a comp");
debug(vals);
debug(diam);
debug(end1, end2);
debug(d[0], "from vals[0");
debug(d[1], "from end1");
debug(d[2], "from end2");
debug("doneeeeeeeeeeeeeeeeeeeee");
}
};
vector<comp> comps;
void genComps() {
for (int i = 0; i < n; i++) {
if (vis[i]) continue;
VI cur;
dfscomps(i, cur);
if (cur.empty()) continue;
// debug(cur);
comp psh;
psh.init(sz(cur), cur);
comps.PB(psh);
}
}
int travelTime(int N, int M, int L, int* A, int* B, int* T) {
n = N;
m = M;
ext = l;
init();
for (int i = 0; i < m; i++) {
int u, v, w;
u = a[i];
v = b[i];
w = t[i];
adj[u].PB(MP(v, w));
adj[v].PB(MP(u, w));
}
genComps();
VII dists;
for (int i = 0; i < sz(comps); i++) {
dists.PB(comps[i].calc());
}
// comps[0].deb();
sort(all(dists), greater<PII>());
// debug(dists);
for (int i = 1; i < sz(dists); i++) {
int u, v;
u = dists[0].y;
v = dists[i].y;
adj[u].PB(MP(v, ext));
adj[v].PB(MP(u, ext));
}
VI nodes(n);
iota(all(nodes), 0);
comp tot;
tot.init(n, nodes);
tot.calc();
return tot.getDiameter();
}
// int main() {
// #ifndef ONLINE_JUDGE
// freopen("dreaming.in", "r", stdin);
// freopen("dreaming.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0);
// cin.tie(0);
// 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] >> b[i] >> t[i];
// }
// cout << travelTime(n, m, l, a, b, t) << '\n';
// return 0;
// }
Compilation message
dreaming.cpp: In member function 'void comp::deb()':
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:119:5: note: in expansion of macro 'debug'
119 | debug("debugging a comp");
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:120:5: note: in expansion of macro 'debug'
120 | debug(vals);
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:121:5: note: in expansion of macro 'debug'
121 | debug(diam);
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:122:5: note: in expansion of macro 'debug'
122 | debug(end1, end2);
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:123:5: note: in expansion of macro 'debug'
123 | debug(d[0], "from vals[0");
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:124:5: note: in expansion of macro 'debug'
124 | debug(d[1], "from end1");
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:125:5: note: in expansion of macro 'debug'
125 | debug(d[2], "from end2");
| ^~~~~
dreaming.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
dreaming.cpp:126:5: note: in expansion of macro 'debug'
126 | debug("doneeeeeeeeeeeeeeeeeeeee");
| ^~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:148:9: error: 'l' was not declared in this scope
148 | ext = l;
| ^
dreaming.cpp:152:9: error: 'a' was not declared in this scope
152 | u = a[i];
| ^
dreaming.cpp:153:9: error: 'b' was not declared in this scope
153 | v = b[i];
| ^
dreaming.cpp:154:9: error: 't' was not declared in this scope
154 | w = t[i];
| ^