Submission #1084280

# Submission time Handle Problem Language Result Execution time Memory
1084280 2024-09-05T18:12:48 Z EkinOnal Race (IOI11_race) C++17
0 / 100
5 ms 10588 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

#define MAX 200000
#define pb push_back
#define mp make_pair 
#define f first
#define s second
#define vi vector<int>
#define pii pair<int,int>
#define si set<int>
#define vpii vector<pair<int,int>> 
const int mod = 1e9+7;
const int INF = 1e9;

int n,k,cev;
vpii adj[MAX];
vi dead(MAX),sz(MAX),mpp(1e6+5);

void dfs(int node,int par){
  sz[node]=1;
  for(auto u : adj[node]){
    if(dead[u.f] || u.f==par) continue;
    sz[node]+=sz[u.f];
  }
}

int findc(int node,int par,int val){
  for(auto u : adj[node]){
    if(dead[u.f] || u.f==par) continue;
    if(sz[u.f]>=val/2) return findc(u.f,node,val); // val olmalidir
  }
  return node;
}

void check(int node,int par,int len,int sayi){
  if(len>k) return;
  if(mpp[k-len]!=INF) cev=min(cev,mpp[k-len]+sayi);
  for(auto u : adj[node]){
    if(dead[u.f] || u.f==par) continue;
    check(u.f,node,len+u.s,sayi+1);
  }
}

void build(int node,int par,int len,int sayi){
  if(len>k) return;
  mpp[len]=min(mpp[len],sayi);
  for(auto u : adj[node]){
    if(dead[u.f] || u.f==par) continue;
    check(u.f,node,len+u.s,sayi+1);
  }
}

void sifirla(int node,int par,int der){
  if(der>k) return;
  mpp[der]=INF;
  for(auto u : adj[node]){
    if(dead[u.f] || u.f==par) continue;
    sifirla(u.f,node,der+u.s);
  }
}

void solve(int node){
  dfs(node,node);
  int x = findc(node,node,sz[node]);
  dead[x]=true;
  mpp[0]=0;
  for(auto u : adj[x]){
    if(dead[u.f]) continue;
    check(u.f,x,u.s,1);
    build(u.f,x,u.s,1);
  }
   for(auto u : adj[x]){
    if(dead[u.f]) continue;
    sifirla(u.f,x,u.s);
  }

  for(auto u : adj[x]){
    if(dead[u.f]) continue;
    solve(u.f);
  }

}


int best_path(int N, int K, int H[][2], int L[])
{
  n=N,k=K;
  for(int i=0;i<n-1;i++){
    adj[H[i][0]].pb({H[i][1],L[i]});
    adj[H[i][1]].pb({H[i][0],L[i]});
  }
  fill(mpp.begin(),mpp.end(),INF);

  cev=n+2;
  solve(0);
  if(cev==n+2) cev=-1;
  return cev;
}

# Verdict Execution time Memory Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Incorrect 5 ms 10588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Incorrect 5 ms 10588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Incorrect 5 ms 10588 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 10588 KB Output is correct
2 Correct 5 ms 10588 KB Output is correct
3 Incorrect 5 ms 10588 KB Output isn't correct
4 Halted 0 ms 0 KB -