이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
struct Disjoint_set {
vector<int> lab;
Disjoint_set(int n = 0) {
lab.assign(n + 7, -1);
}
int root(int u) {
return lab[u] < 0 ? u : lab[u] = root(lab[u]);
}
bool unite(int u, int v) {
u = root(u); v = root(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
lab[u] += lab[v];
lab[v] = u;
return true;
}
};
const int N = 5e5 + 7;
const int LOG = 20;
int n, m, num_npp;
int npp[N];
int ind[N];
int dist[N];
int depth[N];
int f[N][LOG + 1];
int par[N][LOG + 1];
bool flag[N];
vector<pair<int, int>> adj[N];
vector<int> g[N];
void shotest_path() {
priority_queue<pair<int, int>> heap;
memset(dist, 0x3f, sizeof dist);
for (int i = 1; i <= num_npp; i ++) {
heap.emplace(0, npp[i]);
dist[npp[i]] = 0;
}
while (!heap.empty()) {
int u, d_u; tie(d_u, u) = heap.top();
heap.pop();
d_u *= -1;
if (dist[u] != d_u) {
continue;
}
for (pair<int, int> v: adj[u]) if (minimize(dist[v.fi], d_u + v.se)) {
heap.emplace(-d_u - v.se, v.fi);
}
}
}
void dfs(int u, int p) {
par[u][0] = p;
f[u][0] = dist[u];
for (int i = 1; i <= LOG; i ++) {
par[u][i] = par[par[u][i - 1]][i - 1];
f[u][i] = min(f[u][i - 1], f[par[u][i - 1]][i - 1]);
}
for (int v: g[u]) if (v != p) {
depth[v] = depth[u] + 1;
dfs(v, u);
}
}
int lca(int u, int v) {
if (depth[v] > depth[u]) {
swap(u, v);
}
int dist = depth[u] - depth[v], res = (int) 1e9;
for (int i = LOG; i >= 0; i --) if ((dist >> i) & 1) {
minimize(res, f[u][i]);
u = par[u][i];
}
if (u == v) {
return min(res, f[u][0]);
}
for (int i = LOG; i >= 0; i --) if (par[u][i] != par[v][i]) {
minimize(res, f[u][i]);
minimize(res, f[v][i]);
u = par[u][i];
v = par[v][i];
}
int w = par[u][0];
minimize(res, f[w][0]);
minimize(res, f[u][0]);
return min(res, f[v][0]);
}
void build_tree() {
iota(ind + 1, ind + n + 1, 1);
sort(ind + 1, ind + n + 1, [&](int u, int v) {
return dist[u] > dist[v];
});
Disjoint_set dsu(n);
for (int i = 1; i <= n; i ++) {
int u = ind[i];
for (pair<int, int> v: adj[u]) {
if (flag[v.fi]) {
if (dsu.unite(u, v.fi)) {
g[u].push_back(v.fi);
g[v.fi].push_back(u);
}
}
}
flag[u] = true;
}
dfs(1, 1);
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i ++) {
int u, v, w; cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
cin >> num_npp;
for (int i = 1; i <= num_npp; i ++) {
cin >> npp[i];
}
shotest_path();
build_tree();
int q; cin >> q;
while (q --) {
int u, v; cin >> u >> v;
cout << lca(u, v) << '\n';
}
return 0;
}
# | 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... |