답안 #639076

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
639076 2022-09-08T13:46:05 Z Andrey 경주 (Race) (IOI11_race) C++14
100 / 100
1422 ms 67232 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;

int n,k,ans = INT_MAX;
vector<pair<int,int>> haha[200001];
vector<int> st(200001);
vector<bool> yeah(200001,true);
int dfs(int a, int t) {
    int sb = 1;
    for(int i = 0; i < haha[a].size(); i++) {
        if(haha[a][i].first != t && yeah[haha[a][i].first]) {
            sb+=dfs(haha[a][i].first,a);
        }
    }
    st[a] = sb;
    return sb;
}

void calc(map<int,int>& wut, int a, int d, int sb, int t) {
    if(wut[k-sb] != 0 || k == sb) {
        ans = min(ans,wut[k-sb]+d);
    }
    for(int i = 0; i < haha[a].size(); i++) {
        if(yeah[haha[a][i].first] && haha[a][i].first != t) {
            calc(wut,haha[a][i].first,d+1,sb+haha[a][i].second,a);
        }
    }
}

void update(map<int,int>& wut, int a, int d, int sb, int t) {
    if(wut[sb] != 0) {
        wut[sb] = min(wut[sb],d);
    }
    else {
        wut[sb] = d;
    }
    for(int i = 0; i < haha[a].size(); i++) {
        if(yeah[haha[a][i].first] && haha[a][i].first != t) {
            update(wut,haha[a][i].first,d+1,sb+haha[a][i].second,a);
        }
    }
}

void dude(int a) {
    dfs(a,-1);
    int c = 0,p = a;
    while(p != -1) {
        c = p;
        p = -1;
        for(int i = 0; i < haha[c].size(); i++) {
            if(yeah[haha[c][i].first] && st[c] > st[haha[c][i].first] && st[haha[c][i].first] > st[a]/2) {
                p = haha[c][i].first;
            }
        }
    }
    map<int,int> wut;
    wut[0] = 0;
    for(int i = 0; i < haha[c].size(); i++) {
        if(yeah[haha[c][i].first]) {
            calc(wut,haha[c][i].first,1,haha[c][i].second,c);
            update(wut,haha[c][i].first,1,haha[c][i].second,c);
        }
    }
    yeah[c] = false;
    for(int i = 0; i < haha[c].size(); i++) {
        if(yeah[haha[c][i].first]) {
            dude(haha[c][i].first);
        }
    }
    yeah[c] = true;
}

int best_path(int N, int K, int H[][2], int L[]) {
    n = N;
    k = K;
    for(int i = 0; i < n-1; i++) {
        haha[H[i][0]].push_back({H[i][1],L[i]});
        haha[H[i][1]].push_back({H[i][0],L[i]});
    }
    dude(0);
    if(ans == INT_MAX) {
        ans = -1;
    }
    return ans;
}

Compilation message

