답안 #528170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
528170 2022-02-19T14:48:08 Z LFFB 경주 (Race) (IOI11_race) C++17
100 / 100
927 ms 64664 KB
#include <iostream>
#include <vector>
#include <string>

#define debug(args...) //printf(args)

typedef std::pair<int, int> intPair;

#define forAllChild(node, code) for (intPair childPair : neighbors[node]) {\
    int child = childPair.first;\
    int childWeight = childPair.second;\
    if (child == parent.get(node)) continue;\
    if (removed.get(child)) continue;\
    code;\
}

const int MAX_N = 1000000 + 10;
const int INF = 1000000000;

template<typename T>
class SpecialVector {

    private:
        int time;
        T values[MAX_N];
        T standard;
        int lastChanged[MAX_N];

    public:
        SpecialVector(T val) {
            time = 1;
            standard = val;
        }

        T get(int i) {
            if (i < 0 || i >= MAX_N) return standard;
            if (lastChanged[i] == time) return values[i];
            return standard;
        }

        void set(int i, T v) {
            if (i < 0 || i >= MAX_N) return;
            values[i] = v;
            lastChanged[i] = time;
        }

        bool defined(int i) {
            if (i < 0 || i >= MAX_N) return false;
            return lastChanged[i] == time;
        }

        void clear() {
            time++;
        }

};

int n;
int k;

std::vector<intPair> neighbors[MAX_N];

SpecialVector<int> parent(-1);
SpecialVector<int> parentWeight(-1);

SpecialVector<bool> removed(false);

SpecialVector<int> lengths(-1);

SpecialVector<int> lengthMap(-1);

namespace CentroidDecomposition {

    int findBetterCentroid(int node, int root) {
        forAllChild(node, {
            if (lengths.get(child) > lengths.get(root)/2) return child;
        });
        return -1;
    }

    int findCentroid(int root) {
        int node = root;
        while (findBetterCentroid(node, root) != -1) {
            debug("found better centroid for %d (root: %d)\n", node, root);
            node = findBetterCentroid(node, root);
        }
        debug("root %d, found centroid %d\n", root, node);
        return node;
    }

}

void setMin(int &a, int b) {
    a = std::min(a, b);
}

void findParents(int node) {
    lengths.set(node, 1);
    forAllChild(node, {
        parent.set(child, node);
        parentWeight.set(child, childWeight);
        findParents(child);
        lengths.set(node, lengths.get(node) + lengths.get(child));
    });
}

int getMin(int node, int depth, int distance) {
    int minimum;
    if (lengthMap.defined(k - distance)) {
        minimum = depth + lengthMap.get(k - distance);
    } else {
        minimum = INF;
    }

    forAllChild(node, {
        int childMinumum = getMin(child, depth + 1, distance + childWeight);
        setMin(minimum, childMinumum);
    });

    return minimum;
}

void addToMapping(int node, int depth, int distance) {

    if (lengthMap.get(distance) > depth || !lengthMap.defined(distance)) {
        lengthMap.set(distance, depth);
    }

    forAllChild(node, {
        addToMapping(child, depth + 1, distance + childWeight);
    });

}

int getMinimumLength(int root) {

    parent.clear();
    lengths.clear();
    findParents(root);

    int centroid = CentroidDecomposition::findCentroid(root);
    removed.set(centroid, true);

    lengthMap.clear();
    lengthMap.set(0, 0);
    lengths.clear();
    parent.clear();

    int minimum = INF;

    for (intPair neighborPair : neighbors[centroid]) {
        int neighbor = neighborPair.first;
        int neighborWeight = neighborPair.second;
        if (removed.get(neighbor)) continue;

        findParents(neighbor);

        int neighborMinimum = getMin(neighbor, 1, neighborWeight);
        setMin(minimum, neighborMinimum);

        addToMapping(neighbor, 1, neighborWeight);
    }

    for (intPair neighborPair : neighbors[centroid]) {
        int neighbor = neighborPair.first;
        int neighborWeight = neighborPair.second;
        if (removed.get(neighbor)) continue;

        int neighborMinimum = getMinimumLength(neighbor);
        setMin(minimum, neighborMinimum);
    }

    debug("root %d, centroid %d, returning %d\n", root, centroid, minimum);
    return minimum;
}

void printRepeated(std::string s, int x) {
    for (int i = 0; i < x; i++) printf("%s", s.c_str());
}
void printTree(int node, int depth=0) {
    printRepeated("- ", depth);
    printf("%d\n", node);
    forAllChild(node, {
        printTree(child, depth + 1);
    });
}

