Submission #859789

# Submission time Handle Problem Language Result Execution time Memory
859789 2023-10-10T15:58:08 Z vladburac Race (IOI11_race) C++17
100 / 100
1953 ms 195256 KB
#include <bits/stdc++.h>
//#include "race.h"
using namespace std;
using ll = long long;
const int NMAX = 2e5;
const int INF = 1e9;
const int MAXLOG = 18;

int parent[NMAX+1];
bool is_removed[NMAX+1];
int sz[NMAX+1];
vector <pair<ll, int>> distSubtree[NMAX+1];
vector <pair<int, int>> edges[NMAX+1];
vector<int> centroidEdges[NMAX+1];

int calcSize( int node, int parent ) {
  sz[node] = 1;
  for( auto vec: edges[node] ) {
    if( vec.first != parent && !is_removed[vec.first] )
      sz[node] += calcSize( vec.first, node );
  }
  return sz[node];
}

int find_centroid( int node, int parent, int totalSz ) {
  for( auto vec: edges[node] ) {
    if( vec.first != parent && !is_removed[vec.first] && sz[vec.first] > totalSz / 2 )
      return find_centroid( vec.first, node, totalSz );
  }
  return node;
}

struct Ancestor{
  ll sum;
  int length;
};
Ancestor anc[NMAX+1][MAXLOG+3];
int depth[NMAX+1];

void updDist( int init, int node, int parent, int dep, ll sumEdges ) {
  distSubtree[init].push_back( { sumEdges, node } );
  anc[node][depth[init]] = { sumEdges, dep };
  for( auto vec: edges[node] ) {
    if( !is_removed[vec.first] && vec.first != parent )
      updDist( init, vec.first, node, dep + 1, sumEdges + vec.second );
  }
}

void decomposition( int node, int centroidParent, int dep ) {
  int totalSz = calcSize( node, -1 );
  int centroid = find_centroid( node, -1, totalSz );
  depth[centroid] = dep;
  is_removed[centroid] = true;
  parent[centroid] = centroidParent;
  centroidEdges[centroid].push_back( centroidParent );
  centroidEdges[centroidParent].push_back( centroid );
  updDist( centroid, centroid, -1, 0, 0 );
  for( auto vec: edges[centroid] ) {
    //cout << vec.first << " ";
    if( !is_removed[vec.first] )
      decomposition( vec.first, centroid, dep + 1 );
  }
}

map<ll, int> dp;
int ans = INF;

