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 ;
const int MAX = 1e5 + 10 ;
int arr[MAX] ;
int n , m , k , q ;
vector< vector< pair<int , int> > >adj(MAX) ;
vector< pair<int , int> >queries[MAX] ;
int dist[MAX] ;
void dijkstra()
{
for(int i = 1 ; i <= n ; ++i)
dist[i] = 1e9 ;
priority_queue< pair<int , int> , vector< pair<int , int> > , greater< pair<int , int> > >q ;
for(int i = 0 ; i < k ; ++i)
dist[arr[i]] = 0 , q.push({0 , arr[i]}) ;
while(!q.empty())
{
pair<int , int>p = q.top() ;
q.pop() ;
int node = p.second , cost = p.first ;
if(dist[node] < cost)
continue ;
for(auto &child : adj[node])
{
int child2 = child.first ;
int cost2 = cost + child.second ;
if(cost2 < dist[child2])
{
dist[child2] = cost2 ;
q.push({cost2 , child2}) ;
}
}
}
}
vector<int>v[MAX] ;
int par[MAX] , sz[MAX] , ans[MAX] ;
void init()
{
for(int i = 1 ; i <= n ; ++i)
par[i] = i , sz[i] = 1 , v[i].push_back(i) ;
}
int root(int node)
{
if(par[node] == node)
return node ;
return (par[node] = root(par[node])) ;
}
void Union(int x , int y , int z)
{
int a = root(x) ;
int b = root(y) ;
if(a == b)
return ;
if(sz[a] < sz[b])
swap(a , b) ;
while(v[b].size())
{
int node = v[b].back() ;
for(auto &p : queries[node])
{
int node2 = p.first , idx = p.second ;
if(root(node2) == a)
ans[idx] = z ;
}
v[a].push_back(node) ;
v[b].pop_back() ;
}
par[b] = a ;
sz[a] += sz[b] ;
}
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n>>m ;
for(int i = 0 ; i < m ; ++i)
{
int x , y , z ;
cin>>x>>y>>z ;
adj[x].emplace_back(y , z) ;
adj[y].emplace_back(x , z) ;
}
cin>>k ;
for(int i = 0 ; i < k ; ++i)
cin>>arr[i] ;
cin>>q ;
for(int i = 0 ; i < q ; ++i)
{
int x , y ;
cin>>x>>y ;
queries[x].emplace_back(y , i) ;
queries[y].emplace_back(x , i) ;
}
dijkstra() ;
vector< array<int , 3> >edges ;
for(int i = 1 ; i <= n ; ++i)
{
for(auto &child : adj[i])
{
if(child.first > i)
edges.push_back({min(dist[i] , dist[child.first]) , i , child.first}) ;
}
}
sort(edges.begin() , edges.end()) ;
reverse(edges.begin() , edges.end()) ;
init() ;
for(auto &a : edges)
{
int x = a[1] , y = a[2] , z = a[0] ;
Union(x , y , z) ;
}
for(int i = 0 ; 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... |