Submission #915319

# Submission time Handle Problem Language Result Execution time Memory
915319 2024-01-23T16:48:30 Z StefanL2005 Race (IOI11_race) C++14
100 / 100
602 ms 83416 KB
#include <bits/stdc++.h>
using namespace std;
#define inf 1000*1000*1000
#define ll long long

struct nod{
    int nr;
    vector<nod*> Ch;
    vector<int> Cost;

    nod(){nr = 0;}
};

struct path{
    int br, sum, muchi;
};
vector<vector<path>> Sum;

void DFS_Descendants(nod *node, nod *p, vector<int> &desc)
{
    desc[node->nr] = 0;

    for (int i = 0; i < node->Ch.size(); i++)
    {
        auto vecin = node->Ch[i];

        if (vecin == p || vecin == 0)
            continue;
        DFS_Descendants(vecin, node, desc);
        desc[node->nr] += desc[vecin->nr];
    }

    desc[node->nr]++;
}

nod* Locate_Central(nod *node, nod *p, int size, vector<int> &desc)
{
    for (int i = 0; i < node->Ch.size(); i++)
    {
        auto vecin = node->Ch[i];
        
        if (vecin == p || vecin == 0)
            continue;
        
        if (desc[vecin->nr] > size/2)
        {
            desc[node->nr] -= desc[vecin->nr];
            desc[vecin->nr] += desc[node->nr];

            return Locate_Central(vecin, node, size, desc);
        }
    }

    return node;
}

void DFS_Paths(nod *node, nod *block, int branch, int p, int s, vector<path> &All_Paths, int k)
{
    if (s > k)
        return;
    path A; A.br = branch; A.sum = s; A.muchi = p;
    All_Paths.push_back(A);
    
    for (int i = 0; i < node->Ch.size(); i++)
    {
        auto vecin = node->Ch[i];
        if (vecin == block || vecin == 0)
            continue;

        DFS_Paths(vecin, node, branch, p + 1, s + node->Cost[i], All_Paths, k);
    }
}

void CalculatePaths(nod* node, int k, int &best)
{
    vector<path> All_Paths;

    int branch = 0;
    for (int i = 0; i < node->Ch.size(); i++)
    {
        auto vecin = node->Ch[i];
        if (vecin == 0)
            continue;
        
        DFS_Paths(vecin, node, branch, 1, node->Cost[i], All_Paths, k);
        branch++;
    }

    for (int i = 0; i < All_Paths.size(); i++)
    {
        auto A = All_Paths[i];

        if (A.sum > k)
            continue;
        switch(Sum[A.sum].size()) 
        {
            case 0:{ Sum[A.sum].push_back(A); break; }
            case 1:{ 
                if (A.br == Sum[A.sum][0].br)
                {
                    if (A.muchi < Sum[A.sum][0].muchi)
                        Sum[A.sum][0] = A;
                    break;
                }

                Sum[A.sum].push_back(A);
                if (Sum[A.sum][1].muchi < Sum[A.sum][0].muchi)
                    swap (Sum[A.sum][1], Sum[A.sum][0]);
            break; }

            default: {
                if (A.br == Sum[A.sum][0].br)
                {
                    if (A.muchi < Sum[A.sum][0].muchi)
                        Sum[A.sum][0] = A;
                    break;
                }

                if (A.muchi < Sum[A.sum][1].muchi)
                    Sum[A.sum][1] = A;
                if (Sum[A.sum][1].muchi < Sum[A.sum][0].muchi)
                    swap (Sum[A.sum][1], Sum[A.sum][0]);
            break; }
        }
    }

    for (int i = 0; i < All_Paths.size(); i++)
    {
        auto A = All_Paths[i];
        if (A.sum > k)
            continue;
        if (A.sum == k)
        {   
            best = min(best, A.muchi);
            continue;
        }
        if (Sum[k - A.sum].size() == 0)
            continue;

        auto B = Sum[k - A.sum][0];
        if (B.br == A.br)
        {
            if (Sum[B.sum].size() == 1)
                continue;
            B = Sum[B.sum][1];
        }
        
        best = min(best, A.muchi + B.muchi);
    }
    for (int i = 0; i < All_Paths.size(); i++)
        Sum[All_Paths[i].sum].clear();
}

void D_C (nod *root, int k, int size, vector<int> &desc, int &best)
{
    if (size == 1)
        return;

    nod *center;
    DFS_Descendants(root, NULL, desc);
    center = Locate_Central(root, NULL, size, desc);
    CalculatePaths(center, k, best);   

    for (int i = 0; i < center->Ch.size(); i++)
    {
        auto vecin = center->Ch[i];

        if (vecin == 0)
            continue;

        for (int j = 0; j < vecin->Ch.size(); j++)
            if (vecin->Ch[j] == center)
            { vecin->Ch[j] = NULL; break; }

        D_C(vecin, k, desc[vecin->nr], desc, best);
    }
}

int best_path(int n, int k, int H[][2], int L[])
{
    int best = inf;
    vector<int> desc(n, 0);
    vector<nod*> Tree(n);
    Sum.resize(k + 1);

    for (int i = 0; i < n; i++)
    { Tree[i] = new nod; Tree[i]->nr = i; }
    
    for (int i = 0; i < n-1; i++)
    {
        int A = H[i][0], B = H[i][1];
        Tree[A]->Ch.push_back(Tree[B]);
        Tree[B]->Ch.push_back(Tree[A]);
        Tree[A]->Cost.push_back(L[i]);
        Tree[B]->Cost.push_back(L[i]);
    }  

    D_C(Tree[0], k, n, desc, best);

    if (best == inf)
        return -1;
    return best;
}

Compilation message

