#include <algorithm>
#include <bits/stdc++.h>
#include <cassert>
#include <functional>
#include <iomanip>
#include <queue>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define ff first
#define ss second
#define ln "\n"
#define mp make_pair
const ll INF = 2e18;
const ll MOD = 1e9+7;
struct DSU{
vector<ll> p;
vector<set<ll>> qs;
ll n;
DSU(ll N){
n=N; p.resize(n+1, -1);
qs.resize(n+1);
}
void add_query(ll u, ll i){
qs[u].insert(i);
}
ll get(ll u){
return p[u]==-1?u:p[u]=get(p[u]);
}
set<ll> unite(ll u, ll v){
u=get(u); v=get(v);
if (u==v) return set<ll>();
if (qs[u].size()<qs[v].size()) swap(u, v);
p[v]=u;
set<ll> ret;
for (auto i:qs[v]){
if (qs[u].count(i)){
qs[u].erase(i);
ret.insert(i);
}else{
qs[u].insert(i);
}
}
qs[v].clear();
return ret;
}
};
struct edge{
ll u, v, w;
};
ll n, m;
vector<vector<ll>> A;
vector<edge> e;
vector<ll> dist, bad;
void gendist(){
dist.assign(n+1, -1);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> que;
for (auto ch:bad){
que.push({0, ch});
}
while (!que.empty()){
auto cur = que.top(); que.pop();
if (dist[cur.ss]!=-1) continue;
dist[cur.ss]=cur.ff;
for (auto i:A[cur.ss]){
ll v = e[i].u^e[i].v^cur.ss;
if (dist[v]==-1) que.push({e[i].w+cur.ff, v});
}
}
}
void solve(){
cin >> n >> m;
A.assign(n+1, vector<ll>());
e.resize(m);
for (ll i=0; i<m; i++){
cin >> e[i].u >> e[i].v >> e[i].w;
A[e[i].u].push_back(i);
A[e[i].v].push_back(i);
}
ll k; cin >> k; bad.resize(k);
for (ll i=0; i<k; i++) cin >> bad[i];
gendist();
ll mxd = 0; for (auto ch:dist) mxd=max(mxd, ch);
vector<pair<ll, ll>> dst;
for (ll i=1; i<=n; i++){
dst.push_back({dist[i], i});
}
sort(dst.begin(), dst.end());
DSU tr(n);
ll q; cin >> q;
vector<ll> res(q);
for (ll i=0; i<q; i++){
ll u, v; cin >> u >> v;
tr.add_query(u, i);
tr.add_query(v, i);
}
vector<ll> usable(n+1);
ll cur = mxd+1;
while (!dst.empty()){
while (!dst.empty() and dst.back().ff>=cur){
ll proc = dst.back().ss;
dst.pop_back();
for (auto i:A[proc]){
ll v = e[i].u^e[i].v^proc;
if (usable[v]){
set<ll> comp = tr.unite(proc, v);
for (auto qs:comp){
res[qs]=cur;
}
}
}
usable[proc]=1;
}
cur--;
}
for (ll i=0; i<q; i++){
cout << res[i] << ln;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
auto start = chrono::high_resolution_clock::now();
ll t=1;
// cin >> t;
while (t--) solve();
#ifdef LOCAL
auto duration = chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - start);
cout << setprecision(0) << fixed << "time: " << (double)duration.count()/1000.0 << " milliseconds" << endl;
#endif
}
# | 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... |