이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int inf = 1e9+10;
const int mxn = 1e5+10;
vector<pii> paths[mxn];
int dist[mxn];
vector<pii> req[mxn],v;
int dsu[mxn],sz[mxn];
bitset<mxn> active;
int ans[mxn*5];
priority_queue<pii,vector<pii>,greater<pii>> pq;
int n,m;
void DIJKSTRA(){
while(!pq.empty()){
auto now = pq.top();
pq.pop();
if(now.fs != dist[now.sc])continue;
for(auto nxt:paths[now.sc]){
if(dist[nxt.fs]>dist[now.sc]+nxt.sc){
dist[nxt.fs] = now.fs+nxt.sc;
pq.push({dist[nxt.fs],nxt.fs});
}
}
}
return;
}
int root(int k){
return k == dsu[k]?k:dsu[k] = root(dsu[k]);
}
void onion(int a,int b,int c){
a = root(a),b = root(b);
if(a == b)return;
if(req[a].size()<req[b].size()){
swap(a,b);
}
while(!req[b].empty()){
auto tmp = req[b].back();
req[b].pop_back();
if(root(tmp.fs) == a){
if(ans[tmp.sc] == -1)ans[tmp.sc] = c;
}
else req[a].push_back(tmp);
}
if(sz[a]<sz[b]){
req[a].swap(req[b]);
swap(a,b);
}
dsu[b] = a;
sz[a] += sz[b];
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i = 0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
paths[a].push_back({b,c});
paths[b].push_back({a,c});
}
fill(dist,dist+n+1,inf);
int k;
cin>>k;
while(k--){
int tmp;
cin>>tmp;
pq.push({0,tmp});
dist[tmp] = 0;
}
DIJKSTRA();
for(int i =1;i<=n;i++)v.push_back({dist[i],i});
sort(v.rbegin(),v.rend());
for(int i = 1;i<=n;i++)dsu[i] = i,sz[i] = 1;
int q;
cin>>q;
fill(ans,ans+q+1,-1);
for(int i = 1;i<=q;i++){
int a,b;
cin>>a>>b;
req[a].push_back({b,i});
req[b].push_back({a,i});
}
for(auto &i:v){
active[i.sc] = true;
for(auto nxt:paths[i.sc]){
if(active[nxt.fs])onion(nxt.fs,i.sc,i.fs);
}
}
for(int i = 1;i<=q;i++)cout<<ans[i]<<'\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... |