답안 #646390

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
646390 2022-09-29T17:43:42 Z Vectorized 경주 (Race) (IOI11_race) C++17
100 / 100
443 ms 98092 KB
#define ll long long
#define maxN 200005
using namespace std;
#include <iostream>
#include<vector>
#include <climits>
#include<set>
#include<map>
//first = node, second = weight
vector<pair<int ,ll>> adj[maxN];
map<ll, int> sets[maxN];
vector<int> ord;
int p[maxN], lzN[maxN];
ll lzW[maxN];

void dfs(int node, int parent){
    p[node] = parent;
    for(pair<int, int> j: adj[node]){
        if(j.first != parent){
            dfs(j.first, node);
            ord.push_back(j.first);
        }
    }
}


int best_path(int n, int k, int edges[][2], int weights[]){
//    cin >> n >> k;
    for (int i = 0; i < n - 1; ++i) {
//        int a = 0, b = 0, w= 0;
//        cin >> a >> b >> w;
        int a = edges[i][0], b = edges[i][1], w = weights[i];
        adj[a].push_back(make_pair(b, w));
        adj[b].push_back(make_pair(a, w));
    }
    dfs(0, -1);
    p[0] = -1;
    ord.push_back(0);
    int ans = INT_MAX;
    for (int i = 0; i < n; ++i) {
        int node = ord[i];
        if(adj[node].size() == 1 && node != 0){
            lzW[node] = 0;
            lzN[node] = 0;
            sets[node][0] = 0;
            continue;
        }
        int bn = -1, bz = -1;
        ll bw = 0;
        for(pair<int, int> j : adj[node]){
            if(j.first == p[node]) continue;
            if((int)sets[j.first].size() > bz){
                bz = (int)sets[j.first].size();
                bn = j.first;
                bw = j.second;
            }
        }
        swap(sets[bn], sets[node]);
        lzW[node] = lzW[bn] + bw;
        lzN[node] = lzN[bn] + 1;
        sets[node][bw - lzW[node]] = 1 - lzN[node];
        sets[node][0 - lzW[node]] = 0 - lzN[node];
        if(bw == k){
            ans = min(ans, 1);
        }
        for(pair<int, int> j: adj[node]){
            if(j.first == p[node] || j.first == bn) continue;
            for (auto const& a : sets[j.first]){
                ll f = (k - (a.first + lzW[j.first] + j.second)) - lzW[node];
                if(sets[node].find(f) != sets[node].end()){
                    ans = min(ans, a.second + lzN[j.first] + 1 + sets[node][f] + lzN[node]);
                }
            }
            for (auto const& a : sets[j.first]){
                ll f = (a.first + lzW[j.first] + j.second) - lzW[node];
                if(sets[node].find(f) != sets[node].end()){
                    sets[node][f] = min(sets[node][f], a.second + lzN[j.first] + 1 - lzN[node]);
                }else{
                    sets[node][f] = a.second + lzN[j.first] + 1 - lzN[node];
                }
            }
//            ll f = k - j.second;
//            if(sets[node].find(f) != sets[node].end()){
//                ans = min(ans, sets[node][f] + lzN[node] + 1);
//            }
//            sets[node][j.second - lzW[node]] = 1 - lzN[node];
        }
        if(sets[node].find(k - lzW[node]) != sets[node].end()){
            ans = min(ans, sets[node][k - lzW[node]] + lzN[node]);
        }
    }
    return (ans == INT_MAX) ? -1: ans;

}