race.cpp: In function 'void DFS_Descendants(nod*, nod*, std::vector<int>&)':
race.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nod*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |     for (int i = 0; i < node->Ch.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
race.cpp: In function 'nod* Locate_Central(nod*, nod*, int, std::vector<int>&)':
race.cpp:38:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nod*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for (int i = 0; i < node->Ch.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
race.cpp: In function 'void DFS_Paths(nod*, nod*, int, int, int, std::vector<path>&, int)':
race.cpp:64:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nod*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   64 |     for (int i = 0; i < node->Ch.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
race.cpp: In function 'void CalculatePaths(nod*, int, int&)':
race.cpp:79:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nod*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   79 |     for (int i = 0; i < node->Ch.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~
race.cpp:89:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<path>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |     for (int i = 0; i < All_Paths.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
race.cpp:127:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<path>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  127 |     for (int i = 0; i < All_Paths.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
race.cpp:150:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<path>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |     for (int i = 0; i < All_Paths.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~
race.cpp: In function 'void D_C(nod*, int, int, std::vector<int>&, int&)':
race.cpp:164:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nod*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |     for (int i = 0; i < center->Ch.size(); i++)
      |                     ~~^~~~~~~~~~~~~~~~~~~
race.cpp:171:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<nod*>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |         for (int j = 0; j < vecin->Ch.size(); j++)
      |                         ~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 9 ms 24156 KB Output is correct
23 Correct 7 ms 20316 KB Output is correct
24 Correct 9 ms 22876 KB Output is correct
25 Correct 8 ms 22432 KB Output is correct
26 Correct 4 ms 10588 KB Output is correct
27 Correct 8 ms 21340 KB Output is correct
28 Correct 3 ms 7256 KB Output is correct
29 Correct 4 ms 10332 KB Output is correct
30 Correct 5 ms 11356 KB Output is correct
31 Correct 7 ms 18012 KB Output is correct
32 Correct 7 ms 19292 KB Output is correct
33 Correct 8 ms 21080 KB Output is correct
34 Correct 5 ms 16476 KB Output is correct
35 Correct 8 ms 21848 KB Output is correct
36 Correct 10 ms 24412 KB Output is correct
37 Correct 8 ms 21340 KB Output is correct
38 Correct 7 ms 14712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 122 ms 19256 KB Output is correct
20 Correct 116 ms 19140 KB Output is correct
21 Correct 121 ms 19792 KB Output is correct
22 Correct 124 ms 20600 KB Output is correct
23 Correct 113 ms 19196 KB Output is correct
24 Correct 55 ms 19292 KB Output is correct
25 Correct 132 ms 20772 KB Output is correct
26 Correct 96 ms 26184 KB Output is correct
27 Correct 189 ms 34072 KB Output is correct
28 Correct 354 ms 41456 KB Output is correct
29 Correct 281 ms 40688 KB Output is correct
30 Correct 167 ms 34128 KB Output is correct
31 Correct 192 ms 33876 KB Output is correct
32 Correct 220 ms 33872 KB Output is correct
33 Correct 248 ms 32408 KB Output is correct
34 Correct 202 ms 31824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 0 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2648 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 2 ms 2648 KB Output is correct
22 Correct 9 ms 24156 KB Output is correct
23 Correct 7 ms 20316 KB Output is correct
24 Correct 9 ms 22876 KB Output is correct
25 Correct 8 ms 22432 KB Output is correct
26 Correct 4 ms 10588 KB Output is correct
27 Correct 8 ms 21340 KB Output is correct
28 Correct 3 ms 7256 KB Output is correct
29 Correct 4 ms 10332 KB Output is correct
30 Correct 5 ms 11356 KB Output is correct
31 Correct 7 ms 18012 KB Output is correct
32 Correct 7 ms 19292 KB Output is correct
33 Correct 8 ms 21080 KB Output is correct
34 Correct 5 ms 16476 KB Output is correct
35 Correct 8 ms 21848 KB Output is correct
36 Correct 10 ms 24412 KB Output is correct
37 Correct 8 ms 21340 KB Output is correct
38 Correct 7 ms 14712 KB Output is correct
39 Correct 122 ms 19256 KB Output is correct
40 Correct 116 ms 19140 KB Output is correct
41 Correct 121 ms 19792 KB Output is correct
42 Correct 124 ms 20600 KB Output is correct
43 Correct 113 ms 19196 KB Output is correct
44 Correct 55 ms 19292 KB Output is correct
45 Correct 132 ms 20772 KB Output is correct
46 Correct 96 ms 26184 KB Output is correct
47 Correct 189 ms 34072 KB Output is correct
48 Correct 354 ms 41456 KB Output is correct
49 Correct 281 ms 40688 KB Output is correct
50 Correct 167 ms 34128 KB Output is correct
51 Correct 192 ms 33876 KB Output is correct
52 Correct 220 ms 33872 KB Output is correct
53 Correct 248 ms 32408 KB Output is correct
54 Correct 202 ms 31824 KB Output is correct
55 Correct 9 ms 4188 KB Output is correct
56 Correct 9 ms 4188 KB Output is correct
57 Correct 81 ms 20704 KB Output is correct
58 Correct 37 ms 21700 KB Output is correct
59 Correct 108 ms 33072 KB Output is correct
60 Correct 587 ms 83416 KB Output is correct
61 Correct 187 ms 37964 KB Output is correct
62 Correct 213 ms 63428 KB Output is correct
63 Correct 283 ms 63424 KB Output is correct
64 Correct 602 ms 56864 KB Output is correct
65 Correct 247 ms 37316 KB Output is correct
66 Correct 500 ms 70684 KB Output is correct
67 Correct 141 ms 66500 KB Output is correct
68 Correct 318 ms 67496 KB Output is correct
69 Correct 334 ms 67872 KB Output is correct
70 Correct 305 ms 65748 KB Output is correct