Submission #378631

#TimeUsernameProblemLanguageResultExecution timeMemory
378631daniel920712Evacuation plan (IZhO18_plan)C++14
100 / 100
1401 ms69304 KiB
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>

using namespace std;
vector < pair < int , int > > Next[200005];
vector < pair < int , int > > Next2[200005];
pair < int , int > Father[30][200005];
int deg[200005];
int root[200005];
vector < pair < int , pair < int , int > > > road;
priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > dij;
priority_queue < pair < int , int > , vector < pair < int , int > > , less < pair < int , int > > > dij2;
int small[200005];
//int deg[200005];
bool have[200005]={0};
bool cmp(pair < int , pair < int , int > > a,pair < int , pair < int , int > > b)
{
    return a>b;
}
int Find(int here)
{
    if(root[here]==here) return here;
    root[here]=Find(root[here]);
    return root[here];
}
void F(int here,int fa,int deg)
{
    int j;
    ::deg[here]=deg;
    for(auto i:Next2[here])
    {

        if(i.first==fa) continue;
        Father[0][i.first]=make_pair(here,i.second);
        for(j=1;j<25;j++)
        {
            Father[j][i.first].first=Father[j-1][Father[j-1][i.first].first].first;
            Father[j][i.first].second=min(Father[j-1][Father[j-1][i.first].first].second,Father[j-1][i.first].second);
        }
        F(i.first,here,deg+1);
    }
}
int LCA(int a,int b)
{
    int j;
    if(a==b) return 1e9;
    if(deg[a]==deg[b])
    {
        if(Father[0][a].first==Father[0][b].first) return min(Father[0][a].second,Father[0][b].second);
        else
        {
            for(j=20;j>=0;j--)  if(Father[j][a].first!=Father[j][b].first) return min(min(Father[j][a].second,Father[j][b].second),LCA(Father[j][a].first,Father[j][b].first));
        }
    }
    else
    {
        if(deg[a]<deg[b]) swap(a,b);
        for(j=20;j>=0;j--)  if(deg[a]-(1<<j)>=deg[b]) return min(Father[j][a].second,LCA(Father[j][a].first,b));

    }
}
int main()
{
    int N,M,K,Q,i,j,k,a,b,c,d,t;
    scanf("%d %d",&N,&M);
    for(i=0;i<M;i++)
    {
        scanf("%d %d %d",&a,&b,&c);
        Next[a].push_back(make_pair(b,c));
        Next[b].push_back(make_pair(a,c));
    }
    scanf("%d",&K);
    while(K--)
    {
        scanf("%d",&t);
        dij.push(make_pair(0,t));
    }
    while(!dij.empty())
    {
        a=dij.top().second;
        b=dij.top().first;
        dij.pop();
        if(have[a]) continue;
        //printf("%d %d\n",a,b);
        have[a]=1;
        small[a]=b;
        for(auto i:Next[a]) dij.push(make_pair(b+i.second,i.first));
    }
    for(i=1;i<=N;i++) for(auto j:Next[i]) road.push_back(make_pair(min(small[i],small[j.first]),make_pair(i,j.first)));
    sort(road.begin(),road.end(),cmp);
    for(i=0;i<=N;i++)
    {
        root[i]=i;
        for(j=0;j<25;j++) Father[j][i]=make_pair(0,1e9);
    }
    for(auto i:road)
    {
        if(Find(i.second.first)!=Find(i.second.second))
        {
            Next2[i.second.first].push_back(make_pair(i.second.second,i.first));
            Next2[i.second.second].push_back(make_pair(i.second.first,i.first));
            root[Find(i.second.first)]=Find(i.second.second);
        }
    }
    F(1,-1,0);

    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d %d",&c,&d);

        printf("%d\n",LCA(c,d));
    }
    return 0;
}
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:69:21: warning: unused variable 'k' [-Wunused-variable]
   69 |     int N,M,K,Q,i,j,k,a,b,c,d,t;
      |                     ^
plan.cpp: In function 'int LCA(int, int)':
plan.cpp:66:1: warning: control reaches end of non-void function [-Wreturn-type]
   66 | }
      | ^
plan.cpp: In function 'int main()':
plan.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   70 |     scanf("%d %d",&N,&M);
      |     ~~~~~^~~~~~~~~~~~~~~
plan.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   73 |         scanf("%d %d %d",&a,&b,&c);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~
plan.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   77 |     scanf("%d",&K);
      |     ~~~~~^~~~~~~~~
plan.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   80 |         scanf("%d",&t);
      |         ~~~~~^~~~~~~~~
plan.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  112 |     scanf("%d",&Q);
      |     ~~~~~^~~~~~~~~
plan.cpp:115:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  115 |         scanf("%d %d",&c,&d);
      |         ~~~~~^~~~~~~~~~~~~~~
#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...