답안 #1111669

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111669 2024-11-12T14:22:18 Z FubuGold 경주 (Race) (IOI11_race) C++14
100 / 100
373 ms 94536 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 200000;
const long long INF = 1e9 + 5;

vector<pair<int,int>> adj[MAXN+1];
int n;
long long k;
int ans = INF;

struct Node {
    int offset_len = 0;
    long long offset_weight = 0;
    map<long long,int> mp;

    int get_len(long long x) {
        x -= offset_weight;
        auto it = mp.find(x);
        if (it == mp.end()) return INF;
        return it->second + offset_len;
    }

    void merge_node(Node &nd) {
        for (auto it = nd.mp.begin(); it != nd.mp.end(); it++) {
            long long x = it->first + nd.offset_weight;
            int l = it->second + nd.offset_len;
            if (x > k) continue;
            int tmp = get_len(k - x);
            if (tmp != INF) ans = min(ans,tmp + l);
        }
        for (auto it = nd.mp.begin(); it != nd.mp.end(); it++) {
            long long x = it->first + nd.offset_weight;
            int l = it->second + nd.offset_len;
            if (x > k) continue;
            x -= offset_weight;
            l -= offset_len;
            auto tmp_it = mp.find(x);
            if (tmp_it == mp.end()) mp[x] = l;
            else tmp_it->second = min(tmp_it->second,l);
        }
    }
};

Node nd[MAXN+1];
int sz[MAXN+1];

int cal_sz(int u,int p) {
    sz[u] = 1;
    for (int i=0;i<adj[u].size();i++) {
        int v = adj[u][i].first;
        if (v == p) continue;
        sz[u] += cal_sz(v,u);
    }
    return sz[u];
}

void dfs(int u,int p) {
    int mx_v = -1;
    int mx_v_w = 0;
    for (int i=0;i<adj[u].size();i++) {
        int v = adj[u][i].first;
        if (v == p) continue;
        dfs(v,u);
        if (mx_v == -1 || sz[mx_v] < sz[v]) {
            mx_v = v;
            mx_v_w = adj[u][i].second;
        }
    }
    if (mx_v != -1) {
        swap(nd[mx_v],nd[u]);
        nd[u].offset_len++;
        nd[u].offset_weight += mx_v_w;
        for (int i=0;i<adj[u].size();i++) {
            int v = adj[u][i].first;
            if (v == p || v == mx_v) continue;
            nd[v].offset_len++;
            nd[v].offset_weight += adj[u][i].second;
            nd[u].merge_node(nd[v]);
        }
    }
    ans = min(ans,nd[u].get_len(k));
    nd[u].mp[-nd[u].offset_weight] = -nd[u].offset_len;
}

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 x = h[i][0], y = h[i][1];
        int w = l[i];
        x++,y++;
        adj[x].push_back(make_pair(y,w));
        adj[y].push_back(make_pair(x,w));
    }
    cal_sz(1,-1);
    dfs(1,-1);
    if (ans == INF) return -1;
    return ans;
}

//int main() {
//    cin.tie(0) -> sync_with_stdio(0);
//    cin >> n >> k;
//    for (int i=1;i<n;i++) {
//        int x,y,w; cin >> x >> y >> w;
//        x++,y++;
//        adj[x].push_back(make_pair(y,w));
//        adj[y].push_back(make_pair(x,w));
//    }
////    if (k == 0) {
////        cout << 0;
////    }
//    cal_sz(1,-1);
//    dfs(1,-1);
//    if (ans == INF) ans = -1;
//    cout << ans;
//    return 0;
//}

Compilation message