//int main(){
//    cout << best_path(0, 0, {}, 0);
////    int cE[10][2] = {{0, 1}, {0, 2}, {2,3}, {3,4}, {4,5}, {0, 6}, {}};
////    cout << best_path(11, 12, )
//}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14368 KB Output is correct
15 Correct 8 ms 14348 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 9 ms 14440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14368 KB Output is correct
15 Correct 8 ms 14348 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 9 ms 14440 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14404 KB Output is correct
21 Correct 8 ms 14548 KB Output is correct
22 Correct 8 ms 14664 KB Output is correct
23 Correct 9 ms 14676 KB Output is correct
24 Correct 10 ms 14676 KB Output is correct
25 Correct 9 ms 14720 KB Output is correct
26 Correct 10 ms 14676 KB Output is correct
27 Correct 8 ms 14536 KB Output is correct
28 Correct 10 ms 14676 KB Output is correct
29 Correct 9 ms 14668 KB Output is correct
30 Correct 11 ms 14648 KB Output is correct
31 Correct 8 ms 14660 KB Output is correct
32 Correct 9 ms 14664 KB Output is correct
33 Correct 11 ms 14668 KB Output is correct
34 Correct 8 ms 14548 KB Output is correct
35 Correct 8 ms 14548 KB Output is correct
36 Correct 8 ms 14548 KB Output is correct
37 Correct 11 ms 14548 KB Output is correct
38 Correct 8 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14368 KB Output is correct
15 Correct 8 ms 14348 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 9 ms 14440 KB Output is correct
19 Correct 122 ms 33596 KB Output is correct
20 Correct 118 ms 33624 KB Output is correct
21 Correct 109 ms 33612 KB Output is correct
22 Correct 122 ms 33708 KB Output is correct
23 Correct 155 ms 47048 KB Output is correct
24 Correct 125 ms 39440 KB Output is correct
25 Correct 83 ms 26184 KB Output is correct
26 Correct 49 ms 28600 KB Output is correct
27 Correct 198 ms 44104 KB Output is correct
28 Correct 228 ms 55036 KB Output is correct
29 Correct 245 ms 55052 KB Output is correct
30 Correct 190 ms 44092 KB Output is correct
31 Correct 203 ms 44168 KB Output is correct
32 Correct 231 ms 44108 KB Output is correct
33 Correct 206 ms 42600 KB Output is correct
34 Correct 312 ms 74628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 8 ms 14420 KB Output is correct
7 Correct 7 ms 14420 KB Output is correct
8 Correct 7 ms 14420 KB Output is correct
9 Correct 7 ms 14420 KB Output is correct
10 Correct 7 ms 14420 KB Output is correct
11 Correct 7 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 8 ms 14368 KB Output is correct
15 Correct 8 ms 14348 KB Output is correct
16 Correct 7 ms 14420 KB Output is correct
17 Correct 7 ms 14420 KB Output is correct
18 Correct 9 ms 14440 KB Output is correct
19 Correct 7 ms 14420 KB Output is correct
20 Correct 8 ms 14404 KB Output is correct
21 Correct 8 ms 14548 KB Output is correct
22 Correct 8 ms 14664 KB Output is correct
23 Correct 9 ms 14676 KB Output is correct
24 Correct 10 ms 14676 KB Output is correct
25 Correct 9 ms 14720 KB Output is correct
26 Correct 10 ms 14676 KB Output is correct
27 Correct 8 ms 14536 KB Output is correct
28 Correct 10 ms 14676 KB Output is correct
29 Correct 9 ms 14668 KB Output is correct
30 Correct 11 ms 14648 KB Output is correct
31 Correct 8 ms 14660 KB Output is correct
32 Correct 9 ms 14664 KB Output is correct
33 Correct 11 ms 14668 KB Output is correct
34 Correct 8 ms 14548 KB Output is correct
35 Correct 8 ms 14548 KB Output is correct
36 Correct 8 ms 14548 KB Output is correct
37 Correct 11 ms 14548 KB Output is correct
38 Correct 8 ms 14548 KB Output is correct
39 Correct 122 ms 33596 KB Output is correct
40 Correct 118 ms 33624 KB Output is correct
41 Correct 109 ms 33612 KB Output is correct
42 Correct 122 ms 33708 KB Output is correct
43 Correct 155 ms 47048 KB Output is correct
44 Correct 125 ms 39440 KB Output is correct
45 Correct 83 ms 26184 KB Output is correct
46 Correct 49 ms 28600 KB Output is correct
47 Correct 198 ms 44104 KB Output is correct
48 Correct 228 ms 55036 KB Output is correct
49 Correct 245 ms 55052 KB Output is correct
50 Correct 190 ms 44092 KB Output is correct
51 Correct 203 ms 44168 KB Output is correct
52 Correct 231 ms 44108 KB Output is correct
53 Correct 206 ms 42600 KB Output is correct
54 Correct 312 ms 74628 KB Output is correct
55 Correct 21 ms 17492 KB Output is correct
56 Correct 15 ms 16084 KB Output is correct
57 Correct 67 ms 33464 KB Output is correct
58 Correct 54 ms 29456 KB Output is correct
59 Correct 87 ms 36028 KB Output is correct
60 Correct 244 ms 57788 KB Output is correct
61 Correct 218 ms 50500 KB Output is correct
62 Correct 190 ms 47104 KB Output is correct
63 Correct 257 ms 47040 KB Output is correct
64 Correct 443 ms 97344 KB Output is correct
65 Correct 438 ms 98092 KB Output is correct
66 Correct 281 ms 58272 KB Output is correct
67 Correct 195 ms 46064 KB Output is correct
68 Correct 356 ms 65200 KB Output is correct
69 Correct 337 ms 65584 KB Output is correct
70 Correct 320 ms 62900 KB Output is correct