Submission #988479

# Submission time Handle Problem Language Result Execution time Memory
988479 2024-05-24T20:04:11 Z HoriaHaivas Race (IOI11_race) C++14
100 / 100
2703 ms 126096 KB
/*
    "care a facut teste cu Lattice reduction attack e ciudat"
    "linistiti va putin"
    - 2023 -
*/
#include<bits/stdc++.h>
#include "race.h"
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
#pragma GCC optimize("Ofast")
#define int long long

using namespace std;

struct edge
{
    int to;
    int cost;
};

int n,k;
int ans;
bool blocked[200001];
int sz[200001];
int h[200001][2];
int l[200001];
vector<edge> nodes[200001];
unordered_map<int,int> f;

void recalc(int node, int parent)
{
    sz[node]=1;
    for (auto x : nodes[node])
    {
        if (x.to!=parent && !blocked[x.to])
        {
            recalc(x.to,node);
            sz[node]+=sz[x.to];
        }
    }
}

int find_centroid(int compsize, int node, int parent)
{
    for (auto x : nodes[node])
    {
        if (x.to!=parent && !blocked[x.to] && sz[x.to]>compsize/2)
            return find_centroid(compsize,x.to,node);
    }
    return node;
}

void dfs(int node, int parent, int cost, int len)
{
    if (f.find(k-cost)!=f.end())
    ans=min(ans,len+f[k-cost]);
    for (auto x : nodes[node])
    {
        if (x.to!=parent && !blocked[x.to])
            dfs(x.to,node,cost+x.cost,len+1);
    }
}

void update(int node, int parent, int cost, int len)
{
    f[cost]=min(f[cost],len);
    for (auto x : nodes[node])
    {
        if (x.to!=parent && !blocked[x.to])
            update(x.to,node,cost+x.cost,len+1);
    }
}

void rem(int node, int parent, int cost)
{
    f[cost]=n+1;
    for (auto x : nodes[node])
    {
        if (x.to!=parent && !blocked[x.to])
            rem(x.to,node,cost+x.cost);
    }
}

void solve(int node)
{
    int c,i;
    recalc(node,-1);
    c=find_centroid(sz[node],node,-1);
    blocked[c]=1;
    f[0]=0;
    for (auto x : nodes[c])
    {
        if (!blocked[x.to])
        {
            dfs(x.to,c,x.cost,1);
            update(x.to,c,x.cost,1);
        }
    }
    for (auto x : nodes[c])
    {
        if (!blocked[x.to])
        {
            rem(x.to,c,x.cost);
        }
    }
    for (auto x : nodes[c])
    {
        if (!blocked[x.to])
        {
            solve(x.to);
        }
    }
}

signed best_path(signed N, signed K, signed H[][2], signed L[])
{
    int i,j,c,x,y;
    n=N;
    k=K;
    for (i=0;i<=k;i++)
         f[i]=n+1;
    for (i=0; i<n; i++)
    {
        x=H[i][0];
        y=H[i][1];
        x++;
        y++;
        c=L[i];
        nodes[x].push_back({y,c});
        nodes[y].push_back({x,c});
    }
    ans=n+1;
    solve(1);
    if (ans!=n+1)
        return ans;
    else
        return -1;
}

Compilation message

race.cpp: In function 'void solve(long long int)':
race.cpp:86:11: warning: unused variable 'i' [-Wunused-variable]
   86 |     int c,i;
      |           ^
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:117:11: warning: unused variable 'j' [-Wunused-variable]
  117 |     int i,j,c,x,y;
      |           ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4956 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 5036 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4956 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 5036 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 4 ms 4952 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 66 ms 45488 KB Output is correct