race.cpp: In function 'int cal_sz(int, int)':
race.cpp:52:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   52 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int)':
race.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
race.cpp:76:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for (int i=0;i<adj[u].size();i++) {
      |                      ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22068 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 5 ms 22096 KB Output is correct
6 Correct 5 ms 22096 KB Output is correct
7 Correct 5 ms 22096 KB Output is correct
8 Correct 5 ms 22096 KB Output is correct
9 Correct 5 ms 22264 KB Output is correct
10 Correct 5 ms 22096 KB Output is correct
11 Correct 5 ms 22096 KB Output is correct
12 Correct 5 ms 22068 KB Output is correct
13 Correct 5 ms 22268 KB Output is correct
14 Correct 5 ms 22096 KB Output is correct
15 Correct 5 ms 22096 KB Output is correct
16 Correct 6 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 4 ms 22068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22068 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 5 ms 22096 KB Output is correct
6 Correct 5 ms 22096 KB Output is correct
7 Correct 5 ms 22096 KB Output is correct
8 Correct 5 ms 22096 KB Output is correct
9 Correct 5 ms 22264 KB Output is correct
10 Correct 5 ms 22096 KB Output is correct
11 Correct 5 ms 22096 KB Output is correct
12 Correct 5 ms 22068 KB Output is correct
13 Correct 5 ms 22268 KB Output is correct
14 Correct 5 ms 22096 KB Output is correct
15 Correct 5 ms 22096 KB Output is correct
16 Correct 6 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 4 ms 22068 KB Output is correct
19 Correct 5 ms 22008 KB Output is correct
20 Correct 5 ms 22096 KB Output is correct
21 Correct 5 ms 22056 KB Output is correct
22 Correct 5 ms 22052 KB Output is correct
23 Correct 5 ms 22096 KB Output is correct
24 Correct 5 ms 22096 KB Output is correct
25 Correct 5 ms 22108 KB Output is correct
26 Correct 5 ms 22096 KB Output is correct
27 Correct 5 ms 18000 KB Output is correct
28 Correct 5 ms 18000 KB Output is correct
29 Correct 6 ms 18000 KB Output is correct
30 Correct 6 ms 22096 KB Output is correct
31 Correct 5 ms 18000 KB Output is correct
32 Correct 6 ms 18256 KB Output is correct
33 Correct 7 ms 18104 KB Output is correct
34 Correct 7 ms 18000 KB Output is correct
35 Correct 5 ms 18000 KB Output is correct
36 Correct 5 ms 18000 KB Output is correct
37 Correct 6 ms 18000 KB Output is correct
38 Correct 4 ms 18000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22068 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 5 ms 22096 KB Output is correct
6 Correct 5 ms 22096 KB Output is correct
7 Correct 5 ms 22096 KB Output is correct
8 Correct 5 ms 22096 KB Output is correct
9 Correct 5 ms 22264 KB Output is correct
10 Correct 5 ms 22096 KB Output is correct
11 Correct 5 ms 22096 KB Output is correct
12 Correct 5 ms 22068 KB Output is correct
13 Correct 5 ms 22268 KB Output is correct
14 Correct 5 ms 22096 KB Output is correct
15 Correct 5 ms 22096 KB Output is correct
16 Correct 6 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 4 ms 22068 KB Output is correct
19 Correct 88 ms 34120 KB Output is correct
20 Correct 96 ms 38868 KB Output is correct
21 Correct 83 ms 34228 KB Output is correct
22 Correct 92 ms 35964 KB Output is correct
23 Correct 82 ms 39496 KB Output is correct
24 Correct 87 ms 37036 KB Output is correct
25 Correct 73 ms 34064 KB Output is correct
26 Correct 53 ms 44540 KB Output is correct
27 Correct 131 ms 44360 KB Output is correct
28 Correct 222 ms 81480 KB Output is correct
29 Correct 198 ms 79688 KB Output is correct
30 Correct 146 ms 41032 KB Output is correct
31 Correct 120 ms 40880 KB Output is correct
32 Correct 179 ms 40928 KB Output is correct
33 Correct 133 ms 40044 KB Output is correct
34 Correct 133 ms 40776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22068 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 5 ms 22096 KB Output is correct
6 Correct 5 ms 22096 KB Output is correct
7 Correct 5 ms 22096 KB Output is correct
8 Correct 5 ms 22096 KB Output is correct
9 Correct 5 ms 22264 KB Output is correct
10 Correct 5 ms 22096 KB Output is correct
11 Correct 5 ms 22096 KB Output is correct
12 Correct 5 ms 22068 KB Output is correct
13 Correct 5 ms 22268 KB Output is correct
14 Correct 5 ms 22096 KB Output is correct
15 Correct 5 ms 22096 KB Output is correct
16 Correct 6 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 4 ms 22068 KB Output is correct
19 Correct 5 ms 22008 KB Output is correct
20 Correct 5 ms 22096 KB Output is correct
21 Correct 5 ms 22056 KB Output is correct
22 Correct 5 ms 22052 KB Output is correct
23 Correct 5 ms 22096 KB Output is correct
24 Correct 5 ms 22096 KB Output is correct
25 Correct 5 ms 22108 KB Output is correct
26 Correct 5 ms 22096 KB Output is correct
27 Correct 5 ms 18000 KB Output is correct
28 Correct 5 ms 18000 KB Output is correct
29 Correct 6 ms 18000 KB Output is correct
30 Correct 6 ms 22096 KB Output is correct
31 Correct 5 ms 18000 KB Output is correct
32 Correct 6 ms 18256 KB Output is correct
33 Correct 7 ms 18104 KB Output is correct
34 Correct 7 ms 18000 KB Output is correct
35 Correct 5 ms 18000 KB Output is correct
36 Correct 5 ms 18000 KB Output is correct
37 Correct 6 ms 18000 KB Output is correct
38 Correct 4 ms 18000 KB Output is correct
39 Correct 88 ms 34120 KB Output is correct
40 Correct 96 ms 38868 KB Output is correct
41 Correct 83 ms 34228 KB Output is correct
42 Correct 92 ms 35964 KB Output is correct
43 Correct 82 ms 39496 KB Output is correct
44 Correct 87 ms 37036 KB Output is correct
45 Correct 73 ms 34064 KB Output is correct
46 Correct 53 ms 44540 KB Output is correct
47 Correct 131 ms 44360 KB Output is correct
48 Correct 222 ms 81480 KB Output is correct
49 Correct 198 ms 79688 KB Output is correct
50 Correct 146 ms 41032 KB Output is correct
51 Correct 120 ms 40880 KB Output is correct
52 Correct 179 ms 40928 KB Output is correct
53 Correct 133 ms 40044 KB Output is correct
54 Correct 133 ms 40776 KB Output is correct
55 Correct 18 ms 20560 KB Output is correct
56 Correct 14 ms 19280 KB Output is correct
57 Correct 66 ms 36168 KB Output is correct
58 Correct 37 ms 30652 KB Output is correct
59 Correct 75 ms 50504 KB Output is correct
60 Correct 204 ms 81480 KB Output is correct
61 Correct 144 ms 45896 KB Output is correct
62 Correct 139 ms 44104 KB Output is correct
63 Correct 165 ms 44104 KB Output is correct
64 Correct 373 ms 94536 KB Output is correct
65 Correct 142 ms 44624 KB Output is correct
66 Correct 205 ms 73548 KB Output is correct
67 Correct 150 ms 44472 KB Output is correct
68 Correct 265 ms 64072 KB Output is correct
69 Correct 269 ms 62296 KB Output is correct
70 Correct 273 ms 61368 KB Output is correct