답안 #777861

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
777861 2023-07-09T19:24:32 Z a_aguilo 경주 (Race) (IOI11_race) C++14
100 / 100
254 ms 34924 KB
#include "race.h"
#include<bits/stdc++.h>
 
using namespace std;
 
int n, k, cnt;
const int maxN = 200000;
const int maxK = 1000002;
vector<pair<int, int>> AdjList[maxN];
vector<int> exists;
int padre[maxN];
int subTreeSize[maxN];
pair<int, int> prov[maxN];
int lengths[maxK];
int centroidToLength[maxK];
 
void print(vector<pair<int, int>>& V){
  for(pair<int, int>& pii: V){
    cout << pii.first << " ";
  }
  cout << endl;
}
 
void preProcess(int node){
  subTreeSize[node] = 1;
  for(pair<int, int>& next: AdjList[node]){
    //cout << node << " -> " << next.first << endl;
    if(next.first != padre[node] && exists[next.first]){
      padre[next.first] = node;
      preProcess(next.first);
      subTreeSize[node] += subTreeSize[next.first];
    }
  }
  //cout << node << " " << subTreeSize[node] << endl;
}
 
int find_Centroid(int node, int sze){
  for(int i = 0; i < AdjList[node].size(); ++i){
    pair<int, int> next = AdjList[node][i];
    if(next.first == padre[node] || !exists[next.first]) continue;
    if(subTreeSize[next.first] > (sze/2)) {
      i = -1;
      node = next.first;
    }
  }
  return node;
}
 
void findDistances(int node, int currDist, int numEdges){
  if(currDist > k) return;
  prov[cnt] = {currDist, numEdges};
  cnt++;
  for(pair<int, int>& next: AdjList[node]){
    if(exists[next.first] && next.first != padre[node]){
      padre[next.first] = node;
      findDistances(next.first, currDist+next.second, numEdges+1);
    }
  }
}
 
int solveSubTree(int node){
  padre[node] = node;
  preProcess(node);
  int sze = subTreeSize[node];
  node = find_Centroid(node, sze);
  //cout << node << endl;
  padre[node] = node;
  int ans = 1e9;
 
  for(pair<int, int>& next: AdjList[node]){
    if(next.first == padre[node]) continue;
    if(!exists[next.first]) continue;
    padre[next.first] = node;
    cnt = 0;
    findDistances(next.first, next.second, 1);
    for(int i = 0; i < cnt; ++i){
      pair<int, int> val = prov[i];
      int d = val.first;
      int edges = val.second;
      if(centroidToLength[k-d] == node)ans = min(ans, lengths[k - d] + edges);
    }
    for(int i = 0; i < cnt; ++i){
      pair<int, int> val = prov[i];
      int d = val.first;
      int edges = val.second;
      if(centroidToLength[d] != node) {
        lengths[d] = edges;
        centroidToLength[d] = node;
      }
      else lengths[d] = min(lengths[d], edges);
    }
    /*
    cout << node << " -> " << next.first << endl;
    for(int i = 1; i <= k; ++i) cout << lengths[i] << " ";
    cout << endl;*/
  }
  if(centroidToLength[k] == node) ans = min(ans, lengths[k]);
 
  exists[node] = 0;
 
  for(pair<int, int>& next: AdjList[node]){
    if(exists[next.first]) ans = min(ans, solveSubTree(next.first));
  }
  return ans;
}
 
int best_path(int N, int K, int H[][2], int L[])
{
  n = N;
  k = K;
  exists = vector<int>(N, 1);
  memset(centroidToLength, -1, sizeof(centroidToLength));
  int a, b, c;
  for(int i = 0; i < N-1; ++i){
    a = H[i][0];
    b = H[i][1];
    c = L[i];
    AdjList[a].push_back({b, c});
    AdjList[b].push_back({a, c});
  }
  /*
  for(int i = 0; i < N-1; ++i){
    cout << i << '\t';
    print(AdjList[i]);
  }*/
  int res = solveSubTree(0);
  if(res == 1e9) return -1;
  return res;
}
 
/*
int main(){
  int N, K;
  cin >> N >> K;
  int H[N-1][2];
  int L[N-1];
  for(int i = 0; i < N-1; ++i) cin >> H[i][0] >> H[i][1] >> L[i];
  cout << best_path(N, K, H, L);
  return 0;
}*/

