Submission #552807

# Submission time Handle Problem Language Result Execution time Memory
552807 2022-04-24T03:49:03 Z Olympia Race (IOI11_race) C++17
100 / 100
1881 ms 74004 KB
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <cassert>
#include <climits>
#include "race.h"
using namespace std;
class Tree {
public:
    vector<vector<int>> adj;
    map<pair<int,int>,int64_t> weight;
    vector<bool> hasVisited;
    vector<int> sub;
    vector<int> parent;
    vector<int> gw;
    int sz = 0;
    void prelim (int curNode, int prevNode) {
        parent[curNode] = prevNode;
        gw[curNode] = weight[make_pair(curNode, prevNode)];
        for (int i: adj[curNode]) {
            if (i != prevNode) {
                prelim(i, curNode);
            }
        }
    }
    int get_weight (int curNode, int prevNode) {
        if (parent[curNode] == prevNode) {
            return gw[curNode];
        } else if (parent[prevNode] == curNode){
            return gw[prevNode];
        }
      assert(false);
    }
    int dfs_for_sz (int curNode, int prevNode) {
        sub[curNode] = 1;
        for (int i: adj[curNode]) {
            if (i != prevNode && !hasVisited[i]) {
                sub[curNode] += dfs_for_sz (i, curNode);
            }
        }
        return (sz = sub[curNode]);
    }
    int find_centroid (int curNode, int prevNode) {
        for (int i: adj[curNode]) {
            if (sub[i] * 2 > sz && !hasVisited[i] && i != prevNode) {
                return find_centroid(i, curNode);
            }
        }
        return curNode;
    }
    map<int64_t,int> tot, exc;
    void dfs (int curNode, int prevNode, int64_t w, int d) {
        if (exc.count(w)) exc[w] = min(exc[w], d);
        else exc[w] = d;
        for (int i: adj[curNode]) {
            if (i != prevNode && !hasVisited[i]) {
                dfs (i, curNode, w + get_weight(i, curNode), d + 1);
            }
        }
    }
    int ans = INT_MAX;
    int64_t k;
    void solve (int curNode) {
        dfs_for_sz(curNode, curNode);
        int centroid = find_centroid(curNode, curNode);
        hasVisited[centroid] = true;
        //Two Cases:
        //(1) It passes through the centroid
        //(2) It doesn't pass through the centroid
        for (int i: adj[centroid]) {
            if (!hasVisited[i]) {
                dfs (i, i, get_weight(i, centroid), 1);
                for (auto& p: exc) {
                    if (tot.count(k - p.first)) {
                        ans = min(ans, tot[k - p.first] + p.second);
                    }
                }
                for (auto& p: exc) {
                    if (tot.count(p.first)) tot[p.first] = min(tot[p.first], p.second);
                    else tot[p.first] = p.second;
                }
                exc.clear();
            }
        }
        if (tot.count(k)) {
            ans = min(ans, tot[k]);
        }
        tot.clear();
        for (int i: adj[centroid]) {
            if (!hasVisited[i]) {
                solve (i);
            }
        }
    }
    void add_weight (int u, int v, int64_t w) {
        weight[make_pair(u, v)] = weight[make_pair(v, u)] = w;
        adj[u].push_back(v), adj[v].push_back(u);
    }
    Tree (int n, int k) {
        adj.resize(n), hasVisited.assign(n, false), sub.assign(n, false), gw.resize(n), parent.resize(n);
        this->k = k;
    }
};
int best_path(int n, int k, int H[][2], int L[]){
    Tree myTree(n, k);
    for (int i = 0; i < n - 1; i++) {
        myTree.add_weight(H[i][0], H[i][1], L[i]);
    }
    myTree.prelim(0, 0);
    myTree.solve(0);
    if (myTree.ans == INT_MAX) return -1;
    return myTree.ans;
}
/*
int main() {
    
}
*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 3 ms 612 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 3 ms 468 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 3 ms 596 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 3 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 429 ms 20804 KB Output is correct
20 Correct 410 ms 20788 KB Output is correct
21 Correct 411 ms 20684 KB Output is correct
22 Correct 511 ms 20808 KB Output is correct
23 Correct 609 ms 20784 KB Output is correct
24 Correct 367 ms 20888 KB Output is correct
25 Correct 444 ms 26840 KB Output is correct
26 Correct 145 ms 26112 KB Output is correct
27 Correct 706 ms 41780 KB Output is correct
28 Correct 1837 ms 70664 KB Output is correct
29 Correct 1819 ms 73848 KB Output is correct
30 Correct 700 ms 44680 KB Output is correct
31 Correct 782 ms 44644 KB Output is correct
32 Correct 1016 ms 44788 KB Output is correct
33 Correct 1248 ms 44312 KB Output is correct
34 Correct 1781 ms 60384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 468 KB Output is correct
22 Correct 3 ms 468 KB Output is correct
23 Correct 3 ms 612 KB Output is correct
24 Correct 3 ms 596 KB Output is correct
25 Correct 3 ms 468 KB Output is correct
26 Correct 3 ms 468 KB Output is correct
27 Correct 3 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 3 ms 596 KB Output is correct
31 Correct 3 ms 468 KB Output is correct
32 Correct 3 ms 596 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 3 ms 596 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 3 ms 468 KB Output is correct
37 Correct 2 ms 468 KB Output is correct
38 Correct 3 ms 468 KB Output is correct
39 Correct 429 ms 20804 KB Output is correct
40 Correct 410 ms 20788 KB Output is correct
41 Correct 411 ms 20684 KB Output is correct
42 Correct 511 ms 20808 KB Output is correct
43 Correct 609 ms 20784 KB Output is correct
44 Correct 367 ms 20888 KB Output is correct
45 Correct 444 ms 26840 KB Output is correct
46 Correct 145 ms 26112 KB Output is correct
47 Correct 706 ms 41780 KB Output is correct
48 Correct 1837 ms 70664 KB Output is correct
49 Correct 1819 ms 73848 KB Output is correct
50 Correct 700 ms 44680 KB Output is correct
51 Correct 782 ms 44644 KB Output is correct
52 Correct 1016 ms 44788 KB Output is correct
53 Correct 1248 ms 44312 KB Output is correct
54 Correct 1781 ms 60384 KB Output is correct
55 Correct 38 ms 2900 KB Output is correct
56 Correct 21 ms 2368 KB Output is correct
57 Correct 219 ms 22020 KB Output is correct
58 Correct 132 ms 22172 KB Output is correct
59 Correct 522 ms 35328 KB Output is correct
60 Correct 1845 ms 72668 KB Output is correct
61 Correct 779 ms 46532 KB Output is correct
62 Correct 687 ms 44716 KB Output is correct
63 Correct 1079 ms 44740 KB Output is correct
64 Correct 1881 ms 53460 KB Output is correct
65 Correct 1725 ms 58184 KB Output is correct
66 Correct 1789 ms 74004 KB Output is correct
67 Correct 576 ms 45436 KB Output is correct
68 Correct 1164 ms 55360 KB Output is correct
69 Correct 1194 ms 56092 KB Output is correct
70 Correct 1066 ms 52800 KB Output is correct