race.cpp: In function 'int dfs(int, int)':
race.cpp:11:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i = 0; i < haha[a].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void calc(std::map<int, int>&, int, int, int, int)':
race.cpp:24:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |     for(int i = 0; i < haha[a].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void update(std::map<int, int>&, int, int, int, int)':
race.cpp:38:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i = 0; i < haha[a].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
race.cpp: In function 'void dude(int)':
race.cpp:51:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |         for(int i = 0; i < haha[c].size(); i++) {
      |                        ~~^~~~~~~~~~~~~~~~
race.cpp:59:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |     for(int i = 0; i < haha[c].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
race.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   66 |     for(int i = 0; i < haha[c].size(); i++) {
      |                    ~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5844 KB Output is correct
6 Correct 3 ms 5844 KB Output is correct
7 Correct 4 ms 5796 KB Output is correct
8 Correct 3 ms 5844 KB Output is correct
9 Correct 3 ms 5844 KB Output is correct
10 Correct 3 ms 5844 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 4 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 3 ms 5844 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5844 KB Output is correct
6 Correct 3 ms 5844 KB Output is correct
7 Correct 4 ms 5796 KB Output is correct
8 Correct 3 ms 5844 KB Output is correct
9 Correct 3 ms 5844 KB Output is correct
10 Correct 3 ms 5844 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 4 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 3 ms 5844 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5844 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 3 ms 5716 KB Output is correct
21 Correct 5 ms 5844 KB Output is correct
22 Correct 5 ms 5972 KB Output is correct
23 Correct 6 ms 5972 KB Output is correct
24 Correct 6 ms 5972 KB Output is correct
25 Correct 6 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 3 ms 5844 KB Output is correct
28 Correct 5 ms 5972 KB Output is correct
29 Correct 5 ms 5972 KB Output is correct
30 Correct 5 ms 5972 KB Output is correct
31 Correct 5 ms 5972 KB Output is correct
32 Correct 5 ms 5972 KB Output is correct
33 Correct 5 ms 5972 KB Output is correct
34 Correct 5 ms 5972 KB Output is correct
35 Correct 5 ms 5972 KB Output is correct
36 Correct 5 ms 5972 KB Output is correct
37 Correct 5 ms 6032 KB Output is correct
38 Correct 5 ms 5972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5844 KB Output is correct
6 Correct 3 ms 5844 KB Output is correct
7 Correct 4 ms 5796 KB Output is correct
8 Correct 3 ms 5844 KB Output is correct
9 Correct 3 ms 5844 KB Output is correct
10 Correct 3 ms 5844 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 4 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 3 ms 5844 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5844 KB Output is correct
19 Correct 329 ms 10788 KB Output is correct
20 Correct 299 ms 10760 KB Output is correct
21 Correct 333 ms 10708 KB Output is correct
22 Correct 260 ms 10744 KB Output is correct
23 Correct 443 ms 11344 KB Output is correct
24 Correct 170 ms 10828 KB Output is correct
25 Correct 246 ms 16500 KB Output is correct
26 Correct 124 ms 17868 KB Output is correct
27 Correct 387 ms 16152 KB Output is correct
28 Correct 1235 ms 67232 KB Output is correct
29 Correct 1258 ms 66128 KB Output is correct
30 Correct 385 ms 16204 KB Output is correct
31 Correct 410 ms 16112 KB Output is correct
32 Correct 504 ms 16204 KB Output is correct
33 Correct 686 ms 15512 KB Output is correct
34 Correct 1365 ms 47420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5844 KB Output is correct
2 Correct 3 ms 5844 KB Output is correct
3 Correct 3 ms 5844 KB Output is correct
4 Correct 3 ms 5716 KB Output is correct
5 Correct 3 ms 5844 KB Output is correct
6 Correct 3 ms 5844 KB Output is correct
7 Correct 4 ms 5796 KB Output is correct
8 Correct 3 ms 5844 KB Output is correct
9 Correct 3 ms 5844 KB Output is correct
10 Correct 3 ms 5844 KB Output is correct
11 Correct 3 ms 5844 KB Output is correct
12 Correct 3 ms 5844 KB Output is correct
13 Correct 4 ms 5844 KB Output is correct
14 Correct 3 ms 5844 KB Output is correct
15 Correct 3 ms 5844 KB Output is correct
16 Correct 3 ms 5844 KB Output is correct
17 Correct 3 ms 5716 KB Output is correct
18 Correct 3 ms 5844 KB Output is correct
19 Correct 3 ms 5716 KB Output is correct
20 Correct 3 ms 5716 KB Output is correct
21 Correct 5 ms 5844 KB Output is correct
22 Correct 5 ms 5972 KB Output is correct
23 Correct 6 ms 5972 KB Output is correct
24 Correct 6 ms 5972 KB Output is correct
25 Correct 6 ms 5972 KB Output is correct
26 Correct 4 ms 5972 KB Output is correct
27 Correct 3 ms 5844 KB Output is correct
28 Correct 5 ms 5972 KB Output is correct
29 Correct 5 ms 5972 KB Output is correct
30 Correct 5 ms 5972 KB Output is correct
31 Correct 5 ms 5972 KB Output is correct
32 Correct 5 ms 5972 KB Output is correct
33 Correct 5 ms 5972 KB Output is correct
34 Correct 5 ms 5972 KB Output is correct
35 Correct 5 ms 5972 KB Output is correct
36 Correct 5 ms 5972 KB Output is correct
37 Correct 5 ms 6032 KB Output is correct
38 Correct 5 ms 5972 KB Output is correct
39 Correct 329 ms 10788 KB Output is correct
40 Correct 299 ms 10760 KB Output is correct
41 Correct 333 ms 10708 KB Output is correct
42 Correct 260 ms 10744 KB Output is correct
43 Correct 443 ms 11344 KB Output is correct
44 Correct 170 ms 10828 KB Output is correct
45 Correct 246 ms 16500 KB Output is correct
46 Correct 124 ms 17868 KB Output is correct
47 Correct 387 ms 16152 KB Output is correct
48 Correct 1235 ms 67232 KB Output is correct
49 Correct 1258 ms 66128 KB Output is correct
50 Correct 385 ms 16204 KB Output is correct
51 Correct 410 ms 16112 KB Output is correct
52 Correct 504 ms 16204 KB Output is correct
53 Correct 686 ms 15512 KB Output is correct
54 Correct 1365 ms 47420 KB Output is correct
55 Correct 32 ms 7252 KB Output is correct
56 Correct 21 ms 6228 KB Output is correct
57 Correct 206 ms 10884 KB Output is correct
58 Correct 45 ms 10916 KB Output is correct
59 Correct 420 ms 31660 KB Output is correct
60 Correct 1224 ms 62796 KB Output is correct
61 Correct 525 ms 18508 KB Output is correct
62 Correct 391 ms 16276 KB Output is correct
63 Correct 490 ms 16088 KB Output is correct
64 Correct 1422 ms 37660 KB Output is correct
65 Correct 1247 ms 48428 KB Output is correct
66 Correct 1331 ms 63680 KB Output is correct
67 Correct 127 ms 16060 KB Output is correct
68 Correct 646 ms 39472 KB Output is correct
69 Correct 722 ms 40208 KB Output is correct
70 Correct 606 ms 38032 KB Output is correct