Compilation message

race.cpp: In function 'int find_Centroid(int, int)':
race.cpp:38:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |   for(int i = 0; i < AdjList[node].size(); ++i){
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 3 ms 8916 KB Output is correct
3 Correct 4 ms 8916 KB Output is correct
4 Correct 4 ms 8916 KB Output is correct
5 Correct 4 ms 8916 KB Output is correct
6 Correct 4 ms 8916 KB Output is correct
7 Correct 3 ms 8916 KB Output is correct
8 Correct 3 ms 8916 KB Output is correct
9 Correct 3 ms 8916 KB Output is correct
10 Correct 3 ms 8916 KB Output is correct
11 Correct 4 ms 8916 KB Output is correct
12 Correct 4 ms 8916 KB Output is correct
13 Correct 4 ms 8916 KB Output is correct
14 Correct 4 ms 8916 KB Output is correct
15 Correct 3 ms 8900 KB Output is correct
16 Correct 4 ms 8864 KB Output is correct
17 Correct 4 ms 8916 KB Output is correct
18 Correct 4 ms 8896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 3 ms 8916 KB Output is correct
3 Correct 4 ms 8916 KB Output is correct
4 Correct 4 ms 8916 KB Output is correct
5 Correct 4 ms 8916 KB Output is correct
6 Correct 4 ms 8916 KB Output is correct
7 Correct 3 ms 8916 KB Output is correct
8 Correct 3 ms 8916 KB Output is correct
9 Correct 3 ms 8916 KB Output is correct
10 Correct 3 ms 8916 KB Output is correct
11 Correct 4 ms 8916 KB Output is correct
12 Correct 4 ms 8916 KB Output is correct
13 Correct 4 ms 8916 KB Output is correct
14 Correct 4 ms 8916 KB Output is correct
15 Correct 3 ms 8900 KB Output is correct
16 Correct 4 ms 8864 KB Output is correct
17 Correct 4 ms 8916 KB Output is correct
18 Correct 4 ms 8896 KB Output is correct
19 Correct 4 ms 8916 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
21 Correct 4 ms 8916 KB Output is correct
22 Correct 5 ms 11348 KB Output is correct
23 Correct 5 ms 11092 KB Output is correct
24 Correct 5 ms 11860 KB Output is correct
25 Correct 5 ms 10772 KB Output is correct
26 Correct 4 ms 10320 KB Output is correct
27 Correct 4 ms 8916 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 4 ms 10196 KB Output is correct
30 Correct 4 ms 10324 KB Output is correct
31 Correct 5 ms 10708 KB Output is correct
32 Correct 6 ms 10836 KB Output is correct
33 Correct 5 ms 11604 KB Output is correct
34 Correct 5 ms 11220 KB Output is correct
35 Correct 5 ms 11860 KB Output is correct
36 Correct 5 ms 12192 KB Output is correct
37 Correct 5 ms 10836 KB Output is correct
38 Correct 4 ms 10068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 3 ms 8916 KB Output is correct
3 Correct 4 ms 8916 KB Output is correct
4 Correct 4 ms 8916 KB Output is correct
5 Correct 4 ms 8916 KB Output is correct
6 Correct 4 ms 8916 KB Output is correct
7 Correct 3 ms 8916 KB Output is correct
8 Correct 3 ms 8916 KB Output is correct
9 Correct 3 ms 8916 KB Output is correct
10 Correct 3 ms 8916 KB Output is correct
11 Correct 4 ms 8916 KB Output is correct
12 Correct 4 ms 8916 KB Output is correct
13 Correct 4 ms 8916 KB Output is correct
14 Correct 4 ms 8916 KB Output is correct
15 Correct 3 ms 8900 KB Output is correct
16 Correct 4 ms 8864 KB Output is correct
17 Correct 4 ms 8916 KB Output is correct
18 Correct 4 ms 8896 KB Output is correct
19 Correct 80 ms 14956 KB Output is correct
20 Correct 73 ms 14980 KB Output is correct
21 Correct 76 ms 15144 KB Output is correct
22 Correct 73 ms 15272 KB Output is correct
23 Correct 51 ms 15152 KB Output is correct
24 Correct 52 ms 15072 KB Output is correct
25 Correct 85 ms 17720 KB Output is correct
26 Correct 65 ms 21084 KB Output is correct
27 Correct 107 ms 21096 KB Output is correct
28 Correct 170 ms 32244 KB Output is correct
29 Correct 201 ms 31352 KB Output is correct
30 Correct 100 ms 21172 KB Output is correct
31 Correct 109 ms 21068 KB Output is correct
32 Correct 152 ms 21172 KB Output is correct
33 Correct 135 ms 20016 KB Output is correct
34 Correct 121 ms 19980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 3 ms 8916 KB Output is correct
3 Correct 4 ms 8916 KB Output is correct
4 Correct 4 ms 8916 KB Output is correct
5 Correct 4 ms 8916 KB Output is correct
6 Correct 4 ms 8916 KB Output is correct
7 Correct 3 ms 8916 KB Output is correct
8 Correct 3 ms 8916 KB Output is correct
9 Correct 3 ms 8916 KB Output is correct
10 Correct 3 ms 8916 KB Output is correct
11 Correct 4 ms 8916 KB Output is correct
12 Correct 4 ms 8916 KB Output is correct
13 Correct 4 ms 8916 KB Output is correct
14 Correct 4 ms 8916 KB Output is correct
15 Correct 3 ms 8900 KB Output is correct
16 Correct 4 ms 8864 KB Output is correct
17 Correct 4 ms 8916 KB Output is correct
18 Correct 4 ms 8896 KB Output is correct
19 Correct 4 ms 8916 KB Output is correct
20 Correct 4 ms 8916 KB Output is correct
21 Correct 4 ms 8916 KB Output is correct
22 Correct 5 ms 11348 KB Output is correct
23 Correct 5 ms 11092 KB Output is correct
24 Correct 5 ms 11860 KB Output is correct
25 Correct 5 ms 10772 KB Output is correct
26 Correct 4 ms 10320 KB Output is correct
27 Correct 4 ms 8916 KB Output is correct
28 Correct 4 ms 9684 KB Output is correct
29 Correct 4 ms 10196 KB Output is correct
30 Correct 4 ms 10324 KB Output is correct
31 Correct 5 ms 10708 KB Output is correct
32 Correct 6 ms 10836 KB Output is correct
33 Correct 5 ms 11604 KB Output is correct
34 Correct 5 ms 11220 KB Output is correct
35 Correct 5 ms 11860 KB Output is correct
36 Correct 5 ms 12192 KB Output is correct
37 Correct 5 ms 10836 KB Output is correct
38 Correct 4 ms 10068 KB Output is correct
39 Correct 80 ms 14956 KB Output is correct
40 Correct 73 ms 14980 KB Output is correct
41 Correct 76 ms 15144 KB Output is correct
42 Correct 73 ms 15272 KB Output is correct
43 Correct 51 ms 15152 KB Output is correct
44 Correct 52 ms 15072 KB Output is correct
45 Correct 85 ms 17720 KB Output is correct
46 Correct 65 ms 21084 KB Output is correct
47 Correct 107 ms 21096 KB Output is correct
48 Correct 170 ms 32244 KB Output is correct
49 Correct 201 ms 31352 KB Output is correct
50 Correct 100 ms 21172 KB Output is correct
51 Correct 109 ms 21068 KB Output is correct
52 Correct 152 ms 21172 KB Output is correct
53 Correct 135 ms 20016 KB Output is correct
54 Correct 121 ms 19980 KB Output is correct
55 Correct 12 ms 9592 KB Output is correct
56 Correct 9 ms 9556 KB Output is correct
57 Correct 52 ms 15472 KB Output is correct
58 Correct 30 ms 15212 KB Output is correct
59 Correct 66 ms 21380 KB Output is correct
60 Correct 254 ms 34924 KB Output is correct
61 Correct 152 ms 22700 KB Output is correct
62 Correct 112 ms 22744 KB Output is correct
63 Correct 156 ms 22684 KB Output is correct
64 Correct 252 ms 24308 KB Output is correct
65 Correct 130 ms 22660 KB Output is correct
66 Correct 245 ms 34728 KB Output is correct
67 Correct 75 ms 22972 KB Output is correct
68 Correct 157 ms 27508 KB Output is correct
69 Correct 212 ms 27728 KB Output is correct
70 Correct 147 ms 26848 KB Output is correct