답안 #760333

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
760333 2023-06-17T12:54:55 Z Andrei_Calota 경주 (Race) (IOI11_race) C++14
100 / 100
1818 ms 108568 KB
#include "race.h"
#include <bits/stdc++.h>


using namespace std;
using int64 = long long;

const int N_MAX = 2e5;
const int K_MAX = 1e6;



struct defEdge {
   int from, to, cost;
}; vector<defEdge> tree[1 + N_MAX];
int n, k; int sizOf[1 + N_MAX];
int64 preffSum[1 + N_MAX]; int depth[1 + N_MAX];

void auxDFS (int root, int p, int cost)  {
    preffSum[root] = preffSum[p] + cost;
    depth[root] = depth[p] + 1;

    for (defEdge e : tree[root])
       if (e.to != p)
         auxDFS (e.to, root, e.cost);
    sizOf[root] = 1;
    for (defEdge e : tree[root])
       sizOf[root] += sizOf[e.to];
}

const int NONE = -1;

map<int64, int> minDist; /// minDist[k] = numarul minim de muchii cu distanta k
vector<int> subTree[1 + N_MAX];
int answer[1 + N_MAX]; /// answer[root] = numarul minim de muchii care trec prin root cu suma k

void DFS (int root, int p, bool toEliminate) {
    int maxSize = 0, heavyChild = NONE;
    for (defEdge e : tree[root])
       if (e.to != p && sizOf[e.to] > maxSize)
         maxSize = sizOf[e.to], heavyChild = e.to;

    for (defEdge e : tree[root])
       if (e.to != p && e.to != heavyChild)
         DFS (e.to, root, true);

    if (heavyChild != NONE) {
      DFS (heavyChild, root, false);
      swap (subTree[root], subTree[heavyChild]);
    }

    int64 target = k + 2 * preffSum[root];

    subTree[root].push_back (root);
    minDist[preffSum[root]] = depth[root];

    if (minDist[target - preffSum[root]])
      answer[root] = minDist[target - preffSum[root]] - minDist[preffSum[root]];

    for (defEdge e : tree[root]) {
       if (e.to != p && e.to != heavyChild) {
         for (int aux : subTree[e.to])
            if (minDist[target - preffSum[aux]])
              answer[root] = min (answer[root], minDist[target - preffSum[aux]] + depth[aux] - 2 * depth[root]);
         for (int aux : subTree[e.to]) {
            subTree[root].push_back (aux);
            if (!minDist[preffSum[aux]] || minDist[preffSum[aux]] > depth[aux])
              minDist[preffSum[aux]] = depth[aux];
         }
       }
    }

    if (toEliminate)
      for (int aux : subTree[root])
         minDist[preffSum[aux]] = 0;
}

const int PAIR = 2;
int best_path (int N, int K, int H[][PAIR], int L[]) {
   n = N, k = K;
   for (int i = 0; i < n - 1; i ++) {
      int u = H[i][0], v = H[i][1]; u ++, v ++;
      tree[u].push_back ({u, v, L[i]});
      tree[v].push_back ({v, u, L[i]});
   }
   depth[0] = NONE, preffSum[0] = 0; auxDFS (1, 0, 0);
   for (int node = 1; node <= n; node ++)
      answer[node] = 1 + N_MAX;
   DFS (1, 0, true);

   int miny = answer[1];
   for (int node = 2; node <= n; node ++)
      miny = min (miny, answer[node]);

   if (miny == 1 + N_MAX)
     return NONE;
   return miny;
}

/**void TestOut (void) {
    cin >> n >> k;
    for (int i = 1; i < n; i ++) {
       int u, v, cost; cin >> u >> v >> cost; u ++, v ++;
       tree[u].push_back ({u, v, cost});
       tree[v].push_back ({v, u, cost});
    }
    depth[0] = NONE, preffSum[0] = 0; auxDFS (1, 0, 0);
    for (int node = 1; node <= n; node ++)
       cout << depth[node] << " " << preffSum[node] << "\n";

    for (int node = 1; node <= n; node ++)
       answer[node] = 1 + N_MAX;
    DFS (1, 0, true);

    int miny = answer[1];
   for (int node = 2; node <= n; node ++)
      miny = min (miny, answer[node]);

   if (miny == 1 + N_MAX)
      cout << NONE;
   else
      cout << miny;
}**/

