Submission #592099

# Submission time Handle Problem Language Result Execution time Memory
592099 2022-07-08T13:44:11 Z Hackapie Race (IOI11_race) C++17
100 / 100
411 ms 48456 KB
#include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#include "race.h"
using namespace std;
int ans;
const ll lim=200005;
const ll lim2=2000000;
int vis[lim],sz[lim],ok[lim2],add[lim2];
vector<pair<int,int>> g[lim]; 
int kk;
int timer=1;
void dfs_sz(int node,int p){
  sz[node]=1;
  for(auto x:g[node]){
    if(x.first==p)continue;
    if(vis[x.first])continue;
    dfs_sz(x.first,node);
    sz[node]+=sz[x.first];
  }
}
int centroid(int &cnt,int node,int p){
  for(auto x:g[node]){
    if(x.first==p)continue;
    if(vis[x.first])continue;
    if(sz[x.first]>(cnt/2)){
      return centroid(cnt,x.first,node);
    }
  }
  return node;
}
void solve(int node,int p,ll dis,int cnt,vector<pair<ll,ll>> &info){
  if(dis<=kk && ok[kk-dis]==timer){                        //if a valid path exists
    ans=min(ans,cnt+add[kk-dis]);
  }
  if(dis<=kk){                         
    info.push_back({dis,cnt});                      //record information
    for(auto x:g[node]){
      if(x.first==p)continue;
      if(!vis[x.first]){
        solve(x.first,node,dis+x.second,cnt+1,info);
      }
    }
  }
}
void create(int root,int p){
  dfs_sz(root,p);                      //-> find size
  int c=centroid(sz[root],root,p);     //find new centroid
  vis[c]=true;                          //visited
  ++timer;
  ok[0]=timer;
  add[0]=0;                          //updating info
  for(auto x:g[c]){                   //for every subtree in centroid find paths ..will help in finding if there exists some path that posses through centrod
    if(vis[x.first])continue;
    if(x.first==p)continue;
    vector<pair<ll,ll>> v;
    solve(x.first,c,x.second,1,v);
    for(auto y:v){                       //updating info
      if(ok[y.first]!=timer){ 
        ok[y.first]=timer;
        add[y.first]=y.second;
      }else{
        if(add[y.first]>y.second){
          ok[y.first]=timer;
          add[y.first]=y.second;
        }
      }
    }
  }
  for(auto x:g[c]){    
    if(x.first==p)continue;      //centroid decom
    if(!vis[x.first])create(x.first,c);
  }
 
}
int best_path(int n, int k, int h[][2], int l[])
{
  ans=2*n;
  for(int i=1;i<=n;i++){
    g[i].clear();
    vis[i]=false;
    sz[i]=0;
    ok[i]=0;
    add[i]=0;
  }
  ok[0]=0;
  add[0]=0;
  for(int i=n;i<lim2;i++){
    ok[i]=0;
    add[i]=0;
  }
  timer=1;
  kk=k;
  for(int i=0;i<n-1;i++){
    g[h[i][0]].push_back({h[i][1],l[i]});
    g[h[i][1]].push_back({h[i][0],l[i]});
  }
  create(1,-1);
  if(ans==(2*n))ans=-1;
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 10 ms 20564 KB Output is correct
2 Correct 8 ms 20648 KB Output is correct
3 Correct 8 ms 20564 KB Output is correct
4 Correct 8 ms 20564 KB Output is correct
5 Correct 8 ms 20564 KB Output is correct
6 Correct 11 ms 20564 KB Output is correct
7 Correct 9 ms 20660 KB Output is correct
8 Correct 9 ms 20564 KB Output is correct
9 Correct 8 ms 20564 KB Output is correct
10 Correct 8 ms 20564 KB Output is correct
11 Correct 9 ms 20564 KB Output is correct
12 Correct 9 ms 20564 KB Output is correct
13 Correct 10 ms 20568 KB Output is correct
14 Correct 9 ms 20564 KB Output is correct
15 Correct 11 ms 20584 KB Output is correct
16 Correct 9 ms 20688 KB Output is correct
17 Correct 9 ms 20564 KB Output is correct
18 Correct 10 ms 20564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 20564 KB Output is correct
2 Correct 8 ms 20648 KB Output is correct
3 Correct 8 ms 20564 KB Output is correct
4 Correct 8 ms 20564 KB Output is correct
5 Correct 8 ms 20564 KB Output is correct
6 Correct 11 ms 20564 KB Output is correct
7 Correct 9 ms 20660 KB Output is correct
8 Correct 9 ms 20564 KB Output is correct
9 Correct 8 ms 20564 KB Output is correct
10 Correct 8 ms 20564 KB Output is correct
11 Correct 9 ms 20564 KB Output is correct
12 Correct 9 ms 20564 KB Output is correct
13 Correct 10 ms 20568 KB Output is correct
14 Correct 9 ms 20564 KB Output is correct
15 Correct 11 ms 20584 KB Output is correct
16 Correct 9 ms 20688 KB Output is correct
17 Correct 9 ms 20564 KB Output is correct
18 Correct 10 ms 20564 KB Output is correct
19 Correct 8 ms 20564 KB Output is correct
20 Correct 8 ms 20628 KB Output is correct
21 Correct 10 ms 20744 KB Output is correct
22 Correct 10 ms 20692 KB Output is correct
23 Correct 9 ms 20692 KB Output is correct
24 Correct 9 ms 20692 KB Output is correct
25 Correct 10 ms 20692 KB Output is correct
26 Correct 9 ms 20692 KB Output is correct
27 Correct 10 ms 20692 KB Output is correct
28 Correct 12 ms 20740 KB Output is correct
29 Correct 10 ms 20744 KB Output is correct
30 Correct 11 ms 20700 KB Output is correct
31 Correct 10 ms 20724 KB Output is correct
32 Correct 10 ms 20692 KB Output is correct
33 Correct 10 ms 20692 KB Output is correct
34 Correct 11 ms 20664 KB Output is correct
35 Correct 9 ms 20692 KB Output is correct
36 Correct 10 ms 20692 KB Output is correct
37 Correct 11 ms 20692 KB Output is correct
38 Correct 9 ms 20668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 20564 KB Output is correct
2 Correct 8 ms 20648 KB Output is correct
3 Correct 8 ms 20564 KB Output is correct
4 Correct 8 ms 20564 KB Output is correct
5 Correct 8 ms 20564 KB Output is correct
6 Correct 11 ms 20564 KB Output is correct
7 Correct 9 ms 20660 KB Output is correct
8 Correct 9 ms 20564 KB Output is correct
9 Correct 8 ms 20564 KB Output is correct
10 Correct 8 ms 20564 KB Output is correct
11 Correct 9 ms 20564 KB Output is correct
12 Correct 9 ms 20564 KB Output is correct
13 Correct 10 ms 20568 KB Output is correct
14 Correct 9 ms 20564 KB Output is correct
15 Correct 11 ms 20584 KB Output is correct
16 Correct 9 ms 20688 KB Output is correct
17 Correct 9 ms 20564 KB Output is correct
18 Correct 10 ms 20564 KB Output is correct
19 Correct 109 ms 26508 KB Output is correct
20 Correct 114 ms 26480 KB Output is correct
21 Correct 126 ms 26948 KB Output is correct
22 Correct 111 ms 27036 KB Output is correct
23 Correct 75 ms 26444 KB Output is correct
24 Correct 56 ms 26432 KB Output is correct
25 Correct 120 ms 32856 KB Output is correct
26 Correct 96 ms 36696 KB Output is correct
27 Correct 158 ms 35180 KB Output is correct
28 Correct 277 ms 48456 KB Output is correct
29 Correct 259 ms 43240 KB Output is correct
30 Correct 173 ms 35096 KB Output is correct
31 Correct 186 ms 35024 KB Output is correct
32 Correct 225 ms 35152 KB Output is correct
33 Correct 199 ms 33948 KB Output is correct
34 Correct 169 ms 34644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 20564 KB Output is correct
2 Correct 8 ms 20648 KB Output is correct
3 Correct 8 ms 20564 KB Output is correct
4 Correct 8 ms 20564 KB Output is correct
5 Correct 8 ms 20564 KB Output is correct
6 Correct 11 ms 20564 KB Output is correct
7 Correct 9 ms 20660 KB Output is correct
8 Correct 9 ms 20564 KB Output is correct
9 Correct 8 ms 20564 KB Output is correct
10 Correct 8 ms 20564 KB Output is correct
11 Correct 9 ms 20564 KB Output is correct
12 Correct 9 ms 20564 KB Output is correct
13 Correct 10 ms 20568 KB Output is correct
14 Correct 9 ms 20564 KB Output is correct
15 Correct 11 ms 20584 KB Output is correct
16 Correct 9 ms 20688 KB Output is correct
17 Correct 9 ms 20564 KB Output is correct
18 Correct 10 ms 20564 KB Output is correct
19 Correct 8 ms 20564 KB Output is correct
20 Correct 8 ms 20628 KB Output is correct
21 Correct 10 ms 20744 KB Output is correct
22 Correct 10 ms 20692 KB Output is correct
23 Correct 9 ms 20692 KB Output is correct
24 Correct 9 ms 20692 KB Output is correct
25 Correct 10 ms 20692 KB Output is correct
26 Correct 9 ms 20692 KB Output is correct
27 Correct 10 ms 20692 KB Output is correct
28 Correct 12 ms 20740 KB Output is correct
29 Correct 10 ms 20744 KB Output is correct
30 Correct 11 ms 20700 KB Output is correct
31 Correct 10 ms 20724 KB Output is correct
32 Correct 10 ms 20692 KB Output is correct
33 Correct 10 ms 20692 KB Output is correct
34 Correct 11 ms 20664 KB Output is correct
35 Correct 9 ms 20692 KB Output is correct
36 Correct 10 ms 20692 KB Output is correct
37 Correct 11 ms 20692 KB Output is correct
38 Correct 9 ms 20668 KB Output is correct
39 Correct 109 ms 26508 KB Output is correct
40 Correct 114 ms 26480 KB Output is correct
41 Correct 126 ms 26948 KB Output is correct
42 Correct 111 ms 27036 KB Output is correct
43 Correct 75 ms 26444 KB Output is correct
44 Correct 56 ms 26432 KB Output is correct
45 Correct 120 ms 32856 KB Output is correct
46 Correct 96 ms 36696 KB Output is correct
47 Correct 158 ms 35180 KB Output is correct
48 Correct 277 ms 48456 KB Output is correct
49 Correct 259 ms 43240 KB Output is correct
50 Correct 173 ms 35096 KB Output is correct
51 Correct 186 ms 35024 KB Output is correct
52 Correct 225 ms 35152 KB Output is correct
53 Correct 199 ms 33948 KB Output is correct
54 Correct 169 ms 34644 KB Output is correct
55 Correct 15 ms 21452 KB Output is correct
56 Correct 16 ms 21460 KB Output is correct
57 Correct 82 ms 29024 KB Output is correct
58 Correct 39 ms 27512 KB Output is correct
59 Correct 100 ms 36744 KB Output is correct
60 Correct 411 ms 48256 KB Output is correct
61 Correct 169 ms 35196 KB Output is correct
62 Correct 188 ms 35148 KB Output is correct
63 Correct 217 ms 35156 KB Output is correct
64 Correct 392 ms 37532 KB Output is correct
65 Correct 207 ms 35916 KB Output is correct
66 Correct 324 ms 47288 KB Output is correct
67 Correct 110 ms 35868 KB Output is correct
68 Correct 268 ms 35852 KB Output is correct
69 Correct 218 ms 36060 KB Output is correct
70 Correct 224 ms 35436 KB Output is correct