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>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 100005
typedef long long ll;
using namespace std;
int n,m;
vector<pair<int,pair<int,int> > > grane;
vector<pair<int,int> > stablo[MAXN],niz[MAXN];
int pre[MAXN][20];
int cale[MAXN],masa[MAXN],dub[MAXN];
int docal[MAXN],udalj[MAXN];
int koren(int p){
while(p!=cale[p])p=cale[p];
return p;
}
void spoji(int a,int b,int masa2){
a=koren(a);
b=koren(b);
if(a==b)return;
if(masa[a]<masa[b])swap(a,b);
//printf(" %d %d %d\n",a,b,masa2);
pre[b][0]=a;
docal[b]=masa2;
stablo[a].pb(mp(b,masa2));
masa[a]+=masa[b];
cale[b]=a;
}
void dfsS(int t,int o,int h){
// printf(" %d %d\n",t,h);
dub[t]=h;
for(auto x:stablo[t])
dfsS(x.F,t,h+1);
}
int resi(int a,int b){
//printf(" %d %d ",dub[a],dub[b]);
if(dub[b]>dub[a])swap(a,b);
for(int j=19;j>=0;j--)
if(pre[a][j]!=0 && dub[pre[a][j]]>dub[b])a=pre[a][j];
if(a==b)return udalj[a];
if(pre[a][0]==b)return docal[a];
//printf("NISAM GOTOV ");
if(dub[a]!=dub[b])a=pre[a][0];
for(int j=19;j>=0;j--)
if(pre[a][j]!=pre[b][j])a=pre[a][j],b=pre[b][j];
//printf(" %d %d ", a,b);
return min(docal[a],docal[b]);
}
set<pair<int,int> > q;
bool obidjen[MAXN];
void dijkstra(){
while(!q.empty()){
pair<int,int> t=*q.begin();
q.erase(q.begin());
if(obidjen[t.S])continue;
obidjen[t.S]=true;
udalj[t.S]=t.F;
for(auto x:niz[t.S]){
if(obidjen[x.F])continue;
q.insert(mp(x.S+t.F,x.F));
}
}
}
int main()
{
scanf("%d %d", &n, &m);
for(int i=1;i<=n;i++){
cale[i]=i;
masa[i]=1;
}
for(int i=1;i<=m;i++){
int t1,t2,t3;
scanf("%d %d %d", &t1, &t2, &t3);
niz[t1].pb(mp(t2,t3));
niz[t2].pb(mp(t1,t3));
grane.pb(mp(t3,mp(t1,t2)));
}
int k;
scanf("%d", &k);
while(k--){
int t;
scanf("%d", &t);
q.insert(mp(0,t));
//printf("gotov");
}
dijkstra();
for(auto it=grane.begin();it!=grane.end();it++)it->F=-min(udalj[it->S.F],udalj[it->S.S]);
sort(all(grane));
for(auto x:grane)
spoji(x.S.F,x.S.S,min(udalj[x.S.F],udalj[x.S.S]));
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++)
pre[i][j]=pre[pre[i][j-1]][j-1];
}
dfsS(koren(1),0,1);
int qq;
scanf("%d", &qq);
while(qq--){
int a,b;
scanf("%d %d", &a, &b);
printf("%d\n",resi(a,b));
}
return 0;
}
Compilation message (stderr)
plan.cpp: In function 'int main()':
plan.cpp:84:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
plan.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &t1, &t2, &t3);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:99:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &k);
~~~~~^~~~~~~~~~
plan.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &t);
~~~~~^~~~~~~~~~
plan.cpp:117:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &qq);
~~~~~^~~~~~~~~~~
plan.cpp:120:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# | 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... |