답안 #401486

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401486 2021-05-10T11:41:40 Z fvogel499 경주 (Race) (IOI11_race) C++17
21 / 100
3000 ms 26308 KB
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define siz 200000
#define pii pair<int, int>
#define ll long long
#define int ll

int n;
vector<int> graph [siz];
vector<pii> wGraph [siz];
bool isCentroid [siz];
int centroidParent [siz];
int subtreeSize [siz];
int parent [siz];
vector<vector<int>> centroidGraph;
int nodeAssignedVal [siz];

int getSubtreeSizes(int curNode) {
    int locNodes = 1;
    subtreeSize[curNode] = 1;
    for (int newNode : graph[curNode]) {
        if (newNode != parent[curNode] and !isCentroid[newNode]) {
            parent[newNode] = curNode;
            locNodes += getSubtreeSizes(newNode);
            subtreeSize[curNode] += subtreeSize[newNode];
        }
    }
    return locNodes;
}

int getCentroidSub(int curNode, int& totalNodes) {
    bool centroidOk = true;
    int counter = totalNodes-1;
    for (int newNode : graph[curNode]) {
        if (isCentroid[newNode] or parent[curNode] == newNode) continue;
        counter -= subtreeSize[newNode];
        if (subtreeSize[newNode] > (totalNodes)/2) {
            centroidOk = false;
            break;
        }
    }
    if ((parent[curNode] == -1 or counter <= (totalNodes)/2) and centroidOk) return curNode;
    for (int newNode : graph[curNode]) {
        if (newNode != parent[curNode] and !isCentroid[newNode]) {
            int res = getCentroidSub(newNode, totalNodes);
            if (res != -1) return res;
        }
    }
    return -1;
}

int getCentroid(int root) {
    parent[root] = -1;
    int totalNodes = getSubtreeSizes(root);
    int res = getCentroidSub(root, totalNodes);
    return res;
}

int generateCentroidSubtree(int curNode) {
    int centroidNode = getCentroid(curNode);
    isCentroid[centroidNode] = true;
    for (int newNode : graph[centroidNode]) {
        if (!isCentroid[newNode]) {
            centroidParent[generateCentroidSubtree(newNode)] = centroidNode;
        }
    }
    return centroidNode;
}

int centroidDecomp() {
    for (int i = 0; i < n; i++) {
        isCentroid[i] = false;
        centroidParent[i] = -1;
    }
    generateCentroidSubtree(0);
    centroidGraph = vector<vector<int>>(n);
    int root;
    for (int i = 0; i < n; i++) {
        if (centroidParent[i] != -1) centroidGraph[centroidParent[i]].push_back(i);
        else root = i;
    }
    return root;
} 

void dfs(unordered_map<int, unordered_map<int, int>>& perChild, int curNode, int parentNode, int subtreeNode, int forbiddenNode, int curDist, int edgesOnPath) {
    if (curNode == forbiddenNode) return;
    if (subtreeNode == -1 and parentNode != -1) subtreeNode = curNode;
    if (subtreeNode != -1) {
        if (perChild[subtreeNode].find(curDist) == perChild[subtreeNode].end()) perChild[subtreeNode][curDist] = n;
        perChild[subtreeNode][curDist] = min(perChild[subtreeNode][curDist], edgesOnPath);
        perChild[subtreeNode][0] = 0;
    }
    for (pii newEdge : wGraph[curNode]) {
        if (newEdge.first == parentNode) continue;
        dfs(perChild, newEdge.first, curNode, subtreeNode, forbiddenNode, curDist+newEdge.second, edgesOnPath+1);
    }
}

int best_path(int32_t ln, int32_t kval, int32_t edges [siz-1][2], int32_t edgeLen [siz-1]) {
    n = ln;

    for (int i = 0; i < n-1; i++) {
        graph[edges[i][0]].push_back(edges[i][1]);
        graph[edges[i][1]].push_back(edges[i][0]);
        wGraph[edges[i][0]].push_back(pii(edges[i][1], edgeLen[i]));
        wGraph[edges[i][1]].push_back(pii(edges[i][0], edgeLen[i]));
    }

    centroidDecomp();

    int minEdgesInPath = n;

    for (int i = 0; i < n; i++) {
        unordered_map<int, unordered_map<int, int>> perChild;
        dfs(perChild, i, -1, -1, centroidParent[i], 0, 0);
        unordered_map<int, multiset<int>> s;
        for (pair<int, unordered_map<int, int>> j : perChild) {
            for (pii k : j.second) s[k.first].insert(k.second);
        }
        for (pair<int, unordered_map<int, int>> j : perChild) {
            for (pii k : j.second) s[k.first].erase(s[k.first].find(k.second));
            for (pii k : j.second) {
                if (s.find(kval-k.first) != s.end() and !s[kval-k.first].empty()) {
                    minEdgesInPath = min(minEdgesInPath, (*(s[kval-k.first].begin()))+k.second);
                }
            }
            for (pii k : j.second) s[k.first].insert(k.second);
        }
    }

    if (minEdgesInPath == n) minEdgesInPath = -1;

    return minEdgesInPath;
}

// int H[siz-1][2];
// int L[siz-1];

