답안 #16694

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
16694 2015-09-06T07:29:50 Z suhgyuho_william 경주 (Race) (IOI11_race) C++
100 / 100
854 ms 35520 KB
#include "race.h"
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int n,k;
int answer = 999999999;
vector<pair<int,int> > edge[200002];
queue<pair<int,int> > q,qt;
int root[1000002],small[1000002];
bool check[200002];
queue<int> qv;
int downcnt[200002];
int max_subtree[200002];

int j,nextv,tmp;
pair<int,int> top;

int make_qv(int x){
    int i;

    check[x] = true;
    qv.push(x);
    downcnt[x] = max_subtree[x] = 0;
    for(i=0; i<edge[x].size(); i++){
        nextv = edge[x][i].first;
        if(!check[nextv]){
            tmp = make_qv(nextv);
            downcnt[x] += tmp;
            if(max_subtree[x] < tmp) max_subtree[x] = tmp;
        }
    }
    check[x] = false;
    return (downcnt[x]+1);
}

int find_middle(int x){
    int i,below_x;
    int big = 0,v;

    below_x = make_qv(x);
    while(!qv.empty()){
        v = qv.front();
        tmp = min(below_x - max_subtree[v],max_subtree[v]+1);
        if(big < tmp){
            big = tmp;
            x = v;
        }
        qv.pop();
    }

    return x;
}

void dfs(int v,int sum,int cnt){
    int i;

    if(sum > k) return;
    check[v] = true;
    q.push(make_pair(sum,cnt));
    qt.push(make_pair(sum,cnt));

    for(i=0; i<edge[v].size(); i++){
        nextv = edge[v][i].first;
        if(check[nextv]) continue;
        dfs(nextv,sum+edge[v][i].second,cnt+1);
    }
    check[v] = false;
}

// T(n) = 2T(n/2) + O(n);

void race(int x){
    int i;
    int v = find_middle(x);

    check[v] = true;

    root[0] = v;
    small[0] = 0;
    for(i=0; i<edge[v].size(); i++){
        nextv = edge[v][i].first;
        if(check[nextv]) continue;
        dfs(nextv,edge[v][i].second,1);

        while(!q.empty()){
            top = q.front();
            if(root[k-top.first] == v) answer = min(answer,small[k-top.first] + top.second);
            q.pop();
        }
        while(!qt.empty()){
            top = qt.front();
            qt.pop();

            if(root[top.first] != v){
                root[top.first] = v;
                small[top.first] = top.second;
            }else if(small[top.first] > top.second) small[top.first] = top.second;
        }
    }

    for(i=0; i<edge[v].size(); i++){
        nextv = edge[v][i].first;
        if(!check[nextv]) race(nextv);
    }
}

int best_path(int N, int K, int H[][2], int L[])
{
    n = N; k = K;
    for(j=0; j<n-1; j++){
        edge[H[j][0]].push_back(make_pair(H[j][1],L[j]));
        edge[H[j][1]].push_back(make_pair(H[j][0],L[j]));
    }
    for(j=0; j<=k; j++) root[j] = -1;

    race(0);

    if(answer == 999999999) answer = -1;
    return answer;
}

Compilation message