int best_path(int N, int K, int H[][2], int L[]) {
    n = N;
    k = K;
    for (int i = 0; i < n - 1; i++) {
        int a = H[i][0];
        int b = H[i][1];
        int w = L[i];
        neighbors[a].push_back(std::make_pair(b, w));
        neighbors[b].push_back(std::make_pair(a, w));
    }
    //findParents(0);
    //printTree(0);
    int answer = getMinimumLength(0);
    if (answer == INF) answer = -1;
    return answer;
}

Compilation message

race.cpp: In function 'int CentroidDecomposition::findBetterCentroid(int, int)':
race.cpp:12:9: warning: unused variable 'childWeight' [-Wunused-variable]
   12 |     int childWeight = childPair.second;\
      |         ^~~~~~~~~~~
race.cpp:76:9: note: in expansion of macro 'forAllChild'
   76 |         forAllChild(node, {
      |         ^~~~~~~~~~~
race.cpp: In function 'int getMinimumLength(int)':
race.cpp:167:13: warning: unused variable 'neighborWeight' [-Wunused-variable]
  167 |         int neighborWeight = neighborPair.second;
      |             ^~~~~~~~~~~~~~
race.cpp: In function 'void printTree(int, int)':
race.cpp:12:9: warning: unused variable 'childWeight' [-Wunused-variable]
   12 |     int childWeight = childPair.second;\
      |         ^~~~~~~~~~~
race.cpp:184:5: note: in expansion of macro 'forAllChild'
  184 |     forAllChild(node, {
      |     ^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23856 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23792 KB Output is correct
4 Correct 13 ms 23884 KB Output is correct
5 Correct 15 ms 23768 KB Output is correct
6 Correct 15 ms 23856 KB Output is correct
7 Correct 13 ms 23884 KB Output is correct
8 Correct 14 ms 23908 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 12 ms 23836 KB Output is correct
12 Correct 12 ms 23848 KB Output is correct
13 Correct 15 ms 23800 KB Output is correct
14 Correct 14 ms 23848 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Correct 14 ms 23884 KB Output is correct
17 Correct 13 ms 23808 KB Output is correct
18 Correct 13 ms 23888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23856 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23792 KB Output is correct
4 Correct 13 ms 23884 KB Output is correct
5 Correct 15 ms 23768 KB Output is correct
6 Correct 15 ms 23856 KB Output is correct
7 Correct 13 ms 23884 KB Output is correct
8 Correct 14 ms 23908 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 12 ms 23836 KB Output is correct
12 Correct 12 ms 23848 KB Output is correct
13 Correct 15 ms 23800 KB Output is correct
14 Correct 14 ms 23848 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Correct 14 ms 23884 KB Output is correct
17 Correct 13 ms 23808 KB Output is correct
18 Correct 13 ms 23888 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 16 ms 23884 KB Output is correct
21 Correct 14 ms 23944 KB Output is correct
22 Correct 16 ms 29424 KB Output is correct
23 Correct 16 ms 29836 KB Output is correct
24 Correct 17 ms 30496 KB Output is correct
25 Correct 16 ms 27456 KB Output is correct
26 Correct 17 ms 31436 KB Output is correct
27 Correct 13 ms 23884 KB Output is correct
28 Correct 15 ms 26812 KB Output is correct
29 Correct 15 ms 27408 KB Output is correct
30 Correct 15 ms 27264 KB Output is correct
31 Correct 15 ms 27452 KB Output is correct
32 Correct 16 ms 27572 KB Output is correct
33 Correct 16 ms 29172 KB Output is correct
34 Correct 17 ms 30972 KB Output is correct
35 Correct 18 ms 30776 KB Output is correct
36 Correct 18 ms 30760 KB Output is correct
37 Correct 15 ms 27696 KB Output is correct
38 Correct 14 ms 26156 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23856 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23792 KB Output is correct
4 Correct 13 ms 23884 KB Output is correct
5 Correct 15 ms 23768 KB Output is correct
6 Correct 15 ms 23856 KB Output is correct
7 Correct 13 ms 23884 KB Output is correct
8 Correct 14 ms 23908 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 12 ms 23836 KB Output is correct
12 Correct 12 ms 23848 KB Output is correct
13 Correct 15 ms 23800 KB Output is correct
14 Correct 14 ms 23848 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Correct 14 ms 23884 KB Output is correct
17 Correct 13 ms 23808 KB Output is correct
18 Correct 13 ms 23888 KB Output is correct
19 Correct 235 ms 32808 KB Output is correct
20 Correct 238 ms 32836 KB Output is correct
21 Correct 217 ms 32892 KB Output is correct
22 Correct 210 ms 33180 KB Output is correct
23 Correct 196 ms 33224 KB Output is correct
24 Correct 91 ms 33076 KB Output is correct
25 Correct 264 ms 36960 KB Output is correct
26 Correct 127 ms 40132 KB Output is correct
27 Correct 248 ms 42292 KB Output is correct
28 Correct 926 ms 64664 KB Output is correct
29 Correct 916 ms 63488 KB Output is correct
30 Correct 233 ms 42344 KB Output is correct
31 Correct 244 ms 42280 KB Output is correct
32 Correct 355 ms 42476 KB Output is correct
33 Correct 524 ms 41284 KB Output is correct
34 Correct 571 ms 49900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 23856 KB Output is correct
2 Correct 13 ms 23756 KB Output is correct
3 Correct 13 ms 23792 KB Output is correct
4 Correct 13 ms 23884 KB Output is correct
5 Correct 15 ms 23768 KB Output is correct
6 Correct 15 ms 23856 KB Output is correct
7 Correct 13 ms 23884 KB Output is correct
8 Correct 14 ms 23908 KB Output is correct
9 Correct 13 ms 23884 KB Output is correct
10 Correct 13 ms 23884 KB Output is correct
11 Correct 12 ms 23836 KB Output is correct
12 Correct 12 ms 23848 KB Output is correct
13 Correct 15 ms 23800 KB Output is correct
14 Correct 14 ms 23848 KB Output is correct
15 Correct 13 ms 23856 KB Output is correct
16 Correct 14 ms 23884 KB Output is correct
17 Correct 13 ms 23808 KB Output is correct
18 Correct 13 ms 23888 KB Output is correct
19 Correct 12 ms 23756 KB Output is correct
20 Correct 16 ms 23884 KB Output is correct
21 Correct 14 ms 23944 KB Output is correct
22 Correct 16 ms 29424 KB Output is correct
23 Correct 16 ms 29836 KB Output is correct
24 Correct 17 ms 30496 KB Output is correct
25 Correct 16 ms 27456 KB Output is correct
26 Correct 17 ms 31436 KB Output is correct
27 Correct 13 ms 23884 KB Output is correct
28 Correct 15 ms 26812 KB Output is correct
29 Correct 15 ms 27408 KB Output is correct
30 Correct 15 ms 27264 KB Output is correct
31 Correct 15 ms 27452 KB Output is correct
32 Correct 16 ms 27572 KB Output is correct
33 Correct 16 ms 29172 KB Output is correct
34 Correct 17 ms 30972 KB Output is correct
35 Correct 18 ms 30776 KB Output is correct
36 Correct 18 ms 30760 KB Output is correct
37 Correct 15 ms 27696 KB Output is correct
38 Correct 14 ms 26156 KB Output is correct
39 Correct 235 ms 32808 KB Output is correct
40 Correct 238 ms 32836 KB Output is correct
41 Correct 217 ms 32892 KB Output is correct
42 Correct 210 ms 33180 KB Output is correct
43 Correct 196 ms 33224 KB Output is correct
44 Correct 91 ms 33076 KB Output is correct
45 Correct 264 ms 36960 KB Output is correct
46 Correct 127 ms 40132 KB Output is correct
47 Correct 248 ms 42292 KB Output is correct
48 Correct 926 ms 64664 KB Output is correct
49 Correct 916 ms 63488 KB Output is correct
50 Correct 233 ms 42344 KB Output is correct
51 Correct 244 ms 42280 KB Output is correct
52 Correct 355 ms 42476 KB Output is correct
53 Correct 524 ms 41284 KB Output is correct
54 Correct 571 ms 49900 KB Output is correct
55 Correct 22 ms 24908 KB Output is correct
56 Correct 23 ms 24632 KB Output is correct
57 Correct 141 ms 33028 KB Output is correct
58 Correct 46 ms 32824 KB Output is correct
59 Correct 129 ms 40856 KB Output is correct
60 Correct 850 ms 60620 KB Output is correct
61 Correct 238 ms 42692 KB Output is correct
62 Correct 216 ms 42324 KB Output is correct
63 Correct 359 ms 42460 KB Output is correct
64 Correct 753 ms 44988 KB Output is correct
65 Correct 590 ms 51132 KB Output is correct
66 Correct 927 ms 61108 KB Output is correct
67 Correct 130 ms 43028 KB Output is correct
68 Correct 380 ms 48468 KB Output is correct
69 Correct 360 ms 48708 KB Output is correct
70 Correct 398 ms 47636 KB Output is correct