Submission #115758

# Submission time Handle Problem Language Result Execution time Memory
115758 2019-06-08T21:08:09 Z YazanZk Race (IOI11_race) C++14
100 / 100
1296 ms 39620 KB
#include <bits/stdc++.h>
typedef long long ll ;
using namespace std;
const int N = 200005;

vector < pair < int , int > > G[N];
int sub[N] , par[N];
bool blocked[N];
int n , k ;

void get_size(int u, int p){

    par[u] = p ;
    sub[u] = 1 ;

    for(auto ch : G[u]){
          int v = ch.first;
          if(v == p || blocked[v]) continue ;
          get_size(v , u);
          sub[u] += sub[v];
    }
}

set < pair < int , int > > se;

int dfs(int u , int p , ll w , int d){

    if(w > k) return n ;

    int ret = n ;

    if(w == k) ret = d;

    auto it = se.lower_bound({k - w , 0});
    if(it != se.end()){
          auto t = *it;
          if(t.first == k - w)
             ret = min(ret , d + t.second) ;
    }

    for(auto ch : G[u]){
          int v = ch.first , ww = ch.second ;
          if(v == p || blocked[v]) continue ;
          ret = min(ret , dfs(v , u , w + ww , d + 1));
    }

    return ret ;
}

void update(int u , int p , ll w , int d){

     if(w > k) return ;

     se.insert({w , d});

     for(auto ch : G[u]){
          int v = ch.first , ww = ch.second ;
          if(v == p || blocked[v]) continue ;
          update(v , u , w + ww , d + 1);
     }
}

int solve(int u){

    se.clear() ;

    int ret = n ;

    for(auto ch : G[u]){
          int v = ch.first , w = ch.second ;
          if(blocked[v]) continue ;
          ret = min(ret , dfs(v , u , w , 1));
          update(v , u , w , 1);
    }

    return ret;
}

int get_centroid(int src){

    get_size(src , src);

    queue < int > Q ;
    Q.push(src);

    int centroid = src , best = sub[src];

    while(!Q.empty()){
          int u = Q.front();
          Q.pop();

          int size = sub[src] - sub[u];
          for(auto ch : G[u]){
              int v = ch.first;
              if(v == par[u] || blocked[v]) continue ;
              Q.push(v);
              size = max(size , sub[v]);
          }

          if(best > size){
               best = size ;
               centroid = u ;
          }
    }

    blocked[centroid] = true ;

    int ret = solve(centroid);

    for(auto ch : G[centroid]){
          int v = ch.first;
          if(blocked[v]) continue ;
          ret = min(ret , get_centroid(v));
    }

    return ret;
}


int best_path(int nn , int kk , int H[][2] , int L[]) {

    n = nn ;
    k = kk ;

    vector < pair < int , int > > ve ;

    for(int i = 0 ; i < n - 1 ; i++){
          int u , v , w ;
          u = H[i][0] , v =  H[i][1] , w = L[i];
          G[u].push_back({v , w});
          G[v].push_back({u , w});
    }

    int ans = get_centroid(0);

    if(ans == n) ans = -1 ;

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 4992 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 7 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 8 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 7 ms 5120 KB Output is correct
35 Correct 7 ms 5120 KB Output is correct
36 Correct 7 ms 5120 KB Output is correct
37 Correct 7 ms 5120 KB Output is correct
38 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 4992 KB Output is correct
19 Correct 261 ms 10780 KB Output is correct
20 Correct 272 ms 12152 KB Output is correct
21 Correct 298 ms 12152 KB Output is correct
22 Correct 281 ms 12380 KB Output is correct
23 Correct 142 ms 12664 KB Output is correct
24 Correct 120 ms 12608 KB Output is correct
25 Correct 312 ms 14980 KB Output is correct
26 Correct 257 ms 22412 KB Output is correct
27 Correct 275 ms 19576 KB Output is correct
28 Correct 590 ms 30968 KB Output is correct
29 Correct 736 ms 30060 KB Output is correct
30 Correct 285 ms 19648 KB Output is correct
31 Correct 272 ms 19704 KB Output is correct
32 Correct 353 ms 19760 KB Output is correct
33 Correct 421 ms 18460 KB Output is correct
34 Correct 369 ms 19448 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 5120 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 6 ms 5120 KB Output is correct
5 Correct 6 ms 4992 KB Output is correct
6 Correct 5 ms 4992 KB Output is correct
7 Correct 5 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 5 ms 5120 KB Output is correct
10 Correct 6 ms 5120 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 4992 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 4992 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 4992 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 7 ms 5120 KB Output is correct
25 Correct 8 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 5120 KB Output is correct
28 Correct 7 ms 5120 KB Output is correct
29 Correct 7 ms 5120 KB Output is correct
30 Correct 7 ms 5120 KB Output is correct
31 Correct 7 ms 5120 KB Output is correct
32 Correct 8 ms 5120 KB Output is correct
33 Correct 8 ms 5120 KB Output is correct
34 Correct 7 ms 5120 KB Output is correct
35 Correct 7 ms 5120 KB Output is correct
36 Correct 7 ms 5120 KB Output is correct
37 Correct 7 ms 5120 KB Output is correct
38 Correct 7 ms 5120 KB Output is correct
39 Correct 261 ms 10780 KB Output is correct
40 Correct 272 ms 12152 KB Output is correct
41 Correct 298 ms 12152 KB Output is correct
42 Correct 281 ms 12380 KB Output is correct
43 Correct 142 ms 12664 KB Output is correct
44 Correct 120 ms 12608 KB Output is correct
45 Correct 312 ms 14980 KB Output is correct
46 Correct 257 ms 22412 KB Output is correct
47 Correct 275 ms 19576 KB Output is correct
48 Correct 590 ms 30968 KB Output is correct
49 Correct 736 ms 30060 KB Output is correct
50 Correct 285 ms 19648 KB Output is correct
51 Correct 272 ms 19704 KB Output is correct
52 Correct 353 ms 19760 KB Output is correct
53 Correct 421 ms 18460 KB Output is correct
54 Correct 369 ms 19448 KB Output is correct
55 Correct 25 ms 5888 KB Output is correct
56 Correct 24 ms 5880 KB Output is correct
57 Correct 159 ms 12532 KB Output is correct
58 Correct 61 ms 12500 KB Output is correct
59 Correct 286 ms 22572 KB Output is correct
60 Correct 1144 ms 39620 KB Output is correct
61 Correct 338 ms 20984 KB Output is correct
62 Correct 771 ms 22008 KB Output is correct
63 Correct 546 ms 22136 KB Output is correct
64 Correct 1296 ms 28536 KB Output is correct
65 Correct 361 ms 20600 KB Output is correct
66 Correct 854 ms 28536 KB Output is correct
67 Correct 198 ms 21092 KB Output is correct
68 Correct 584 ms 24660 KB Output is correct
69 Correct 525 ms 24696 KB Output is correct
70 Correct 514 ms 23800 KB Output is correct