void dfs( int node, int i, int type, int k ) {
  ll sum = anc[node][depth[i]].sum;
  int length = anc[node][depth[i]].length;
  if( type == 0 && dp.find( k - sum ) != dp.end() )
    ans = min( ans, dp[k-sum] + length );
  else if( type == 1 ) {
    if( dp[sum] )
      dp[sum] = min( dp[sum], length );
    else
      dp[sum] = length;
  }
  for( auto vec: centroidEdges[node] ) {
    if( vec != parent[node] )
      dfs( vec, i, type, k );
  }
}
int best_path( int n, int k, int h[][2], int len[] ) {
  int i;
  for( i = 0; i < n - 1; i++ ) {
    edges[h[i][0]].push_back( { h[i][1], len[i] } );
    edges[h[i][1]].push_back( { h[i][0], len[i] } );
  }
  decomposition( 0, -1, 0 );
  ans = INF;
  for( i = 0; i < n; i++ ) {
    dp.clear();
    dp[0] = 0;
    for( auto vec: centroidEdges[i] ) {
      if( vec != parent[i] ) {
        dfs( vec, i, 0, k );
        dfs( vec, i, 1, k );
      }
    }
  }
  if( ans == INF )
    ans = -1;
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21140 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 5 ms 21084 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 6 ms 21100 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21080 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 6 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21140 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 5 ms 21084 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 6 ms 21100 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21080 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 6 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21080 KB Output is correct
19 Correct 4 ms 21080 KB Output is correct
20 Correct 4 ms 21084 KB Output is correct
21 Correct 7 ms 21340 KB Output is correct
22 Correct 6 ms 21340 KB Output is correct
23 Correct 6 ms 21340 KB Output is correct
24 Correct 6 ms 21340 KB Output is correct
25 Correct 6 ms 21340 KB Output is correct
26 Correct 7 ms 21340 KB Output is correct
27 Correct 5 ms 21340 KB Output is correct
28 Correct 7 ms 21340 KB Output is correct
29 Correct 6 ms 21336 KB Output is correct
30 Correct 7 ms 21596 KB Output is correct
31 Correct 6 ms 21340 KB Output is correct
32 Correct 6 ms 21340 KB Output is correct
33 Correct 8 ms 21340 KB Output is correct
34 Correct 6 ms 21340 KB Output is correct
35 Correct 6 ms 21340 KB Output is correct
36 Correct 6 ms 21340 KB Output is correct
37 Correct 6 ms 21340 KB Output is correct
38 Correct 6 ms 21340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21140 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 5 ms 21084 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 6 ms 21100 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21080 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 6 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21080 KB Output is correct
19 Correct 487 ms 86976 KB Output is correct
20 Correct 455 ms 86920 KB Output is correct
21 Correct 468 ms 86720 KB Output is correct
22 Correct 438 ms 84288 KB Output is correct
23 Correct 604 ms 91068 KB Output is correct
24 Correct 242 ms 78756 KB Output is correct
25 Correct 486 ms 102088 KB Output is correct
26 Correct 210 ms 102340 KB Output is correct
27 Correct 398 ms 133704 KB Output is correct
28 Correct 1643 ms 195256 KB Output is correct
29 Correct 1953 ms 194520 KB Output is correct
30 Correct 441 ms 133812 KB Output is correct
31 Correct 440 ms 134232 KB Output is correct
32 Correct 678 ms 134628 KB Output is correct
33 Correct 1207 ms 161360 KB Output is correct
34 Correct 1540 ms 172108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 21084 KB Output is correct
2 Correct 5 ms 21084 KB Output is correct
3 Correct 4 ms 21084 KB Output is correct
4 Correct 4 ms 21080 KB Output is correct
5 Correct 4 ms 21140 KB Output is correct
6 Correct 5 ms 21084 KB Output is correct
7 Correct 4 ms 21084 KB Output is correct
8 Correct 5 ms 21084 KB Output is correct
9 Correct 5 ms 21080 KB Output is correct
10 Correct 6 ms 21100 KB Output is correct
11 Correct 4 ms 21084 KB Output is correct
12 Correct 5 ms 21080 KB Output is correct
13 Correct 5 ms 21080 KB Output is correct
14 Correct 5 ms 21080 KB Output is correct
15 Correct 4 ms 21084 KB Output is correct
16 Correct 6 ms 21084 KB Output is correct
17 Correct 5 ms 21084 KB Output is correct
18 Correct 5 ms 21080 KB Output is correct
19 Correct 4 ms 21080 KB Output is correct
20 Correct 4 ms 21084 KB Output is correct
21 Correct 7 ms 21340 KB Output is correct
22 Correct 6 ms 21340 KB Output is correct
23 Correct 6 ms 21340 KB Output is correct
24 Correct 6 ms 21340 KB Output is correct
25 Correct 6 ms 21340 KB Output is correct
26 Correct 7 ms 21340 KB Output is correct
27 Correct 5 ms 21340 KB Output is correct
28 Correct 7 ms 21340 KB Output is correct
29 Correct 6 ms 21336 KB Output is correct
30 Correct 7 ms 21596 KB Output is correct
31 Correct 6 ms 21340 KB Output is correct
32 Correct 6 ms 21340 KB Output is correct
33 Correct 8 ms 21340 KB Output is correct
34 Correct 6 ms 21340 KB Output is correct
35 Correct 6 ms 21340 KB Output is correct
36 Correct 6 ms 21340 KB Output is correct
37 Correct 6 ms 21340 KB Output is correct
38 Correct 6 ms 21340 KB Output is correct
39 Correct 487 ms 86976 KB Output is correct
40 Correct 455 ms 86920 KB Output is correct
41 Correct 468 ms 86720 KB Output is correct
42 Correct 438 ms 84288 KB Output is correct
43 Correct 604 ms 91068 KB Output is correct
44 Correct 242 ms 78756 KB Output is correct
45 Correct 486 ms 102088 KB Output is correct
46 Correct 210 ms 102340 KB Output is correct
47 Correct 398 ms 133704 KB Output is correct
48 Correct 1643 ms 195256 KB Output is correct
49 Correct 1953 ms 194520 KB Output is correct
50 Correct 441 ms 133812 KB Output is correct
51 Correct 440 ms 134232 KB Output is correct
52 Correct 678 ms 134628 KB Output is correct
53 Correct 1207 ms 161360 KB Output is correct
54 Correct 1540 ms 172108 KB Output is correct
55 Correct 27 ms 25932 KB Output is correct
56 Correct 21 ms 25816 KB Output is correct
57 Correct 290 ms 90916 KB Output is correct
58 Correct 59 ms 70220 KB Output is correct
59 Correct 432 ms 106976 KB Output is correct
60 Correct 1645 ms 193736 KB Output is correct
61 Correct 444 ms 135868 KB Output is correct
62 Correct 433 ms 133980 KB Output is correct
63 Correct 646 ms 133820 KB Output is correct
64 Correct 1321 ms 157676 KB Output is correct
65 Correct 1351 ms 158040 KB Output is correct
66 Correct 1922 ms 194240 KB Output is correct
67 Correct 286 ms 116424 KB Output is correct
68 Correct 872 ms 142128 KB Output is correct
69 Correct 860 ms 142176 KB Output is correct
70 Correct 774 ms 138164 KB Output is correct