// int32_t main() {
//     int N, K;
//     cin >> N >> K;
//     for (int i = 0; i < N-1; i++) cin >> H[i][0] >> H[i][1];
//     for (int i = 0; i < N-1; i++) cin >> L[i];
//     cout << best_path(N, K, H, L);
// }

Compilation message

race.cpp: In function 'long long int centroidDecomp()':
race.cpp:89:12: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
   89 |     return root;
      |            ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9676 KB Output is correct
2 Correct 9 ms 9676 KB Output is correct
3 Correct 9 ms 9704 KB Output is correct
4 Correct 6 ms 9680 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 9 ms 9676 KB Output is correct
7 Correct 9 ms 9720 KB Output is correct
8 Correct 9 ms 9676 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 8 ms 9676 KB Output is correct
11 Correct 8 ms 9676 KB Output is correct
12 Correct 8 ms 9784 KB Output is correct
13 Correct 8 ms 9772 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9780 KB Output is correct
17 Correct 8 ms 9672 KB Output is correct
18 Correct 8 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9676 KB Output is correct
2 Correct 9 ms 9676 KB Output is correct
3 Correct 9 ms 9704 KB Output is correct
4 Correct 6 ms 9680 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 9 ms 9676 KB Output is correct
7 Correct 9 ms 9720 KB Output is correct
8 Correct 9 ms 9676 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 8 ms 9676 KB Output is correct
11 Correct 8 ms 9676 KB Output is correct
12 Correct 8 ms 9784 KB Output is correct
13 Correct 8 ms 9772 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9780 KB Output is correct
17 Correct 8 ms 9672 KB Output is correct
18 Correct 8 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 50 ms 9932 KB Output is correct
22 Correct 164 ms 10136 KB Output is correct
23 Correct 174 ms 10136 KB Output is correct
24 Correct 109 ms 10128 KB Output is correct
25 Correct 100 ms 10136 KB Output is correct
26 Correct 104 ms 10060 KB Output is correct
27 Correct 25 ms 9888 KB Output is correct
28 Correct 101 ms 10120 KB Output is correct
29 Correct 105 ms 10136 KB Output is correct
30 Correct 100 ms 10060 KB Output is correct
31 Correct 102 ms 10140 KB Output is correct
32 Correct 103 ms 10152 KB Output is correct
33 Correct 162 ms 10136 KB Output is correct
34 Correct 275 ms 10104 KB Output is correct
35 Correct 272 ms 10060 KB Output is correct
36 Correct 226 ms 10060 KB Output is correct
37 Correct 217 ms 10072 KB Output is correct
38 Correct 267 ms 10124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9676 KB Output is correct
2 Correct 9 ms 9676 KB Output is correct
3 Correct 9 ms 9704 KB Output is correct
4 Correct 6 ms 9680 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 9 ms 9676 KB Output is correct
7 Correct 9 ms 9720 KB Output is correct
8 Correct 9 ms 9676 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 8 ms 9676 KB Output is correct
11 Correct 8 ms 9676 KB Output is correct
12 Correct 8 ms 9784 KB Output is correct
13 Correct 8 ms 9772 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9780 KB Output is correct
17 Correct 8 ms 9672 KB Output is correct
18 Correct 8 ms 9676 KB Output is correct
19 Execution timed out 3060 ms 26308 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 9676 KB Output is correct
2 Correct 9 ms 9676 KB Output is correct
3 Correct 9 ms 9704 KB Output is correct
4 Correct 6 ms 9680 KB Output is correct
5 Correct 8 ms 9676 KB Output is correct
6 Correct 9 ms 9676 KB Output is correct
7 Correct 9 ms 9720 KB Output is correct
8 Correct 9 ms 9676 KB Output is correct
9 Correct 8 ms 9676 KB Output is correct
10 Correct 8 ms 9676 KB Output is correct
11 Correct 8 ms 9676 KB Output is correct
12 Correct 8 ms 9784 KB Output is correct
13 Correct 8 ms 9772 KB Output is correct
14 Correct 9 ms 9812 KB Output is correct
15 Correct 8 ms 9676 KB Output is correct
16 Correct 8 ms 9780 KB Output is correct
17 Correct 8 ms 9672 KB Output is correct
18 Correct 8 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 6 ms 9676 KB Output is correct
21 Correct 50 ms 9932 KB Output is correct
22 Correct 164 ms 10136 KB Output is correct
23 Correct 174 ms 10136 KB Output is correct
24 Correct 109 ms 10128 KB Output is correct
25 Correct 100 ms 10136 KB Output is correct
26 Correct 104 ms 10060 KB Output is correct
27 Correct 25 ms 9888 KB Output is correct
28 Correct 101 ms 10120 KB Output is correct
29 Correct 105 ms 10136 KB Output is correct
30 Correct 100 ms 10060 KB Output is correct
31 Correct 102 ms 10140 KB Output is correct
32 Correct 103 ms 10152 KB Output is correct
33 Correct 162 ms 10136 KB Output is correct
34 Correct 275 ms 10104 KB Output is correct
35 Correct 272 ms 10060 KB Output is correct
36 Correct 226 ms 10060 KB Output is correct
37 Correct 217 ms 10072 KB Output is correct
38 Correct 267 ms 10124 KB Output is correct
39 Execution timed out 3060 ms 26308 KB Time limit exceeded
40 Halted 0 ms 0 KB -