제출 #392408

#제출 시각아이디문제언어결과실행 시간메모리
392408Hazem경주 (Race) (IOI11_race)C++14
100 / 100
852 ms109672 KiB
#include <bits/stdc++.h>
//#include "race.h"
//#include "grader.h"

using namespace std;
 
#define S second
#define F first
#define LL long long 
 
const int N1 = 2e5+10;
const LL MOD = 1e9+7;
const LL LINF = 1e18;
const LL INF = 1e9;

map<LL,int> *mp[N1];
vector<pair<int,LL>>adj[N1];
LL depth[N1],dis[N1],sizes[N1],in[N1],out[N1],ans[N1];
vector<int>vec;

LL get_mn(LL x,int i){

    if((*mp[i]).find(x)!=(*mp[i]).end())
        return (*mp[i])[x];
    else 
        return LINF;
}

void update(LL x,LL val,int i){

    if((*mp[i]).find(x)!=(*mp[i]).end())
        (*mp[i])[x] = min(1ll*(*mp[i])[x],val);
    else 
        (*mp[i])[x] = val;
}

map<LL,int>mp1;

void dfs(int i,int pr,int k){

    in[i] = vec.size();
    vec.push_back(i);

    mp[i] = new map<LL,int>;

    sizes[i] = 1;
    int heavy = -1;
    for(auto x:adj[i]){
        if(x.F==pr)continue;
        
        depth[x.F] = depth[i]+1;
        dis[x.F] = dis[i]+x.S;
        
        dfs(x.F,i,k);
        
        sizes[i] += sizes[x.F];
        if(heavy==-1||sizes[x.F]>sizes[heavy])
            heavy = x.F;
    }
    
    if(heavy==-1)heavy = i;
    mp[i] = mp[heavy];

    out[i] = vec.size();
    vec.push_back(i);

    ans[i] = get_mn(k+dis[i],i)-depth[i];
    for(auto x:adj[i]){

        int u = x.F;
        if(u==pr||u==heavy)continue;
        
        for(int j=in[u];j<=out[u];j++){
            
            int v = vec[j];
            ans[i] = min(ans[i],depth[v]+get_mn(k-dis[v]+dis[i]*2,i)-depth[i]*2);
            
            if(dis[v]-dis[i]==k)
                ans[i] = min(ans[i],depth[v]-depth[i]);
        }

        for(int j=in[u];j<=out[u];j++)
            update(dis[vec[j]],depth[vec[j]],i);
    }
    update(dis[i],depth[i],i);
}

int best_path(int N, int K, int H[][2], int L[])
{
    int n = N;
    for(int i=0;i<n-1;i++){
        adj[H[i][0]].push_back({H[i][1],L[i]});
        adj[H[i][1]].push_back({H[i][0],L[i]});
    }

    dfs(0,0,K);
    LL ans1 = LINF;
    for(int i=0;i<n;i++)
        ans1 = min(ans1,ans[i]);
    
    return ans1<n?ans1:-1;
}

/*

#define MAX_N 500000

static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;

inline 
void my_assert(int e) {if (!e) abort();}
void read_input()
{
  int i;
  my_assert(2==scanf("%d %d",&N,&K));
  for(i=0; i<N-1; i++)
    my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
  my_assert(1==scanf("%d",&solution));
}

int main()
{
  int ans1;
  read_input();
  ans1 = best_path(N,K,H,L);
  if(ans1==solution)
    printf("Correct.\n");
  else
    printf("Incorrect. Returned %d, Expected %d.\n",ans1,solution);

  return 0;
}*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...