Submission #59513

# Submission time Handle Problem Language Result Execution time Memory
59513 2018-07-22T08:13:26 Z Vahan Race (IOI11_race) C++17
100 / 100
1384 ms 81648 KB
#include "race.h"
#include<vector>
#include<iostream>
using namespace std;
#define mp make_pair
vector< pair<int,int> > g[300000];
int n,rr,s[300000],k,tm[1000008],us[300000];
const int MAXH=1000000;
void dfs(int v,int p)
{
    n++;
    s[v]=0;
    for(int i=0;i<g[v].size();i++)
    {
        int to=g[v][i].first;
        if(to==p || us[to])
            continue;
        dfs(to,v);
    }
}
int centr(int v,int p)
{
    s[v]=1;
    int e=-1;
    for(int i=0;i<g[v].size();i++)
    {
        int to=g[v][i].first;
        if(to==p || us[to])
            continue;
        centr(to,v);
        s[v]+=s[to];
        e=max(e,s[to]);
    }
    e=max(e,n-s[v]);
    if(e<=n/2)
        rr=v;
}
void dfsh(int v,int p,int h,int qan)
{
    if(h<=k)
    {
        if(tm[k-h]>=0)
        {
            if(tm[k]==-1)
                tm[k]=tm[k-h]+qan;
            tm[k]=min(tm[k],tm[k-h]+qan);
        }
    }
    for(int i=0;i<g[v].size();i++)
    {
        int to=g[v][i].first;
        int dis=g[v][i].second;
        if(to==p || us[to])
            continue;
        dfsh(to,v,dis+h,qan+1);
    }
}
void dfsm(int v,int p,int h,int qan)
{
    if(h<MAXH)
    {
        if(tm[h]==-1)
            tm[h]=qan;
        tm[h]=min(tm[h],qan);
    }
    for(int i=0;i<g[v].size();i++)
    {
        int to=g[v][i].first;
        int dis=g[v][i].second;
        if(to==p || us[to])
            continue;
        dfsm(to,v,dis+h,qan+1);
    }
}
void dfsg(int v,int p,int h)
{
    if(h<=MAXH && h!=k)
        tm[h]=-1;
    for(int i=0;i<g[v].size();i++)
    {
        int to=g[v][i].first;
        int dis=g[v][i].second;
        if(to==p || us[to])
            continue;
        dfsg(to,v,dis+h);
    }
}
void ans(int v,int p)
{
    rr=0;
    n=0;
    dfs(v,p);
    centr(v,p);
    int r=rr;
    us[r]=1;
    for(int i=0;i<g[r].size();i++)
    {
        int to=g[r][i].first;
        int dis=g[r][i].second;
        if(to==p || us[to])
            continue;
        dfsh(to,r,dis,1);
        dfsm(to,r,dis,1);
    }
    for(int i=0;i<g[r].size();i++)
    {
        int to=g[r][i].first;
        int dis=g[r][i].second;
        if(to==p || us[to])
            continue;
        dfsg(to,r,dis);
    }
    for(int i=0;i<g[r].size();i++)
    {
        int to=g[r][i].first;
        int dis=g[r][i].second;
        if(to==p || us[to])
            continue;
        ans(to,r);
    }
    for(int i=0;i<g[r].size();i++)
    {
        int to=g[r][i].first;
        int dis=g[r][i].second;
        if(to==p || us[to])
            continue;
        dfsh(to,r,dis,1);
        dfsm(to,r,dis,1);
        ans(to,r);
    }
}
int best_path(int N, int K, int H[][2], int L[])
{
    k=K;
    tm[0]=0;
    for(int i=1;i<=k;i++)
        tm[i]=-1;
    for(int i=0;i<N-1;i++)
    {
        g[H[i][0]].push_back(mp(H[i][1],L[i]));
        g[H[i][1]].push_back(mp(H[i][0],L[i]));
    }
    ans(0,-1);
    return tm[k];
}
/*
TEST1
4 3
0 1 1
1 2 2
1 3 4
2
TEST2
3 3
0 1 1
1 2 1
-1
TEST3
11 12
0 1 3
0 2 4
2 3 5
3 4 4
4 5 6
0 6 3
6 7 2
6 8 5
8 9 6
8 10 7
2
*/


Compilation message

race.cpp: In function 'void dfs(int, int)':
race.cpp:13:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[v].size();i++)
                 ~^~~~~~~~~~~~
race.cpp: In function 'int centr(int, int)':
race.cpp:25:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[v].size();i++)
                 ~^~~~~~~~~~~~
race.cpp:37:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
race.cpp: In function 'void dfsh(int, int, int, int)':
race.cpp:49:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[v].size();i++)
                 ~^~~~~~~~~~~~