/**int main()
{
  /// TestOut ();
    return 0;
}**/
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9708 KB Output is correct
3 Correct 5 ms 9768 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9756 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 4 ms 9752 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9712 KB Output is correct
11 Correct 5 ms 9708 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 6 ms 9736 KB Output is correct
18 Correct 6 ms 9704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9708 KB Output is correct
3 Correct 5 ms 9768 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9756 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 4 ms 9752 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9712 KB Output is correct
11 Correct 5 ms 9708 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 6 ms 9736 KB Output is correct
18 Correct 6 ms 9704 KB Output is correct
19 Correct 5 ms 9708 KB Output is correct
20 Correct 4 ms 9712 KB Output is correct
21 Correct 5 ms 9844 KB Output is correct
22 Correct 6 ms 10104 KB Output is correct
23 Correct 6 ms 10068 KB Output is correct
24 Correct 6 ms 10068 KB Output is correct
25 Correct 7 ms 10068 KB Output is correct
26 Correct 6 ms 10068 KB Output is correct
27 Correct 6 ms 9852 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10068 KB Output is correct
30 Correct 6 ms 10108 KB Output is correct
31 Correct 7 ms 10108 KB Output is correct
32 Correct 7 ms 10068 KB Output is correct
33 Correct 6 ms 10104 KB Output is correct
34 Correct 6 ms 9940 KB Output is correct
35 Correct 6 ms 9940 KB Output is correct
36 Correct 5 ms 9940 KB Output is correct
37 Correct 5 ms 9964 KB Output is correct
38 Correct 6 ms 9940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9708 KB Output is correct
3 Correct 5 ms 9768 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9756 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 4 ms 9752 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9712 KB Output is correct
11 Correct 5 ms 9708 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 6 ms 9736 KB Output is correct
18 Correct 6 ms 9704 KB Output is correct
19 Correct 145 ms 22288 KB Output is correct
20 Correct 175 ms 22212 KB Output is correct
21 Correct 130 ms 22344 KB Output is correct
22 Correct 140 ms 22252 KB Output is correct
23 Correct 216 ms 23608 KB Output is correct
24 Correct 151 ms 23496 KB Output is correct
25 Correct 85 ms 27712 KB Output is correct
26 Correct 48 ms 35108 KB Output is correct
27 Correct 207 ms 32840 KB Output is correct
28 Correct 236 ms 85476 KB Output is correct
29 Correct 200 ms 83780 KB Output is correct
30 Correct 189 ms 32796 KB Output is correct
31 Correct 189 ms 32800 KB Output is correct
32 Correct 228 ms 33048 KB Output is correct
33 Correct 242 ms 29656 KB Output is correct
34 Correct 1055 ms 86464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9708 KB Output is correct
3 Correct 5 ms 9768 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 5 ms 9756 KB Output is correct
6 Correct 5 ms 9716 KB Output is correct
7 Correct 4 ms 9752 KB Output is correct
8 Correct 5 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9712 KB Output is correct
11 Correct 5 ms 9708 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 5 ms 9684 KB Output is correct
17 Correct 6 ms 9736 KB Output is correct
18 Correct 6 ms 9704 KB Output is correct
19 Correct 5 ms 9708 KB Output is correct
20 Correct 4 ms 9712 KB Output is correct
21 Correct 5 ms 9844 KB Output is correct
22 Correct 6 ms 10104 KB Output is correct
23 Correct 6 ms 10068 KB Output is correct
24 Correct 6 ms 10068 KB Output is correct
25 Correct 7 ms 10068 KB Output is correct
26 Correct 6 ms 10068 KB Output is correct
27 Correct 6 ms 9852 KB Output is correct
28 Correct 6 ms 10068 KB Output is correct
29 Correct 6 ms 10068 KB Output is correct
30 Correct 6 ms 10108 KB Output is correct
31 Correct 7 ms 10108 KB Output is correct
32 Correct 7 ms 10068 KB Output is correct
33 Correct 6 ms 10104 KB Output is correct
34 Correct 6 ms 9940 KB Output is correct
35 Correct 6 ms 9940 KB Output is correct
36 Correct 5 ms 9940 KB Output is correct
37 Correct 5 ms 9964 KB Output is correct
38 Correct 6 ms 9940 KB Output is correct
39 Correct 145 ms 22288 KB Output is correct
40 Correct 175 ms 22212 KB Output is correct
41 Correct 130 ms 22344 KB Output is correct
42 Correct 140 ms 22252 KB Output is correct
43 Correct 216 ms 23608 KB Output is correct
44 Correct 151 ms 23496 KB Output is correct
45 Correct 85 ms 27712 KB Output is correct
46 Correct 48 ms 35108 KB Output is correct
47 Correct 207 ms 32840 KB Output is correct
48 Correct 236 ms 85476 KB Output is correct
49 Correct 200 ms 83780 KB Output is correct
50 Correct 189 ms 32796 KB Output is correct
51 Correct 189 ms 32800 KB Output is correct
52 Correct 228 ms 33048 KB Output is correct
53 Correct 242 ms 29656 KB Output is correct
54 Correct 1055 ms 86464 KB Output is correct
55 Correct 23 ms 11732 KB Output is correct
56 Correct 13 ms 10964 KB Output is correct
57 Correct 98 ms 23420 KB Output is correct
58 Correct 54 ms 22268 KB Output is correct
59 Correct 96 ms 47152 KB Output is correct
60 Correct 238 ms 83628 KB Output is correct
61 Correct 274 ms 36184 KB Output is correct
62 Correct 174 ms 32988 KB Output is correct
63 Correct 281 ms 33076 KB Output is correct
64 Correct 1309 ms 63152 KB Output is correct
65 Correct 1818 ms 108568 KB Output is correct
66 Correct 243 ms 80496 KB Output is correct
67 Correct 170 ms 35940 KB Output is correct
68 Correct 793 ms 70464 KB Output is correct
69 Correct 823 ms 71384 KB Output is correct
70 Correct 820 ms 68136 KB Output is correct