답안 #1111668

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111668 2024-11-12T14:13:35 Z FubuGold 경주 (Race) (IOI11_race) C++14
43 / 100
216 ms 83056 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);
            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:47: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]
   47 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int)':
race.cpp:58: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]
   58 |     for (int i=0;i<adj[u].size();i++) {
      |                  ~^~~~~~~~~~~~~~
race.cpp:71: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]
   71 |         for (int i=0;i<adj[u].size();i++) {
      |                      ~^~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22096 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 4 ms 22264 KB Output is correct
6 Correct 4 ms 22096 KB Output is correct
7 Correct 4 ms 22096 KB Output is correct
8 Correct 4 ms 22096 KB Output is correct
9 Correct 4 ms 22096 KB Output is correct
10 Correct 4 ms 20048 KB Output is correct
11 Correct 5 ms 18004 KB Output is correct
12 Correct 3 ms 18000 KB Output is correct
13 Correct 6 ms 18012 KB Output is correct
14 Correct 5 ms 17936 KB Output is correct
15 Correct 4 ms 18000 KB Output is correct
16 Correct 5 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 5 ms 22096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22096 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 4 ms 22264 KB Output is correct
6 Correct 4 ms 22096 KB Output is correct
7 Correct 4 ms 22096 KB Output is correct
8 Correct 4 ms 22096 KB Output is correct
9 Correct 4 ms 22096 KB Output is correct
10 Correct 4 ms 20048 KB Output is correct
11 Correct 5 ms 18004 KB Output is correct
12 Correct 3 ms 18000 KB Output is correct
13 Correct 6 ms 18012 KB Output is correct
14 Correct 5 ms 17936 KB Output is correct
15 Correct 4 ms 18000 KB Output is correct
16 Correct 5 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 5 ms 22096 KB Output is correct
19 Correct 5 ms 21840 KB Output is correct
20 Correct 4 ms 22068 KB Output is correct
21 Correct 5 ms 22108 KB Output is correct
22 Correct 4 ms 20048 KB Output is correct
23 Correct 8 ms 18000 KB Output is correct
24 Correct 7 ms 18000 KB Output is correct
25 Correct 9 ms 18256 KB Output is correct
26 Correct 5 ms 22176 KB Output is correct
27 Correct 5 ms 22096 KB Output is correct
28 Correct 7 ms 20048 KB Output is correct
29 Correct 5 ms 18424 KB Output is correct
30 Correct 6 ms 22352 KB Output is correct
31 Correct 6 ms 22096 KB Output is correct
32 Correct 5 ms 22096 KB Output is correct
33 Correct 5 ms 22352 KB Output is correct
34 Correct 5 ms 22096 KB Output is correct
35 Correct 5 ms 22096 KB Output is correct
36 Correct 5 ms 22108 KB Output is correct
37 Correct 5 ms 22264 KB Output is correct
38 Correct 5 ms 22068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22096 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 4 ms 22264 KB Output is correct
6 Correct 4 ms 22096 KB Output is correct
7 Correct 4 ms 22096 KB Output is correct
8 Correct 4 ms 22096 KB Output is correct
9 Correct 4 ms 22096 KB Output is correct
10 Correct 4 ms 20048 KB Output is correct
11 Correct 5 ms 18004 KB Output is correct
12 Correct 3 ms 18000 KB Output is correct
13 Correct 6 ms 18012 KB Output is correct
14 Correct 5 ms 17936 KB Output is correct
15 Correct 4 ms 18000 KB Output is correct
16 Correct 5 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 5 ms 22096 KB Output is correct
19 Correct 93 ms 35660 KB Output is correct
20 Correct 79 ms 35628 KB Output is correct
21 Correct 83 ms 35636 KB Output is correct
22 Correct 85 ms 35632 KB Output is correct
23 Correct 74 ms 36168 KB Output is correct
24 Correct 95 ms 38452 KB Output is correct
25 Correct 83 ms 35400 KB Output is correct
26 Correct 50 ms 44360 KB Output is correct
27 Correct 126 ms 46640 KB Output is correct
28 Correct 216 ms 83056 KB Output is correct
29 Correct 200 ms 80060 KB Output is correct
30 Correct 128 ms 46628 KB Output is correct
31 Correct 127 ms 47432 KB Output is correct
32 Correct 183 ms 45640 KB Output is correct
33 Correct 130 ms 45640 KB Output is correct
34 Correct 134 ms 43596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 22096 KB Output is correct
2 Correct 4 ms 22096 KB Output is correct
3 Correct 4 ms 22096 KB Output is correct
4 Correct 4 ms 22096 KB Output is correct
5 Correct 4 ms 22264 KB Output is correct
6 Correct 4 ms 22096 KB Output is correct
7 Correct 4 ms 22096 KB Output is correct
8 Correct 4 ms 22096 KB Output is correct
9 Correct 4 ms 22096 KB Output is correct
10 Correct 4 ms 20048 KB Output is correct
11 Correct 5 ms 18004 KB Output is correct
12 Correct 3 ms 18000 KB Output is correct
13 Correct 6 ms 18012 KB Output is correct
14 Correct 5 ms 17936 KB Output is correct
15 Correct 4 ms 18000 KB Output is correct
16 Correct 5 ms 22096 KB Output is correct
17 Correct 4 ms 22096 KB Output is correct
18 Correct 5 ms 22096 KB Output is correct
19 Correct 5 ms 21840 KB Output is correct
20 Correct 4 ms 22068 KB Output is correct
21 Correct 5 ms 22108 KB Output is correct
22 Correct 4 ms 20048 KB Output is correct
23 Correct 8 ms 18000 KB Output is correct
24 Correct 7 ms 18000 KB Output is correct
25 Correct 9 ms 18256 KB Output is correct
26 Correct 5 ms 22176 KB Output is correct
27 Correct 5 ms 22096 KB Output is correct
28 Correct 7 ms 20048 KB Output is correct
29 Correct 5 ms 18424 KB Output is correct
30 Correct 6 ms 22352 KB Output is correct
31 Correct 6 ms 22096 KB Output is correct
32 Correct 5 ms 22096 KB Output is correct
33 Correct 5 ms 22352 KB Output is correct
34 Correct 5 ms 22096 KB Output is correct
35 Correct 5 ms 22096 KB Output is correct
36 Correct 5 ms 22108 KB Output is correct
37 Correct 5 ms 22264 KB Output is correct
38 Correct 5 ms 22068 KB Output is correct
39 Correct 93 ms 35660 KB Output is correct
40 Correct 79 ms 35628 KB Output is correct
41 Correct 83 ms 35636 KB Output is correct
42 Correct 85 ms 35632 KB Output is correct
43 Correct 74 ms 36168 KB Output is correct
44 Correct 95 ms 38452 KB Output is correct
45 Correct 83 ms 35400 KB Output is correct
46 Correct 50 ms 44360 KB Output is correct
47 Correct 126 ms 46640 KB Output is correct
48 Correct 216 ms 83056 KB Output is correct
49 Correct 200 ms 80060 KB Output is correct
50 Correct 128 ms 46628 KB Output is correct
51 Correct 127 ms 47432 KB Output is correct
52 Correct 183 ms 45640 KB Output is correct
53 Correct 130 ms 45640 KB Output is correct
54 Correct 134 ms 43596 KB Output is correct
55 Correct 18 ms 20684 KB Output is correct
56 Correct 13 ms 19280 KB Output is correct
57 Incorrect 53 ms 35920 KB Output isn't correct
58 Halted 0 ms 0 KB -