race.cpp: In function 'int make_qv(int)':
race.cpp:26:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<edge[x].size(); i++){
              ~^~~~~~~~~~~~~~~
race.cpp: In function 'int find_middle(int)':
race.cpp:39:9: warning: unused variable 'i' [-Wunused-variable]
     int i,below_x;
         ^
race.cpp: In function 'void dfs(int, int, int)':
race.cpp:64:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<edge[v].size(); i++){
              ~^~~~~~~~~~~~~~~
race.cpp: In function 'void race(int)':
race.cpp:82:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<edge[v].size(); i++){
              ~^~~~~~~~~~~~~~~
race.cpp:103:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=0; i<edge[v].size(); i++){
              ~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 5 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 5 ms 5144 KB Output is correct
6 Correct 6 ms 5392 KB Output is correct
7 Correct 6 ms 5392 KB Output is correct
8 Correct 5 ms 5392 KB Output is correct
9 Correct 6 ms 5392 KB Output is correct
10 Correct 5 ms 5392 KB Output is correct
11 Correct 5 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 5 ms 5392 KB Output is correct
14 Correct 7 ms 5392 KB Output is correct
15 Correct 5 ms 5468 KB Output is correct
16 Correct 6 ms 5468 KB Output is correct
17 Correct 6 ms 5468 KB Output is correct
18 Correct 6 ms 5468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 5 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 5 ms 5144 KB Output is correct
6 Correct 6 ms 5392 KB Output is correct
7 Correct 6 ms 5392 KB Output is correct
8 Correct 5 ms 5392 KB Output is correct
9 Correct 6 ms 5392 KB Output is correct
10 Correct 5 ms 5392 KB Output is correct
11 Correct 5 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 5 ms 5392 KB Output is correct
14 Correct 7 ms 5392 KB Output is correct
15 Correct 5 ms 5468 KB Output is correct
16 Correct 6 ms 5468 KB Output is correct
17 Correct 6 ms 5468 KB Output is correct
18 Correct 6 ms 5468 KB Output is correct
19 Correct 5 ms 5468 KB Output is correct
20 Correct 5 ms 5468 KB Output is correct
21 Correct 6 ms 5468 KB Output is correct
22 Correct 11 ms 11440 KB Output is correct
23 Correct 10 ms 11440 KB Output is correct
24 Correct 12 ms 11644 KB Output is correct
25 Correct 10 ms 11644 KB Output is correct
26 Correct 8 ms 11644 KB Output is correct
27 Correct 9 ms 11644 KB Output is correct
28 Correct 7 ms 11644 KB Output is correct
29 Correct 9 ms 11644 KB Output is correct
30 Correct 9 ms 11644 KB Output is correct
31 Correct 10 ms 11644 KB Output is correct
32 Correct 9 ms 11644 KB Output is correct
33 Correct 10 ms 11644 KB Output is correct
34 Correct 9 ms 11644 KB Output is correct
35 Correct 11 ms 11644 KB Output is correct
36 Correct 11 ms 12284 KB Output is correct
37 Correct 10 ms 12284 KB Output is correct
38 Correct 9 ms 12284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 5 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 5 ms 5144 KB Output is correct
6 Correct 6 ms 5392 KB Output is correct
7 Correct 6 ms 5392 KB Output is correct
8 Correct 5 ms 5392 KB Output is correct
9 Correct 6 ms 5392 KB Output is correct
10 Correct 5 ms 5392 KB Output is correct
11 Correct 5 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 5 ms 5392 KB Output is correct
14 Correct 7 ms 5392 KB Output is correct
15 Correct 5 ms 5468 KB Output is correct
16 Correct 6 ms 5468 KB Output is correct
17 Correct 6 ms 5468 KB Output is correct
18 Correct 6 ms 5468 KB Output is correct
19 Correct 178 ms 12284 KB Output is correct
20 Correct 184 ms 12284 KB Output is correct
21 Correct 191 ms 12284 KB Output is correct
22 Correct 179 ms 12284 KB Output is correct
23 Correct 146 ms 12284 KB Output is correct
24 Correct 99 ms 12284 KB Output is correct
25 Correct 229 ms 14204 KB Output is correct
26 Correct 141 ms 17532 KB Output is correct
27 Correct 241 ms 17776 KB Output is correct
28 Correct 464 ms 28964 KB Output is correct
29 Correct 446 ms 28964 KB Output is correct
30 Correct 239 ms 28964 KB Output is correct
31 Correct 252 ms 28964 KB Output is correct
32 Correct 300 ms 28964 KB Output is correct
33 Correct 520 ms 28964 KB Output is correct
34 Correct 357 ms 28964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 4984 KB Output is correct
2 Correct 5 ms 5092 KB Output is correct
3 Correct 5 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 5 ms 5144 KB Output is correct
6 Correct 6 ms 5392 KB Output is correct
7 Correct 6 ms 5392 KB Output is correct
8 Correct 5 ms 5392 KB Output is correct
9 Correct 6 ms 5392 KB Output is correct
10 Correct 5 ms 5392 KB Output is correct
11 Correct 5 ms 5392 KB Output is correct
12 Correct 6 ms 5392 KB Output is correct
13 Correct 5 ms 5392 KB Output is correct
14 Correct 7 ms 5392 KB Output is correct
15 Correct 5 ms 5468 KB Output is correct
16 Correct 6 ms 5468 KB Output is correct
17 Correct 6 ms 5468 KB Output is correct
18 Correct 6 ms 5468 KB Output is correct
19 Correct 5 ms 5468 KB Output is correct
20 Correct 5 ms 5468 KB Output is correct
21 Correct 6 ms 5468 KB Output is correct
22 Correct 11 ms 11440 KB Output is correct
23 Correct 10 ms 11440 KB Output is correct
24 Correct 12 ms 11644 KB Output is correct
25 Correct 10 ms 11644 KB Output is correct
26 Correct 8 ms 11644 KB Output is correct
27 Correct 9 ms 11644 KB Output is correct
28 Correct 7 ms 11644 KB Output is correct
29 Correct 9 ms 11644 KB Output is correct
30 Correct 9 ms 11644 KB Output is correct
31 Correct 10 ms 11644 KB Output is correct
32 Correct 9 ms 11644 KB Output is correct
33 Correct 10 ms 11644 KB Output is correct
34 Correct 9 ms 11644 KB Output is correct
35 Correct 11 ms 11644 KB Output is correct
36 Correct 11 ms 12284 KB Output is correct
37 Correct 10 ms 12284 KB Output is correct
38 Correct 9 ms 12284 KB Output is correct
39 Correct 178 ms 12284 KB Output is correct
40 Correct 184 ms 12284 KB Output is correct
41 Correct 191 ms 12284 KB Output is correct
42 Correct 179 ms 12284 KB Output is correct
43 Correct 146 ms 12284 KB Output is correct
44 Correct 99 ms 12284 KB Output is correct
45 Correct 229 ms 14204 KB Output is correct
46 Correct 141 ms 17532 KB Output is correct
47 Correct 241 ms 17776 KB Output is correct
48 Correct 464 ms 28964 KB Output is correct
49 Correct 446 ms 28964 KB Output is correct
50 Correct 239 ms 28964 KB Output is correct
51 Correct 252 ms 28964 KB Output is correct
52 Correct 300 ms 28964 KB Output is correct
53 Correct 520 ms 28964 KB Output is correct
54 Correct 357 ms 28964 KB Output is correct
55 Correct 18 ms 28964 KB Output is correct
56 Correct 19 ms 28964 KB Output is correct
57 Correct 133 ms 28964 KB Output is correct
58 Correct 48 ms 28964 KB Output is correct
59 Correct 143 ms 28964 KB Output is correct
60 Correct 620 ms 35520 KB Output is correct
61 Correct 251 ms 35520 KB Output is correct
62 Correct 278 ms 35520 KB Output is correct
63 Correct 425 ms 35520 KB Output is correct
64 Correct 854 ms 35520 KB Output is correct
65 Correct 375 ms 35520 KB Output is correct
66 Correct 801 ms 35520 KB Output is correct
67 Correct 159 ms 35520 KB Output is correct
68 Correct 541 ms 35520 KB Output is correct
69 Correct 525 ms 35520 KB Output is correct
70 Correct 416 ms 35520 KB Output is correct