Submission #168715

#TimeUsernameProblemLanguageResultExecution timeMemory
168715beso123Evacuation plan (IZhO18_plan)C++14
100 / 100
3806 ms104396 KiB
#include <bits/stdc++.h>
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
int n,m,N,q,lg=17,timer=1,ans[100005];
map<pair<int,int>,int> mp;
vector<int> g[100005],d,used,p,Size,g1[100005],dp[100005],tin,tout;
multiset<pair<int,int> > s;
vector<pii> ed;
void MS(){
    for(int k=1;k<=n;k++){
        p[k]=k;
        Size[k]=d[k];
    }
}
int FS(int v){
    if(p[v]==v) return v;
    return p[v]=FS(p[v]);
}
void US(int a,int b){
    a=FS(a);
    b=FS(b);
    if(a!=b){
    g1[a].push_back(b);
    g1[b].push_back(a);
        p[b]=a;
        Size[a]=min(Size[a],Size[b]);
    }
}
void DFS(int v,int p){
    used[v]=1;
    dp[v][0]=p;
    for(int k=1;k<=lg;k++)
        dp[v][k]=dp[dp[v][k-1]][k-1];
        tin[v]=timer++;
    for(int k=0;k<g1[v].size();k++){
        int to=g1[v][k];
        if(used[to]==0)
            DFS(to,v);
    }
    tout[v]=timer++;
}
bool tree(int a,int b){
    if(tin[a]<=tin[b] && tout[a]>=tout[b])
        return true;
     return false;
}
int LCA(int a,int b){
    if(tree(a,b)) return a;
    if(tree(b,a)) return b;
    int t=a;
    for(int k=17;k>=0;k--){
        if(dp[t][k]!=0){
        int to=dp[t][k];
        if(!tree(to,b))
            t=to;
    }
}
    return dp[t][0];
}
main(){
cin>>n>>m;
for(int k=1;k<=m;k++){
    int a,b,c;
    cin>>a>>b>>c;
    mp[{a,b}]=c;
    mp[{b,a}]=c;
    g[a].push_back(b);
    g[b].push_back(a);
}
cin>>N;
d.resize(n+1,INT_MAX);
used.resize(n+1,0);
for(int k=1;k<=N;k++){
    int qal;
    cin>>qal;
    d[qal]=0;
    s.insert({0,qal});
}
while(!s.empty()){
   int v=s.begin()->second;
   used[v]=1;
   s.erase(s.begin());
   for(int k=0;k<g[v].size();k++){
    int to=g[v][k];
    if(used[to]==0 && d[to]>d[v]+mp[{v,to}]){
        d[to]=d[v]+mp[{to,v}];
        s.insert({d[to],to});
    }
   }
}
for(int k=1;k<=n;k++){
    ed.push_back({d[k],k});
}
sort(ed.begin(),ed.end());
reverse(ed.begin(),ed.end());
cin>>q;
Size.resize(n+1,0);
p.resize(n+1,0);
used.resize(0);
used.resize(n+1,0);
MS();
for(int k=0;k<ed.size();k++){
    int v=ed[k].y;
    used[v]=1;
    for(int k=0;k<g[v].size();k++)
        if(used[g[v][k]])
        US(v,g[v][k]);
}
used.resize(0);
used.resize(n+1,0);
tin.resize(n+1,0);
tout.resize(n+1,0);
for(int k=0;k<=n;k++)
    dp[k].resize(18,0);
DFS(ed[ed.size()-1].y,ed[ed.size()-1].y);
for(int k=0;k<q;k++){
    int a,b;
    cin>>a>>b;
    int lc=LCA(a,b);
   cout<<d[lc]<<endl;
}
return 0;
}

Compilation message (stderr)

plan.cpp: In function 'void DFS(int, int)':
plan.cpp:34:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
     for(int k=1;k<=lg;k++)
     ^~~
plan.cpp:36:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
         tin[v]=timer++;
         ^~~
plan.cpp:37:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int k=0;k<g1[v].size();k++){
                 ~^~~~~~~~~~~~~
plan.cpp: At global scope:
plan.cpp:62:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main(){
      ^
plan.cpp: In function 'int main()':
plan.cpp:85:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for(int k=0;k<g[v].size();k++){
                ~^~~~~~~~~~~~
plan.cpp:104:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 for(int k=0;k<ed.size();k++){
             ~^~~~~~~~~~
plan.cpp:107:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int k=0;k<g[v].size();k++)
                 ~^~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...