race.cpp: In function 'void dfsm(int, int, int, int)':
race.cpp:66:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[v].size();i++)
                 ~^~~~~~~~~~~~
race.cpp: In function 'void dfsg(int, int, int)':
race.cpp:79:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[v].size();i++)
                 ~^~~~~~~~~~~~
race.cpp: In function 'void ans(int, int)':
race.cpp:96:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[r].size();i++)
                 ~^~~~~~~~~~~~
race.cpp:105:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[r].size();i++)
                 ~^~~~~~~~~~~~
race.cpp:113:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[r].size();i++)
                 ~^~~~~~~~~~~~
race.cpp:116:13: warning: unused variable 'dis' [-Wunused-variable]
         int dis=g[r][i].second;
             ^~~
race.cpp:121:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0;i<g[r].size();i++)
                 ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 11 ms 7528 KB Output is correct
3 Correct 13 ms 7528 KB Output is correct
4 Correct 11 ms 7528 KB Output is correct
5 Correct 9 ms 7548 KB Output is correct
6 Correct 10 ms 7548 KB Output is correct
7 Correct 9 ms 7732 KB Output is correct
8 Correct 10 ms 7732 KB Output is correct
9 Correct 10 ms 7732 KB Output is correct
10 Correct 10 ms 7732 KB Output is correct
11 Correct 11 ms 7732 KB Output is correct
12 Correct 11 ms 7736 KB Output is correct
13 Correct 9 ms 7736 KB Output is correct
14 Correct 8 ms 7736 KB Output is correct
15 Correct 8 ms 7736 KB Output is correct
16 Correct 11 ms 7736 KB Output is correct
17 Correct 10 ms 7740 KB Output is correct
18 Correct 9 ms 7832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 11 ms 7528 KB Output is correct
3 Correct 13 ms 7528 KB Output is correct
4 Correct 11 ms 7528 KB Output is correct
5 Correct 9 ms 7548 KB Output is correct
6 Correct 10 ms 7548 KB Output is correct
7 Correct 9 ms 7732 KB Output is correct
8 Correct 10 ms 7732 KB Output is correct
9 Correct 10 ms 7732 KB Output is correct
10 Correct 10 ms 7732 KB Output is correct
11 Correct 11 ms 7732 KB Output is correct
12 Correct 11 ms 7736 KB Output is correct
13 Correct 9 ms 7736 KB Output is correct
14 Correct 8 ms 7736 KB Output is correct
15 Correct 8 ms 7736 KB Output is correct
16 Correct 11 ms 7736 KB Output is correct
17 Correct 10 ms 7740 KB Output is correct
18 Correct 9 ms 7832 KB Output is correct
19 Correct 9 ms 7832 KB Output is correct
20 Correct 9 ms 7832 KB Output is correct
21 Correct 10 ms 7868 KB Output is correct
22 Correct 15 ms 11864 KB Output is correct
23 Correct 16 ms 11864 KB Output is correct
24 Correct 15 ms 11864 KB Output is correct
25 Correct 14 ms 11864 KB Output is correct
26 Correct 16 ms 11864 KB Output is correct
27 Correct 14 ms 11864 KB Output is correct
28 Correct 13 ms 11864 KB Output is correct
29 Correct 12 ms 11864 KB Output is correct
30 Correct 11 ms 11864 KB Output is correct
31 Correct 14 ms 11864 KB Output is correct
32 Correct 15 ms 11864 KB Output is correct
33 Correct 14 ms 11864 KB Output is correct
34 Correct 13 ms 11864 KB Output is correct
35 Correct 14 ms 11864 KB Output is correct
36 Correct 16 ms 11896 KB Output is correct
37 Correct 16 ms 11896 KB Output is correct
38 Correct 13 ms 11896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 11 ms 7528 KB Output is correct
3 Correct 13 ms 7528 KB Output is correct
4 Correct 11 ms 7528 KB Output is correct
5 Correct 9 ms 7548 KB Output is correct
6 Correct 10 ms 7548 KB Output is correct
7 Correct 9 ms 7732 KB Output is correct
8 Correct 10 ms 7732 KB Output is correct
9 Correct 10 ms 7732 KB Output is correct
10 Correct 10 ms 7732 KB Output is correct
11 Correct 11 ms 7732 KB Output is correct
12 Correct 11 ms 7736 KB Output is correct
13 Correct 9 ms 7736 KB Output is correct
14 Correct 8 ms 7736 KB Output is correct
15 Correct 8 ms 7736 KB Output is correct
16 Correct 11 ms 7736 KB Output is correct
17 Correct 10 ms 7740 KB Output is correct
18 Correct 9 ms 7832 KB Output is correct
19 Correct 400 ms 15196 KB Output is correct
20 Correct 448 ms 16480 KB Output is correct
21 Correct 314 ms 17704 KB Output is correct
22 Correct 388 ms 19328 KB Output is correct
23 Correct 388 ms 20880 KB Output is correct
24 Correct 143 ms 22240 KB Output is correct
25 Correct 350 ms 27024 KB Output is correct
26 Correct 224 ms 31952 KB Output is correct
27 Correct 400 ms 33432 KB Output is correct
28 Correct 988 ms 54800 KB Output is correct
29 Correct 1000 ms 56840 KB Output is correct
30 Correct 428 ms 56840 KB Output is correct
31 Correct 393 ms 56840 KB Output is correct
32 Correct 489 ms 56840 KB Output is correct
33 Correct 868 ms 56840 KB Output is correct
34 Correct 1051 ms 57948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 7416 KB Output is correct
2 Correct 11 ms 7528 KB Output is correct
3 Correct 13 ms 7528 KB Output is correct
4 Correct 11 ms 7528 KB Output is correct
5 Correct 9 ms 7548 KB Output is correct
6 Correct 10 ms 7548 KB Output is correct
7 Correct 9 ms 7732 KB Output is correct
8 Correct 10 ms 7732 KB Output is correct
9 Correct 10 ms 7732 KB Output is correct
10 Correct 10 ms 7732 KB Output is correct
11 Correct 11 ms 7732 KB Output is correct
12 Correct 11 ms 7736 KB Output is correct
13 Correct 9 ms 7736 KB Output is correct
14 Correct 8 ms 7736 KB Output is correct
15 Correct 8 ms 7736 KB Output is correct
16 Correct 11 ms 7736 KB Output is correct
17 Correct 10 ms 7740 KB Output is correct
18 Correct 9 ms 7832 KB Output is correct
19 Correct 9 ms 7832 KB Output is correct
20 Correct 9 ms 7832 KB Output is correct
21 Correct 10 ms 7868 KB Output is correct
22 Correct 15 ms 11864 KB Output is correct
23 Correct 16 ms 11864 KB Output is correct
24 Correct 15 ms 11864 KB Output is correct
25 Correct 14 ms 11864 KB Output is correct
26 Correct 16 ms 11864 KB Output is correct
27 Correct 14 ms 11864 KB Output is correct
28 Correct 13 ms 11864 KB Output is correct
29 Correct 12 ms 11864 KB Output is correct
30 Correct 11 ms 11864 KB Output is correct
31 Correct 14 ms 11864 KB Output is correct
32 Correct 15 ms 11864 KB Output is correct
33 Correct 14 ms 11864 KB Output is correct
34 Correct 13 ms 11864 KB Output is correct
35 Correct 14 ms 11864 KB Output is correct
36 Correct 16 ms 11896 KB Output is correct
37 Correct 16 ms 11896 KB Output is correct
38 Correct 13 ms 11896 KB Output is correct
39 Correct 400 ms 15196 KB Output is correct
40 Correct 448 ms 16480 KB Output is correct
41 Correct 314 ms 17704 KB Output is correct
42 Correct 388 ms 19328 KB Output is correct
43 Correct 388 ms 20880 KB Output is correct
44 Correct 143 ms 22240 KB Output is correct
45 Correct 350 ms 27024 KB Output is correct
46 Correct 224 ms 31952 KB Output is correct
47 Correct 400 ms 33432 KB Output is correct
48 Correct 988 ms 54800 KB Output is correct
49 Correct 1000 ms 56840 KB Output is correct
50 Correct 428 ms 56840 KB Output is correct
51 Correct 393 ms 56840 KB Output is correct
52 Correct 489 ms 56840 KB Output is correct
53 Correct 868 ms 56840 KB Output is correct
54 Correct 1051 ms 57948 KB Output is correct
55 Correct 35 ms 57948 KB Output is correct
56 Correct 31 ms 57948 KB Output is correct
57 Correct 182 ms 57948 KB Output is correct
58 Correct 69 ms 57948 KB Output is correct
59 Correct 188 ms 61348 KB Output is correct
60 Correct 1384 ms 79844 KB Output is correct
61 Correct 422 ms 79844 KB Output is correct
62 Correct 501 ms 79844 KB Output is correct
63 Correct 486 ms 79844 KB Output is correct
64 Correct 1185 ms 79844 KB Output is correct
65 Correct 1054 ms 79844 KB Output is correct
66 Correct 1300 ms 81648 KB Output is correct
67 Correct 233 ms 81648 KB Output is correct
68 Correct 609 ms 81648 KB Output is correct
69 Correct 664 ms 81648 KB Output is correct
70 Correct 554 ms 81648 KB Output is correct