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 <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// #pragma GCC optimize("Ofast,unroll-loops,fast-math")
// #pragma GCC target("popcnt")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define pb push_back
const int mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353; // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const int N = ll(6e5)+100;
const long long inf = 2e18;
const long double eps = 1e-15L;
ll lcm(ll a, ll b) { return (a / __gcd(a,b))*b; }
ll binpow(ll a, ll b, ll m) { ll res=1; a %= m; while(b>0){ if(b&1)res=(res * a) % m; a=(a * a) % m; b/=2; } return res%m;}
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
int n, m, k, q, qu[N], qv[N], qw[N], par[N], qs[N], qt[N], siz[N], dis[N], bl[N], br[N];
vector<int> pos[N];
vector<int> check[N];
vector<pair<int, int> > g[N];
vector<int> nuc;
int findpar(int v) {
if(v == par[v]) return v;
return par[v] = findpar(par[v]);
}
void combine(int a, int b) {
a = findpar(a);
b = findpar(b);
if(a != b) {
if(siz[a] < siz[b]) swap(a, b);
par[b] = a;
siz[a] += siz[b];
}
}
void Baizho() {
cin>>n>>m;
for(int i = 1; i <= m; i ++) {
cin>>qu[i]>>qv[i]>>qw[i];
g[qu[i]].pb({qv[i], qw[i]});
g[qv[i]].pb({qu[i], qw[i]});
}
for(int i = 1; i <= n; i ++) {
dis[i] = 2e9;
}
cin>>k;
set<pair<int, int> > st;
for(int i = 1; i <= k; i ++) {
int x; cin>>x;
nuc.pb(x);
dis[x] = 0;
st.insert({dis[x], x});
}
while(!st.empty()) {
int v = (*st.begin()).ss;
st.erase(st.begin());
for(auto to : g[v]) {
if(dis[to.ff] > dis[v] + to.ss) {
if(dis[to.ff] != 2e9) st.erase({dis[to.ff], to.ff});
dis[to.ff] = dis[v] + to.ss;
st.insert({dis[to.ff], to.ff});
}
}
}
vector<int> comp;
map<int, int> rev;
for(int i = 1; i <= n; i ++) {
comp.pb(dis[i]);
}
sort(all(comp));
comp.erase(unique(all(comp)), comp.end());
for(int i = 1; i <= n; i ++) {
int orig = dis[i];
dis[i] = upper_bound(all(comp), dis[i]) - comp.begin();
rev[dis[i]] = orig;
// cout<<dis[i]<<" ";
}
// cout<<"\n";
for(int i = 1; i <= m; i ++) {
pos[min(dis[qu[i]], dis[qv[i]])].pb(i);
}
cin>>q;
for(int i = 1; i <= q; i ++) {
cin>>qs[i]>>qt[i];
bl[i] = 1, br[i] = comp.sz;
// int l = 1, r = comp.sz;
// while(l <= r) {
// int mid = (l + r) / 2;
// for(int j = 0; j <= n; j ++) {
// par[j] = j;
// siz[j] = 1;
// ok[j] = 0;
// }
// for(int j = 1; j <= n; j ++) {
// if(dis[j] >= mid) ok[j] = 1;
// }
// for(int j = 1; j <= m; j ++) {
// if(ok[qu[j]] && ok[qv[j]]) combine(qu[j], qv[j]);
// dis[qu[j]] >= mid && dis[qv[j]] >= mid
// min() >= mid
// }
// if(findpar(qs[i]) == findpar(qt[i])) l = mid + 1;
// else r = mid - 1;
// }
// cout<<rev[l - 1]<<"\n";
}
for(int bin = 0; bin <= 35; bin ++) {
for(int j = 0; j <= n; j ++) {
par[j] = j;
siz[j] = 1;
check[j].clear();
}
for(int i = 1; i <= q; i ++) {
if(bl[i] <= br[i]) {
check[(bl[i] + br[i]) / 2].pb(i);
// cout<<i<<" "<<bl[i]<<br[i]<<"OLOY \n";
}
}
for(int i = comp.sz; i >= 1; i --) {
for(auto j : pos[i]) {
combine(qu[j], qv[j]);
// cout<<j<<" "<<i<<" "<<qu[j]<<" "<<qv[j]<<"\n";
}
for(auto j : check[i]) {
if(findpar(qs[j]) == findpar(qt[j])) {
bl[j] = i + 1;
// cout<<j<<" "<<i<<"\n";
}
else br[j] = i - 1;
}
}
}
for(int i = 1; i <= q; i ++) cout<<rev[bl[i] - 1]<<"\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// precalc();
int ttt = 1;
// cin>>ttt;
//yo
for(int i=1; i<=ttt; i++) {Baizho(); }
}
Compilation message (stderr)
plan.cpp: In function 'void Freopen(std::string)':
plan.cpp:35:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:35:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |