Submission #12923

# Submission time Handle Problem Language Result Execution time Memory
12923 2015-01-20T11:20:26 Z gs14004 Race (IOI11_race) C++
43 / 100
3000 ms 29680 KB
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
typedef pair<int,int> pi;

vector<pi> graph[200005];

int ret = 1e9;
int size[200005], maxtree[200005];
int kill[200005];
int k;

vector<int> new_vis;

int dfs(int p, int pa){
    if(kill[p]) return 0;
    new_vis.push_back(p);
    int cs = 1;
    int maxv = 0;
    for (int i=0; i<graph[p].size(); i++) {
        if(graph[p][i].second == pa) continue;
        int t = dfs(graph[p][i].second,p);
        maxv = max(t,maxv);
        cs += t;
    }
    maxtree[p] = maxv;
    return size[p] = cs;
}

int get_mid(int p){
    for (int i=0; i<new_vis.size(); i++) {
        size[new_vis[i]] = 0;
        maxtree[new_vis[i]] = 0;
    }
    new_vis.clear();
    int full = dfs(p,-1);
    int mid_pos = 0, mid_res =1e9;
    for (int i=0; i<new_vis.size(); i++) {
        maxtree[new_vis[i]] = max(maxtree[new_vis[i]],full - size[new_vis[i]]);
        if(mid_res > maxtree[new_vis[i]]){
            mid_pos = new_vis[i];
            mid_res = maxtree[mid_pos];
        }
    }
    return mid_pos;
}

int *v;

void solve(int p){
    int mid = get_mid(p);
    kill[mid] = 1;
    memset(v,63,sizeof(int) * (k+1));
    v[0] = 0;
    for (int i=0; i<graph[mid].size(); i++) {
        if(kill[graph[mid][i].second]) continue;
        queue<int> q,d,p,l;
        vector<int> cd1, cd2;
        if(graph[mid][i].first > k) continue;
        q.push(graph[mid][i].second);
        d.push(graph[mid][i].first);
        l.push(1);
        p.push(mid);
        while (!q.empty()) {
            int qf = q.front();
            int df = d.front();
            int pf = p.front();
            int lf = l.front();
            cd1.push_back(df);
            cd2.push_back(lf);
            ret = min(v[k-df] + lf,ret);
            q.pop();
            d.pop();
            p.pop();
            l.pop();
            for (int i=0; i<graph[qf].size(); i++) {
                int pos = graph[qf][i].second;
                if(kill[pos] || pos == pf) continue;
                if(df + graph[qf][i].first > k) continue;
                q.push(pos);
                d.push(df + graph[qf][i].first);
                p.push(qf);
                l.push(lf + 1);
            }
        }
        for (int i=0; i<cd1.size(); i++) {
            v[cd1[i]] = min(v[cd1[i]],cd2[i]);
        }
    }
    for (int i=0; i<graph[mid].size(); i++) {
        if(kill[graph[mid][i].second]) continue;
        solve(graph[mid][i].second);
    }
}

int best_path(int N, int K, int H[][2], int L[]){
    for (int i=0; i<N-1; i++) {
        graph[H[i][0]].push_back(pi(L[i],H[i][1]));
        graph[H[i][1]].push_back(pi(L[i],H[i][0]));
    }
    v = (int*)calloc(K+1,sizeof(int));
    k = K;
    solve(0);
    if(ret > 1e8) return -1;
    return ret;
}

Compilation message

race.cpp: In function 'int dfs(int, int)':
race.cpp:25:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0; i<graph[p].size(); i++) {
                   ~^~~~~~~~~~~~~~~~
race.cpp: In function 'int get_mid(int)':
race.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0; i<new_vis.size(); i++) {
                   ~^~~~~~~~~~~~~~~
race.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0; i<new_vis.size(); i++) {
                   ~^~~~~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:60:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0; i<graph[mid].size(); i++) {
                   ~^~~~~~~~~~~~~~~~~~
race.cpp:81:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             for (int i=0; i<graph[qf].size(); i++) {
                           ~^~~~~~~~~~~~~~~~~
race.cpp:91:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i=0; i<cd1.size(); i++) {
                       ~^~~~~~~~~~~
race.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for (int i=0; i<graph[mid].size(); i++) {
                   ~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5092 KB Output is correct
3 Correct 6 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 6 ms 5144 KB Output is correct
6 Correct 6 ms 5144 KB Output is correct
7 Correct 6 ms 5144 KB Output is correct
8 Correct 6 ms 5220 KB Output is correct
9 Correct 5 ms 5220 KB Output is correct
10 Correct 5 ms 5220 KB Output is correct
11 Correct 5 ms 5220 KB Output is correct
12 Correct 6 ms 5220 KB Output is correct
13 Correct 6 ms 5248 KB Output is correct
14 Correct 6 ms 5248 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 6 ms 5304 KB Output is correct
18 Correct 5 ms 5304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5092 KB Output is correct
3 Correct 6 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 6 ms 5144 KB Output is correct
6 Correct 6 ms 5144 KB Output is correct
7 Correct 6 ms 5144 KB Output is correct
8 Correct 6 ms 5220 KB Output is correct
9 Correct 5 ms 5220 KB Output is correct
10 Correct 5 ms 5220 KB Output is correct
11 Correct 5 ms 5220 KB Output is correct
12 Correct 6 ms 5220 KB Output is correct
13 Correct 6 ms 5248 KB Output is correct
14 Correct 6 ms 5248 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 6 ms 5304 KB Output is correct
18 Correct 5 ms 5304 KB Output is correct
19 Correct 7 ms 5304 KB Output is correct
20 Correct 6 ms 5304 KB Output is correct
21 Correct 8 ms 5332 KB Output is correct
22 Correct 205 ms 9044 KB Output is correct
23 Correct 172 ms 9044 KB Output is correct
24 Correct 187 ms 9044 KB Output is correct
25 Correct 173 ms 9044 KB Output is correct
26 Correct 80 ms 9044 KB Output is correct
27 Correct 183 ms 9044 KB Output is correct
28 Correct 46 ms 9044 KB Output is correct
29 Correct 70 ms 9044 KB Output is correct
30 Correct 81 ms 9044 KB Output is correct
31 Correct 144 ms 9044 KB Output is correct
32 Correct 146 ms 9044 KB Output is correct
33 Correct 165 ms 9044 KB Output is correct
34 Correct 138 ms 9044 KB Output is correct
35 Correct 151 ms 9044 KB Output is correct
36 Correct 172 ms 9084 KB Output is correct
37 Correct 144 ms 9084 KB Output is correct
38 Correct 106 ms 9084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5092 KB Output is correct
3 Correct 6 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 6 ms 5144 KB Output is correct
6 Correct 6 ms 5144 KB Output is correct
7 Correct 6 ms 5144 KB Output is correct
8 Correct 6 ms 5220 KB Output is correct
9 Correct 5 ms 5220 KB Output is correct
10 Correct 5 ms 5220 KB Output is correct
11 Correct 5 ms 5220 KB Output is correct
12 Correct 6 ms 5220 KB Output is correct
13 Correct 6 ms 5248 KB Output is correct
14 Correct 6 ms 5248 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 6 ms 5304 KB Output is correct
18 Correct 5 ms 5304 KB Output is correct
19 Correct 251 ms 11768 KB Output is correct
20 Correct 236 ms 11784 KB Output is correct
21 Correct 249 ms 12020 KB Output is correct
22 Correct 262 ms 12120 KB Output is correct
23 Correct 178 ms 12120 KB Output is correct
24 Correct 139 ms 12120 KB Output is correct
25 Correct 326 ms 14560 KB Output is correct
26 Correct 183 ms 17908 KB Output is correct
27 Correct 298 ms 18544 KB Output is correct
28 Correct 457 ms 29680 KB Output is correct
29 Correct 486 ms 29680 KB Output is correct
30 Correct 356 ms 29680 KB Output is correct
31 Correct 364 ms 29680 KB Output is correct
32 Correct 376 ms 29680 KB Output is correct
33 Correct 477 ms 29680 KB Output is correct
34 Correct 393 ms 29680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4984 KB Output is correct
2 Correct 6 ms 5092 KB Output is correct
3 Correct 6 ms 5092 KB Output is correct
4 Correct 6 ms 5144 KB Output is correct
5 Correct 6 ms 5144 KB Output is correct
6 Correct 6 ms 5144 KB Output is correct
7 Correct 6 ms 5144 KB Output is correct
8 Correct 6 ms 5220 KB Output is correct
9 Correct 5 ms 5220 KB Output is correct
10 Correct 5 ms 5220 KB Output is correct
11 Correct 5 ms 5220 KB Output is correct
12 Correct 6 ms 5220 KB Output is correct
13 Correct 6 ms 5248 KB Output is correct
14 Correct 6 ms 5248 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 6 ms 5304 KB Output is correct
17 Correct 6 ms 5304 KB Output is correct
18 Correct 5 ms 5304 KB Output is correct
19 Correct 7 ms 5304 KB Output is correct
20 Correct 6 ms 5304 KB Output is correct
21 Correct 8 ms 5332 KB Output is correct
22 Correct 205 ms 9044 KB Output is correct
23 Correct 172 ms 9044 KB Output is correct
24 Correct 187 ms 9044 KB Output is correct
25 Correct 173 ms 9044 KB Output is correct
26 Correct 80 ms 9044 KB Output is correct
27 Correct 183 ms 9044 KB Output is correct
28 Correct 46 ms 9044 KB Output is correct
29 Correct 70 ms 9044 KB Output is correct
30 Correct 81 ms 9044 KB Output is correct
31 Correct 144 ms 9044 KB Output is correct
32 Correct 146 ms 9044 KB Output is correct
33 Correct 165 ms 9044 KB Output is correct
34 Correct 138 ms 9044 KB Output is correct
35 Correct 151 ms 9044 KB Output is correct
36 Correct 172 ms 9084 KB Output is correct
37 Correct 144 ms 9084 KB Output is correct
38 Correct 106 ms 9084 KB Output is correct
39 Correct 251 ms 11768 KB Output is correct
40 Correct 236 ms 11784 KB Output is correct
41 Correct 249 ms 12020 KB Output is correct
42 Correct 262 ms 12120 KB Output is correct
43 Correct 178 ms 12120 KB Output is correct
44 Correct 139 ms 12120 KB Output is correct
45 Correct 326 ms 14560 KB Output is correct
46 Correct 183 ms 17908 KB Output is correct
47 Correct 298 ms 18544 KB Output is correct
48 Correct 457 ms 29680 KB Output is correct
49 Correct 486 ms 29680 KB Output is correct
50 Correct 356 ms 29680 KB Output is correct
51 Correct 364 ms 29680 KB Output is correct
52 Correct 376 ms 29680 KB Output is correct
53 Correct 477 ms 29680 KB Output is correct
54 Correct 393 ms 29680 KB Output is correct
55 Correct 25 ms 29680 KB Output is correct
56 Correct 26 ms 29680 KB Output is correct
57 Correct 166 ms 29680 KB Output is correct
58 Correct 90 ms 29680 KB Output is correct
59 Execution timed out 3037 ms 29680 KB Time limit exceeded
60 Halted 0 ms 0 KB -