23 Correct 59 ms 44288 KB Output is correct
24 Correct 65 ms 44308 KB Output is correct
25 Correct 64 ms 44224 KB Output is correct
26 Correct 28 ms 18736 KB Output is correct
27 Correct 60 ms 44184 KB Output is correct
28 Correct 18 ms 14544 KB Output is correct
29 Correct 24 ms 18052 KB Output is correct
30 Correct 31 ms 24272 KB Output is correct
31 Correct 44 ms 31172 KB Output is correct
32 Correct 48 ms 32964 KB Output is correct
33 Correct 58 ms 44240 KB Output is correct
34 Correct 42 ms 29376 KB Output is correct
35 Correct 62 ms 44224 KB Output is correct
36 Correct 68 ms 45572 KB Output is correct
37 Correct 60 ms 44368 KB Output is correct
38 Correct 38 ms 26576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4956 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 5036 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 157 ms 12120 KB Output is correct
20 Correct 155 ms 12112 KB Output is correct
21 Correct 148 ms 12116 KB Output is correct
22 Correct 147 ms 12408 KB Output is correct
23 Correct 117 ms 12628 KB Output is correct
24 Correct 69 ms 11860 KB Output is correct
25 Correct 143 ms 17948 KB Output is correct
26 Correct 115 ms 23548 KB Output is correct
27 Correct 185 ms 20052 KB Output is correct
28 Correct 1382 ms 82760 KB Output is correct
29 Correct 1110 ms 77944 KB Output is correct
30 Correct 187 ms 20052 KB Output is correct
31 Correct 179 ms 20052 KB Output is correct
32 Correct 345 ms 20128 KB Output is correct
33 Correct 284 ms 19028 KB Output is correct
34 Correct 2703 ms 126096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4956 KB Output is correct
2 Correct 3 ms 4956 KB Output is correct
3 Correct 3 ms 4956 KB Output is correct
4 Correct 3 ms 4956 KB Output is correct
5 Correct 3 ms 4956 KB Output is correct
6 Correct 3 ms 4956 KB Output is correct
7 Correct 3 ms 4956 KB Output is correct
8 Correct 3 ms 4956 KB Output is correct
9 Correct 3 ms 4952 KB Output is correct
10 Correct 3 ms 4956 KB Output is correct
11 Correct 3 ms 4956 KB Output is correct
12 Correct 3 ms 4956 KB Output is correct
13 Correct 3 ms 5036 KB Output is correct
14 Correct 3 ms 4956 KB Output is correct
15 Correct 3 ms 4956 KB Output is correct
16 Correct 3 ms 4956 KB Output is correct
17 Correct 3 ms 4956 KB Output is correct
18 Correct 3 ms 4956 KB Output is correct
19 Correct 4 ms 4952 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 66 ms 45488 KB Output is correct
23 Correct 59 ms 44288 KB Output is correct
24 Correct 65 ms 44308 KB Output is correct
25 Correct 64 ms 44224 KB Output is correct
26 Correct 28 ms 18736 KB Output is correct
27 Correct 60 ms 44184 KB Output is correct
28 Correct 18 ms 14544 KB Output is correct
29 Correct 24 ms 18052 KB Output is correct
30 Correct 31 ms 24272 KB Output is correct
31 Correct 44 ms 31172 KB Output is correct
32 Correct 48 ms 32964 KB Output is correct
33 Correct 58 ms 44240 KB Output is correct
34 Correct 42 ms 29376 KB Output is correct
35 Correct 62 ms 44224 KB Output is correct
36 Correct 68 ms 45572 KB Output is correct
37 Correct 60 ms 44368 KB Output is correct
38 Correct 38 ms 26576 KB Output is correct
39 Correct 157 ms 12120 KB Output is correct
40 Correct 155 ms 12112 KB Output is correct
41 Correct 148 ms 12116 KB Output is correct
42 Correct 147 ms 12408 KB Output is correct
43 Correct 117 ms 12628 KB Output is correct
44 Correct 69 ms 11860 KB Output is correct
45 Correct 143 ms 17948 KB Output is correct
46 Correct 115 ms 23548 KB Output is correct
47 Correct 185 ms 20052 KB Output is correct
48 Correct 1382 ms 82760 KB Output is correct
49 Correct 1110 ms 77944 KB Output is correct
50 Correct 187 ms 20052 KB Output is correct
51 Correct 179 ms 20052 KB Output is correct
52 Correct 345 ms 20128 KB Output is correct
53 Correct 284 ms 19028 KB Output is correct
54 Correct 2703 ms 126096 KB Output is correct
55 Correct 13 ms 6492 KB Output is correct
56 Correct 13 ms 5888 KB Output is correct
57 Correct 87 ms 12884 KB Output is correct
58 Correct 34 ms 12740 KB Output is correct
59 Correct 122 ms 32380 KB Output is correct
60 Correct 743 ms 84736 KB Output is correct
61 Correct 190 ms 22192 KB Output is correct
62 Correct 267 ms 64748 KB Output is correct
63 Correct 390 ms 64728 KB Output is correct
64 Correct 1066 ms 42060 KB Output is correct
65 Correct 2292 ms 102200 KB Output is correct
66 Correct 1666 ms 114928 KB Output is correct
67 Correct 184 ms 64496 KB Output is correct
68 Correct 748 ms 68972 KB Output is correct
69 Correct 774 ms 69672 KB Output is correct
70 Correct 646 ms 71216 KB Output is correct