답안 #493580

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
493580 2021-12-12T09:05:30 Z nekotizimo 경주 (Race) (IOI11_race) C++17
100 / 100
398 ms 80872 KB
#include "race.h"
#include <vector>
#include <map>
#include <iostream>
// #define IS_DEBUG
using namespace std;
typedef long long ll;
template<typename a, typename b> ostream& operator<< (std::ostream &out, const pair<a, b> &p) {
    out << "(" << p.first << ", " << p.second << ")";
    return out;
}

const bool DEBUG = false;
const int MAXN = 2e5 + 5;
ll _K, ans = MAXN, o1[MAXN], o2[MAXN];
vector< pair<ll, ll> > adj_list[MAXN];
map<ll, ll> paths[MAXN]; // length, num

void dfs(int cur, int p) {
    for (auto &n : adj_list[cur]) {
        ll neigh = n.first, l = n.second;
        if (p == neigh) continue;
        dfs(neigh, cur);
        if (paths[neigh].size() > paths[cur].size()) {
            paths[cur].swap(paths[neigh]);
            o1[neigh] += l;
            o2[neigh]++;
            o1[cur] -= l;
            o2[cur]--;
            swap(o1[cur], o1[neigh]);
            swap(o2[cur], o2[neigh]);
        }
        for (auto e : paths[neigh]) {
            if (paths[cur].count(_K - (e.first + l + o1[neigh]) - o1[cur])) {
                ans = min(ans, paths[cur][_K - (e.first + l + o1[neigh]) - o1[cur]] + e.second + 1 + o2[neigh] + o2[cur]); 
                
                if (DEBUG) cerr << cur << ": " << paths[cur][_K - (e.first + l + o1[neigh]) - o1[cur]] + e.second + 1 + o2[neigh] + o2[cur] << endl;
                // only with newly added cuz from differnt subtrees
            }
        }
        for (auto e : paths[neigh]) {
            if (paths[cur].count(e.first + l + o1[neigh] - o1[cur])) {
                paths[cur][e.first + l + o1[neigh] - o1[cur]] = min(paths[cur][e.first + l + o1[neigh] - o1[cur]], e.second + 1 + o2[neigh] - o2[cur]);
            } else {
                paths[cur][e.first + l + o1[neigh] - o1[cur]] = e.second + 1 + o2[neigh] - o2[cur];
            }
        }
        paths[neigh].clear();
    }
    if (paths[cur].count(_K - o1[cur])) {
        ans = min(ans, paths[cur][_K - o1[cur]] + o2[cur]);
        if (DEBUG) cerr << cur << ": " << paths[cur][_K - o1[cur]] + o2[cur] << endl;
    } 

    paths[cur][-o1[cur]] = -o2[cur];
    
    if (DEBUG) {
        cerr << cur << ": " << o1[cur] << " " << o2[cur] << " ";
        for (auto e : paths[cur]) cerr << e << " ";
        cerr << endl;
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    _K = K;
    for (int i = 0; i < N - 1; i++) {
        adj_list[H[i][0]].push_back({H[i][1], L[i]}), adj_list[H[i][1]].push_back({H[i][0], L[i]});
    }
    dfs(0, -1);
    return (ans == MAXN) ? -1 : ans;
}

#ifdef IS_DEBUG
int main() {
    int n, k;
	cin >> n >> k;
    int h[MAXN][2], l[MAXN];
    for (int i = 0 ; i < n - 1; i++) {
        cin >> h[i][0] >> h[i][1] >> l[i];
    }

    cout << best_path(n, k, h, l) << endl;

	return 0;
}
#endif
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 8 ms 14396 KB Output is correct
5 Correct 10 ms 14400 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 7 ms 14372 KB Output is correct
9 Correct 7 ms 14404 KB Output is correct
10 Correct 7 ms 14388 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 7 ms 14392 KB Output is correct
13 Correct 7 ms 14384 KB Output is correct
14 Correct 7 ms 14328 KB Output is correct
15 Correct 7 ms 14392 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14376 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 8 ms 14396 KB Output is correct
5 Correct 10 ms 14400 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 7 ms 14372 KB Output is correct
9 Correct 7 ms 14404 KB Output is correct
10 Correct 7 ms 14388 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 7 ms 14392 KB Output is correct
13 Correct 7 ms 14384 KB Output is correct
14 Correct 7 ms 14328 KB Output is correct
15 Correct 7 ms 14392 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14376 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 6 ms 14396 KB Output is correct
20 Correct 7 ms 14412 KB Output is correct
21 Correct 7 ms 14412 KB Output is correct
22 Correct 8 ms 14540 KB Output is correct
23 Correct 8 ms 14536 KB Output is correct
24 Correct 8 ms 14540 KB Output is correct
25 Correct 9 ms 14532 KB Output is correct
26 Correct 8 ms 14540 KB Output is correct
27 Correct 8 ms 14412 KB Output is correct
28 Correct 8 ms 14540 KB Output is correct
29 Correct 9 ms 14540 KB Output is correct
30 Correct 7 ms 14540 KB Output is correct
31 Correct 8 ms 14528 KB Output is correct
32 Correct 8 ms 14540 KB Output is correct
33 Correct 8 ms 14540 KB Output is correct
34 Correct 8 ms 14532 KB Output is correct
35 Correct 8 ms 14496 KB Output is correct
36 Correct 8 ms 14540 KB Output is correct
37 Correct 8 ms 14532 KB Output is correct
38 Correct 8 ms 14532 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 8 ms 14396 KB Output is correct
5 Correct 10 ms 14400 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 7 ms 14372 KB Output is correct
9 Correct 7 ms 14404 KB Output is correct
10 Correct 7 ms 14388 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 7 ms 14392 KB Output is correct
13 Correct 7 ms 14384 KB Output is correct
14 Correct 7 ms 14328 KB Output is correct
15 Correct 7 ms 14392 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14376 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 99 ms 23604 KB Output is correct
20 Correct 100 ms 23608 KB Output is correct
21 Correct 97 ms 23624 KB Output is correct
22 Correct 103 ms 23588 KB Output is correct
23 Correct 125 ms 24224 KB Output is correct
24 Correct 98 ms 23364 KB Output is correct
25 Correct 74 ms 33748 KB Output is correct
26 Correct 52 ms 41940 KB Output is correct
27 Correct 168 ms 32380 KB Output is correct
28 Correct 239 ms 80576 KB Output is correct
29 Correct 244 ms 78800 KB Output is correct
30 Correct 172 ms 32324 KB Output is correct
31 Correct 170 ms 32224 KB Output is correct
32 Correct 207 ms 32388 KB Output is correct
33 Correct 188 ms 31536 KB Output is correct
34 Correct 301 ms 44356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14412 KB Output is correct
2 Correct 7 ms 14396 KB Output is correct
3 Correct 7 ms 14412 KB Output is correct
4 Correct 8 ms 14396 KB Output is correct
5 Correct 10 ms 14400 KB Output is correct
6 Correct 7 ms 14412 KB Output is correct
7 Correct 8 ms 14412 KB Output is correct
8 Correct 7 ms 14372 KB Output is correct
9 Correct 7 ms 14404 KB Output is correct
10 Correct 7 ms 14388 KB Output is correct
11 Correct 7 ms 14412 KB Output is correct
12 Correct 7 ms 14392 KB Output is correct
13 Correct 7 ms 14384 KB Output is correct
14 Correct 7 ms 14328 KB Output is correct
15 Correct 7 ms 14392 KB Output is correct
16 Correct 7 ms 14412 KB Output is correct
17 Correct 7 ms 14376 KB Output is correct
18 Correct 7 ms 14412 KB Output is correct
19 Correct 6 ms 14396 KB Output is correct
20 Correct 7 ms 14412 KB Output is correct
21 Correct 7 ms 14412 KB Output is correct
22 Correct 8 ms 14540 KB Output is correct
23 Correct 8 ms 14536 KB Output is correct
24 Correct 8 ms 14540 KB Output is correct
25 Correct 9 ms 14532 KB Output is correct
26 Correct 8 ms 14540 KB Output is correct
27 Correct 8 ms 14412 KB Output is correct
28 Correct 8 ms 14540 KB Output is correct
29 Correct 9 ms 14540 KB Output is correct
30 Correct 7 ms 14540 KB Output is correct
31 Correct 8 ms 14528 KB Output is correct
32 Correct 8 ms 14540 KB Output is correct
33 Correct 8 ms 14540 KB Output is correct
34 Correct 8 ms 14532 KB Output is correct
35 Correct 8 ms 14496 KB Output is correct
36 Correct 8 ms 14540 KB Output is correct
37 Correct 8 ms 14532 KB Output is correct
38 Correct 8 ms 14532 KB Output is correct
39 Correct 99 ms 23604 KB Output is correct
40 Correct 100 ms 23608 KB Output is correct
41 Correct 97 ms 23624 KB Output is correct
42 Correct 103 ms 23588 KB Output is correct
43 Correct 125 ms 24224 KB Output is correct
44 Correct 98 ms 23364 KB Output is correct
45 Correct 74 ms 33748 KB Output is correct
46 Correct 52 ms 41940 KB Output is correct
47 Correct 168 ms 32380 KB Output is correct
48 Correct 239 ms 80576 KB Output is correct
49 Correct 244 ms 78800 KB Output is correct
50 Correct 172 ms 32324 KB Output is correct
51 Correct 170 ms 32224 KB Output is correct
52 Correct 207 ms 32388 KB Output is correct
53 Correct 188 ms 31536 KB Output is correct
54 Correct 301 ms 44356 KB Output is correct
55 Correct 18 ms 15692 KB Output is correct
56 Correct 13 ms 15308 KB Output is correct
57 Correct 56 ms 23920 KB Output is correct
58 Correct 50 ms 21340 KB Output is correct
59 Correct 72 ms 48144 KB Output is correct
60 Correct 220 ms 80872 KB Output is correct
61 Correct 219 ms 35580 KB Output is correct
62 Correct 167 ms 33816 KB Output is correct
63 Correct 198 ms 33984 KB Output is correct
64 Correct 375 ms 42060 KB Output is correct
65 Correct 398 ms 46632 KB Output is correct
66 Correct 244 ms 76384 KB Output is correct
67 Correct 146 ms 32688 KB Output is correct
68 Correct 302 ms 44428 KB Output is correct
69 Correct 293 ms 48620 KB Output is correct
70 Correct 269 ms 43212 KB Output is correct