Submission #1320395

#TimeUsernameProblemLanguageResultExecution timeMemory
1320395ThanhsRace (IOI11_race)C++20
Compilation error
0 ms0 KiB

#include <stdio.h>
#include <stdlib.h>

#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));
}

#include <bits/stdc++.h>
using namespace std;

#define name "TENBAI"
#define fi first
#define se second
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define all(x) x.begin(), x.end()

const int inf = 1e9;
const int NM = 2e5 + 5;

vector<pair<int, int>> g[NM];
int n, k, ans = inf, done[NM], sz[NM];
map<int, int> mp;
bool b;

int dfs_sz(int u, int p)
{
    sz[u] = 1;
    for (auto v : g[u])
        if (v.fi != p && !done[v.fi])
            sz[u] += dfs_sz(v.fi, u);
    return sz[u];
}

int findcen(int u, int p, int SZ)
{
    for (auto v : g[u])
        if (v.fi != p && !done[v.fi] && 2 * sz[v.fi] >= SZ)
            return findcen(v.fi, u, SZ);
    return u;
}

void get(int u, int p, int w, int c)
{
    if (w > k)
        return;
    if (mp.count(k - w))
        setmin(ans, c + mp[k - w]);
    for (auto v : g[u])
        if (v.fi != p && !done[v.fi])
            get(v.fi, u, w + v.se, c + 1);
}

void add(int u, int p, int w, int c)
{
    if (w > k)
        return;
    if (mp.count(w))
        setmin(mp[w], c);
    else
        mp[w] = c;
    for (auto v : g[u])
        if (v.fi != p && !done[v.fi])
            get(v.fi, u, w + v.se, c + 1);
}

void cen(int u)
{
    int c = findcen(u, -1, dfs_sz(u, -1));
    mp.clear();
    mp[0] = 0;
    for (auto v : g[c])
        if (!done[v.fi])
        {
            get(v.fi, c, v.se, 1);
            add(v.fi, c, v.se, 1);
        }
    done[c] = 1;
    for (auto v : g[c])
        if (!done[v.fi])
            cen(v.fi);
}

int best_path(int N, int K, int H[][2], int L[])
{
    n = N; k = K;
    for (int i = 0; i < n - 1; i++)
    {
        g[H[i][0]].emplace_back(H[i][1], L[i]);
        g[H[i][1]].emplace_back(H[i][0], L[i]);
    }
    cen(0);
  return ans == inf ? -1 : ans;
}

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
  int ans;
  read_input();
  ans = best_path(N,K,H,L);
  if(ans==solution)
    printf("Correct.\n");
  else
    printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);

  return 0;
}

Compilation message (stderr)

race.cpp: In function 'int main()':
race.cpp:116:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  116 |     freopen("in.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
race.cpp:117:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  117 |     freopen("out.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/ld: /tmp/ccbijcRQ.o: in function `read_input()':
grader.cpp:(.text+0x0): multiple definition of `read_input()'; /tmp/ccMMsAvZ.o:race.cpp:(.text+0x510): first defined here
/usr/bin/ld: /tmp/ccbijcRQ.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccMMsAvZ.o:race.cpp:(.text.startup+0x0): first defined here
collect2: error: ld returned 1 exit status