This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
struct dsu {
void resize(int n) {
rt.resize(n);
sz.assign(n, 1);
for (int i = 0; i < n; ++i)
rt[i] = i;
}
int gst(int i) {
if (rt[i] == i)
return i;
return rt[i] = gst(rt[i]);
}
void unite(int i, int j) {
i = gst(i);
j = gst(j);
if (i == j)
return;
if (sz[i] < sz[j])
swap(i, j);
sz[i] += sz[j];
rt[j] = i;
}
private:
vector<int> rt;
vector<int> sz;
};
const int MAXN = 1e5 + 123;
const int INF = 1e9 + 177013;
vector<pair<int, int>> edgw[MAXN];
vector<int> edg[MAXN];
int depth[MAXN];
void get_depth(int n) {
int k;
cin >> k;
vector<int> sts(k);
for (int i = 0; i < k; ++i)
cin >> sts[i], --sts[i];
set<pair<int, int>> rdepth;
for (int i = 0; i < n; ++i)
depth[i] = INF;
for (auto i : sts) {
depth[i] = 0;
rdepth.insert({depth[i], i});
}
while (rdepth.size()) {
auto [d, v] = *rdepth.begin();
rdepth.erase(rdepth.begin());
if (depth[v] < d)
continue;
for (auto [u, w] : edgw[v]) {
if (depth[u] > d + w) {
depth[u] = d + w;
rdepth.insert({depth[u], u});
}
}
}
}
signed main() {
int n, m;
cin >> n >> m;
for (int t = 0; t < m; ++t) {
int u, v, w;
cin >> u >> v >> w;
--u, --v;
edgw[u].push_back({v, w});
edgw[v].push_back({u, w});
edg[u].push_back(v);
edg[v].push_back(u);
}
get_depth(n);
//for (int i = 0; i < n; ++i)
//cout << depth[i] << ' ';
//cout << endl;
vector<pair<int, int>> rdepth(n);
for (int i = 0; i < n; ++i)
rdepth[i] = {depth[i], i};
sort(rdepth.rbegin(), rdepth.rend());
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
--s, --t;
dsu su;
su.resize(n);
for (auto [w, v] : rdepth) {
for (auto u : edg[v]) {
if (depth[u] >= w)
su.unite(v, u);
}
if (su.gst(s) == su.gst(t)) {
cout << min({w, depth[s], depth[t]}) << '\n';
break;
}
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |