#include<bits/stdc++.h>
#include"dreaming.h"
using namespace std;
struct DSU {
int n;
vector<int> parent;
vector<int> size;
DSU(int n) : n(n), parent(n), size(n, 1) {
iota(parent.begin(), parent.end(), 0);
}
int get(int i) {
return parent[i] == i ? i : parent[i] = get(parent[i]);
}
void merge(int v, int u) {
v = get(v);
u = get(u);
if (u == v) return;
if (size[v] < size[u]) swap(v, u);
parent[u] = v;
size[v] += size[u];
}
};
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
DSU cc(n);
vector<vector<pair<int,int>>> aadj(n);
for (int i = 0; i < m;i ++) {
aadj[a[i]].push_back({b[i], t[i]});
aadj[b[i]].push_back({a[i], t[i]});
cc.merge(a[i], b[i]);
}
vector<vector<int>> comps(n);
for (int i = 0; i < n;i++) {
comps[cc.get(i)].push_back(i);
}
for (auto& i : comps)
sort(i.begin(), i.end());
vector<int> ccompress;
for (int i = 0; i < n;i++) {
if (cc.get(i) == i)
ccompress.push_back(i);
}
sort(ccompress.begin(), ccompress.end());
int k = ccompress.size();
vector<vector<vector<pair<int,int>>>> adj(k);
for (int i = 0; i < k; i++) {
adj[i].resize(comps[ccompress[i]].size());
}
for (int i = 0; i <m; i++) {
int u = a[i], v = b[i];
auto& xs = comps[cc.get(u)];
u = lower_bound(xs.begin(), xs.end(), u) - xs.begin();
v = lower_bound(xs.begin(), xs.end(), v) - xs.begin();
int tree_index = lower_bound(ccompress.begin(), ccompress.end(), cc.get(a[i])) - ccompress.begin();
adj[tree_index][v].push_back({u, t[i]});
adj[tree_index][u].push_back({v, t[i]});
}
vector<int> center(k);
vector<array<int, 2>> mxdist(k);
for (int tree = 0; tree < k; tree++) {
auto dfs = [&](
auto& dfs,
int t,
vector<int>& dist,
int i, int p=-1, int w=0)->void{
dist[i] = w;
for (auto [e, ew] : adj[t][i]) {
if (e == p) continue;
dfs(dfs, t, dist, e, i, w + ew);
}
};
int tn = adj[tree].size();
vector<int> dist(tn);
dfs(dfs, tree, dist, 0);
int s1 = max_element(dist.begin(), dist.end()) - dist.begin();
dfs(dfs, tree, dist, s1);
int s2 = max_element(dist.begin(), dist.end()) - dist.begin();
vector<int> dist2(tn);
dfs(dfs, tree, dist2, s2);
for (int i = 0; i < tn; i++)
dist[i] = max(dist[i], dist2[i]);
center[tree] = min_element(dist.begin(), dist.end()) - dist.begin();
dfs(dfs, tree, dist, center[tree]);
nth_element(dist.begin(), dist.begin() + 1, dist.end());
mxdist[tree] = {dist[0], dist[1]};
}
assert(k == 2);
return max({
mxdist[0][0] + mxdist[0][1],
mxdist[1][0] + mxdist[1][1],
mxdist[0][0] + mxdist[1][0],
});
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
84 ms |
19532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
84 ms |
19532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
49 ms |
34828 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
84 ms |
19532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |