Submission #605109

# Submission time Handle Problem Language Result Execution time Memory
605109 2022-07-25T13:06:01 Z daanolav Race (IOI11_race) C++14
100 / 100
411 ms 35240 KB
#include <race.h>
#include <bits/stdc++.h>
 
 
using namespace std;
 
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
 
#define MAXN 200002
 
 
vii adjList[MAXN];
int treeSize[MAXN];
bool ign[MAXN];
 
int getSize(int node, int parent) {
  int size = 1;
  for(ii connection : adjList[node]) {
    int adj = connection.first;
    if(adj == parent) {
      continue;
    }
    if(ign[adj]) {
      continue;
    }
 
    size += getSize(adj,node);
  }
  treeSize[node] = size;
  return size;
}
 
int getCentroid(int node, int parent, int fullSize) {
  
  for(ii connection : adjList[node]) {
    int adj = connection.first;
    if(adj == parent) {
      continue;
    }
    if(ign[adj]) {
      continue;
    }
 
    if(treeSize[adj] * 2 > fullSize) {
      int res = getCentroid(adj,node,fullSize);
      //cout << "getCentroid(" << node << ") returned " << res << endl;
      return res;
    }
  }
  //cout << node << endl;
  return node;
}


int k;
vii pathLengthResults;
int best[1000002];
int auxBest[1000002];
int auxCur = 1;
 
void getPathLengths(int node, int parent,
  int traveled, int steps) {
 
 
  if(traveled > k) {
    return; 
  }
  pathLengthResults.push_back({traveled,steps});
  for(ii connection : adjList[node]) {
    int adj = connection.first;
    int weight = connection.second;
    if(adj == parent) {
      continue;
    }
    if(ign[adj]) {
      continue;
    }
    
    getPathLengths(adj,node, traveled + weight, steps + 1);
    
  }
 
}
 
 

 
int solveFor(int treeStart, int parent) {
  //cerr << "solveFor " << treeStart << endl;
  if(ign[treeStart]) {
    return 2 * MAXN;
  }
  getSize(treeStart,parent);
  int centroid = getCentroid(treeStart,parent,treeSize[treeStart]);
  int childMin = MAXN * 2;
  ign[centroid] = true;



  ++auxCur;
 
  //cout << "starting cross node for " << treeStart << endl;

  int weight,length;
  
  auxBest[0] = auxCur;
  for(ii connection : adjList[centroid]) {
    int adj = connection.first;
    if(ign[adj]) {
      continue;
    }
    pathLengthResults.clear();
    getPathLengths(adj,centroid,connection.second,1);
    for(ii path : pathLengthResults) {
      tie(weight,length) = path;
      if(weight < 0 || weight > k) {
        continue;
      }
      if(auxBest[k - weight] == auxCur) {
        childMin = min(childMin, best[k - weight] + length);
      }
    }
    for(ii path : pathLengthResults) {
      tie(weight,length) = path;
      
      if(auxBest[weight] != auxCur || best[weight] > length) {
        //cout << "added " << it.first << " with " << it.second << " paths" << endl;
        best[weight] = length;
        auxBest[weight] = auxCur;
      }
    }
  }




  for(ii connection : adjList[centroid]) {
    int adj = connection.first;
    int res = solveFor(adj,centroid);
    //cout << "child " << adj << " res " << res << endl;
    childMin = min(childMin, res);
  }


 
  ign[centroid] = false;
 
  //cerr << "solveFor " << treeStart << " result " << childMin << endl;
  return childMin;
 
 
 
}
 
 
int best_path(int N, int K, int H[][2], int L[])
{
  for(int i = 0; i < N - 1; ++i) {
    adjList[H[i][0]].push_back({H[i][1],L[i]});
    adjList[H[i][1]].push_back({H[i][0],L[i]});
  }
  k = K;
  int res = solveFor(0,-1);
  if(res == 2 * MAXN) {
    return -1;
  } else {
    return res;
  }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 4924 KB Output is correct
12 Correct 3 ms 4996 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 4924 KB Output is correct
12 Correct 3 ms 4996 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 7 ms 10036 KB Output is correct
23 Correct 6 ms 9300 KB Output is correct
24 Correct 6 ms 10836 KB Output is correct
25 Correct 7 ms 8532 KB Output is correct
26 Correct 7 ms 7676 KB Output is correct
27 Correct 5 ms 5076 KB Output is correct
28 Correct 6 ms 6612 KB Output is correct
29 Correct 6 ms 7636 KB Output is correct
30 Correct 6 ms 7892 KB Output is correct
31 Correct 7 ms 8704 KB Output is correct
32 Correct 6 ms 8788 KB Output is correct
33 Correct 8 ms 10372 KB Output is correct
34 Correct 6 ms 9556 KB Output is correct
35 Correct 7 ms 10836 KB Output is correct
36 Correct 7 ms 11604 KB Output is correct
37 Correct 5 ms 8916 KB Output is correct
38 Correct 5 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 4924 KB Output is correct
12 Correct 3 ms 4996 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 4948 KB Output is correct
19 Correct 106 ms 10456 KB Output is correct
20 Correct 140 ms 10316 KB Output is correct
21 Correct 116 ms 10644 KB Output is correct
22 Correct 90 ms 10800 KB Output is correct
23 Correct 61 ms 10588 KB Output is correct
24 Correct 46 ms 10400 KB Output is correct
25 Correct 91 ms 13228 KB Output is correct
26 Correct 75 ms 16508 KB Output is correct
27 Correct 158 ms 15772 KB Output is correct
28 Correct 223 ms 27060 KB Output is correct
29 Correct 226 ms 26160 KB Output is correct
30 Correct 186 ms 15816 KB Output is correct
31 Correct 163 ms 15800 KB Output is correct
32 Correct 202 ms 15852 KB Output is correct
33 Correct 186 ms 14752 KB Output is correct
34 Correct 193 ms 14604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 2 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 4 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 4 ms 4924 KB Output is correct
12 Correct 3 ms 4996 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 4 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 7 ms 10036 KB Output is correct
23 Correct 6 ms 9300 KB Output is correct
24 Correct 6 ms 10836 KB Output is correct
25 Correct 7 ms 8532 KB Output is correct
26 Correct 7 ms 7676 KB Output is correct
27 Correct 5 ms 5076 KB Output is correct
28 Correct 6 ms 6612 KB Output is correct
29 Correct 6 ms 7636 KB Output is correct
30 Correct 6 ms 7892 KB Output is correct
31 Correct 7 ms 8704 KB Output is correct
32 Correct 6 ms 8788 KB Output is correct
33 Correct 8 ms 10372 KB Output is correct
34 Correct 6 ms 9556 KB Output is correct
35 Correct 7 ms 10836 KB Output is correct
36 Correct 7 ms 11604 KB Output is correct
37 Correct 5 ms 8916 KB Output is correct
38 Correct 5 ms 7380 KB Output is correct
39 Correct 106 ms 10456 KB Output is correct
40 Correct 140 ms 10316 KB Output is correct
41 Correct 116 ms 10644 KB Output is correct
42 Correct 90 ms 10800 KB Output is correct
43 Correct 61 ms 10588 KB Output is correct
44 Correct 46 ms 10400 KB Output is correct
45 Correct 91 ms 13228 KB Output is correct
46 Correct 75 ms 16508 KB Output is correct
47 Correct 158 ms 15772 KB Output is correct
48 Correct 223 ms 27060 KB Output is correct
49 Correct 226 ms 26160 KB Output is correct
50 Correct 186 ms 15816 KB Output is correct
51 Correct 163 ms 15800 KB Output is correct
52 Correct 202 ms 15852 KB Output is correct
53 Correct 186 ms 14752 KB Output is correct
54 Correct 193 ms 14604 KB Output is correct
55 Correct 13 ms 5588 KB Output is correct
56 Correct 9 ms 5588 KB Output is correct
57 Correct 72 ms 11028 KB Output is correct
58 Correct 34 ms 10532 KB Output is correct
59 Correct 84 ms 17356 KB Output is correct
60 Correct 396 ms 35008 KB Output is correct
61 Correct 161 ms 18892 KB Output is correct
62 Correct 179 ms 18876 KB Output is correct
63 Correct 254 ms 18968 KB Output is correct
64 Correct 411 ms 22108 KB Output is correct
65 Correct 198 ms 19692 KB Output is correct
66 Correct 359 ms 35240 KB Output is correct
67 Correct 111 ms 19520 KB Output is correct
68 Correct 224 ms 24852 KB Output is correct
69 Correct 227 ms 25116 KB Output is correct
70 Correct 235 ms 24192 KB Output is correct