Submission #419453

# Submission time Handle Problem Language Result Execution time Memory
419453 2021-06-07T06:56:32 Z pliam Race (IOI11_race) C++14
100 / 100
560 ms 104648 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define INF (int)1000000005
typedef long long ll;

ll k;
int ans;

struct node{
  map<ll,int> res;//(length,edges)
  ll add_l;
  int add_e;
  node(){
    res[0]=0;
    add_l=add_e=0;
  }
  void merge(node n,ll w){
    for(auto p:n.res){
      ll l=p.first+n.add_l+w;
      int e=p.second+n.add_e+1;
      if(res.count(k-l-add_l)) ans=min(ans,res[k-l-add_l]+add_e+e);
    }
    for(auto p:n.res){
      ll l=p.first+n.add_l+w-add_l;
      int e=p.second+n.add_e+1-add_e;
      if(res.count(l)) res[l]=min(res[l],e);
      else res[l]=e;
    }
  }
  void init_big(ll w){
    add_e+=1;
    add_l+=w;
    res[-add_l]=-add_e;
    if(res.count(k-add_l)) ans=min(ans,res[k-add_l]+add_e);
  }
};

node arr[MAXN];
int rep[MAXN];

vector<pair<int,ll>> adj[MAXN];//(to,weight)

void dfs(int curr,int par=-1){
  int big_c=-1;
  ll w_big=0;
  for(auto p:adj[curr]){
    int to=p.first;
    ll w=p.second;
    if(to==par) continue;
    dfs(to,curr);
    if(big_c==-1||arr[rep[big_c]].res.size()<arr[rep[to]].res.size()){
      big_c=to;
      w_big=w;
    }
  }
  if(big_c==-1){
    rep[curr]=curr;
    return;
  }else{
    rep[curr]=rep[big_c];
    arr[rep[big_c]].init_big(w_big);
  }
  for(auto p:adj[curr]){
    int to=p.first;
    ll w=p.second;
    if(to==par||to==big_c) continue;
    arr[rep[big_c]].merge(arr[rep[to]],w);
    rep[to]=rep[big_c];
  }
}

int best_path(int N, int K, int H[][2], int L[])
{
  ans=INF;
  k=K;
  for(int i=0;i<N-1;i++){
    adj[H[i][0]].push_back({H[i][1],L[i]});
    adj[H[i][1]].push_back({H[i][0],L[i]});
  }
  dfs(0);
  return ans==INF?-1:ans;
}

# Verdict Execution time Memory Grader output
1 Correct 29 ms 30048 KB Output is correct
2 Correct 25 ms 30040 KB Output is correct
3 Correct 25 ms 29960 KB Output is correct
4 Correct 26 ms 30052 KB Output is correct
5 Correct 25 ms 29972 KB Output is correct
6 Correct 26 ms 30024 KB Output is correct
7 Correct 25 ms 30020 KB Output is correct
8 Correct 25 ms 30036 KB Output is correct
9 Correct 25 ms 29992 KB Output is correct
10 Correct 31 ms 30028 KB Output is correct
11 Correct 27 ms 30028 KB Output is correct
12 Correct 25 ms 30028 KB Output is correct
13 Correct 25 ms 30088 KB Output is correct
14 Correct 25 ms 30004 KB Output is correct
15 Correct 26 ms 30032 KB Output is correct
16 Correct 26 ms 30036 KB Output is correct
17 Correct 24 ms 30028 KB Output is correct
18 Correct 24 ms 30028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 30048 KB Output is correct
2 Correct 25 ms 30040 KB Output is correct
3 Correct 25 ms 29960 KB Output is correct
4 Correct 26 ms 30052 KB Output is correct
5 Correct 25 ms 29972 KB Output is correct
6 Correct 26 ms 30024 KB Output is correct
7 Correct 25 ms 30020 KB Output is correct
8 Correct 25 ms 30036 KB Output is correct
9 Correct 25 ms 29992 KB Output is correct
10 Correct 31 ms 30028 KB Output is correct
11 Correct 27 ms 30028 KB Output is correct
12 Correct 25 ms 30028 KB Output is correct
13 Correct 25 ms 30088 KB Output is correct
14 Correct 25 ms 30004 KB Output is correct
15 Correct 26 ms 30032 KB Output is correct
16 Correct 26 ms 30036 KB Output is correct
17 Correct 24 ms 30028 KB Output is correct
18 Correct 24 ms 30028 KB Output is correct
19 Correct 25 ms 30052 KB Output is correct
20 Correct 29 ms 30032 KB Output is correct
21 Correct 27 ms 30148 KB Output is correct
22 Correct 27 ms 30324 KB Output is correct
23 Correct 26 ms 30212 KB Output is correct
24 Correct 26 ms 30304 KB Output is correct
25 Correct 26 ms 30268 KB Output is correct
26 Correct 26 ms 30320 KB Output is correct
27 Correct 26 ms 30104 KB Output is correct
28 Correct 27 ms 30236 KB Output is correct
29 Correct 26 ms 30272 KB Output is correct
30 Correct 27 ms 30320 KB Output is correct
31 Correct 29 ms 30228 KB Output is correct
32 Correct 26 ms 30180 KB Output is correct
33 Correct 28 ms 30312 KB Output is correct
34 Correct 26 ms 30260 KB Output is correct
35 Correct 31 ms 30152 KB Output is correct
36 Correct 25 ms 30184 KB Output is correct
37 Correct 24 ms 30188 KB Output is correct
38 Correct 26 ms 30292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 30048 KB Output is correct
2 Correct 25 ms 30040 KB Output is correct
3 Correct 25 ms 29960 KB Output is correct
4 Correct 26 ms 30052 KB Output is correct
5 Correct 25 ms 29972 KB Output is correct
6 Correct 26 ms 30024 KB Output is correct
7 Correct 25 ms 30020 KB Output is correct
8 Correct 25 ms 30036 KB Output is correct
9 Correct 25 ms 29992 KB Output is correct
10 Correct 31 ms 30028 KB Output is correct
11 Correct 27 ms 30028 KB Output is correct
12 Correct 25 ms 30028 KB Output is correct
13 Correct 25 ms 30088 KB Output is correct
14 Correct 25 ms 30004 KB Output is correct
15 Correct 26 ms 30032 KB Output is correct
16 Correct 26 ms 30036 KB Output is correct
17 Correct 24 ms 30028 KB Output is correct
18 Correct 24 ms 30028 KB Output is correct
19 Correct 161 ms 46480 KB Output is correct
20 Correct 155 ms 46404 KB Output is correct
21 Correct 165 ms 46272 KB Output is correct
22 Correct 161 ms 46020 KB Output is correct
23 Correct 234 ms 59472 KB Output is correct
24 Correct 177 ms 49472 KB Output is correct
25 Correct 109 ms 49284 KB Output is correct
26 Correct 82 ms 57900 KB Output is correct
27 Correct 253 ms 53436 KB Output is correct
28 Correct 292 ms 98796 KB Output is correct
29 Correct 285 ms 97616 KB Output is correct
30 Correct 256 ms 53416 KB Output is correct
31 Correct 231 ms 53516 KB Output is correct
32 Correct 291 ms 53540 KB Output is correct
33 Correct 252 ms 58416 KB Output is correct
34 Correct 431 ms 91880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 30048 KB Output is correct
2 Correct 25 ms 30040 KB Output is correct
3 Correct 25 ms 29960 KB Output is correct
4 Correct 26 ms 30052 KB Output is correct
5 Correct 25 ms 29972 KB Output is correct
6 Correct 26 ms 30024 KB Output is correct
7 Correct 25 ms 30020 KB Output is correct
8 Correct 25 ms 30036 KB Output is correct
9 Correct 25 ms 29992 KB Output is correct
10 Correct 31 ms 30028 KB Output is correct
11 Correct 27 ms 30028 KB Output is correct
12 Correct 25 ms 30028 KB Output is correct
13 Correct 25 ms 30088 KB Output is correct
14 Correct 25 ms 30004 KB Output is correct
15 Correct 26 ms 30032 KB Output is correct
16 Correct 26 ms 30036 KB Output is correct
17 Correct 24 ms 30028 KB Output is correct
18 Correct 24 ms 30028 KB Output is correct
19 Correct 25 ms 30052 KB Output is correct
20 Correct 29 ms 30032 KB Output is correct
21 Correct 27 ms 30148 KB Output is correct
22 Correct 27 ms 30324 KB Output is correct
23 Correct 26 ms 30212 KB Output is correct
24 Correct 26 ms 30304 KB Output is correct
25 Correct 26 ms 30268 KB Output is correct
26 Correct 26 ms 30320 KB Output is correct
27 Correct 26 ms 30104 KB Output is correct
28 Correct 27 ms 30236 KB Output is correct
29 Correct 26 ms 30272 KB Output is correct
30 Correct 27 ms 30320 KB Output is correct
31 Correct 29 ms 30228 KB Output is correct
32 Correct 26 ms 30180 KB Output is correct
33 Correct 28 ms 30312 KB Output is correct
34 Correct 26 ms 30260 KB Output is correct
35 Correct 31 ms 30152 KB Output is correct
36 Correct 25 ms 30184 KB Output is correct
37 Correct 24 ms 30188 KB Output is correct
38 Correct 26 ms 30292 KB Output is correct
39 Correct 161 ms 46480 KB Output is correct
40 Correct 155 ms 46404 KB Output is correct
41 Correct 165 ms 46272 KB Output is correct
42 Correct 161 ms 46020 KB Output is correct
43 Correct 234 ms 59472 KB Output is correct
44 Correct 177 ms 49472 KB Output is correct
45 Correct 109 ms 49284 KB Output is correct
46 Correct 82 ms 57900 KB Output is correct
47 Correct 253 ms 53436 KB Output is correct
48 Correct 292 ms 98796 KB Output is correct
49 Correct 285 ms 97616 KB Output is correct
50 Correct 256 ms 53416 KB Output is correct
51 Correct 231 ms 53516 KB Output is correct
52 Correct 291 ms 53540 KB Output is correct
53 Correct 252 ms 58416 KB Output is correct
54 Correct 431 ms 91880 KB Output is correct
55 Correct 41 ms 32576 KB Output is correct
56 Correct 34 ms 31300 KB Output is correct
57 Correct 94 ms 44452 KB Output is correct
58 Correct 84 ms 37372 KB Output is correct
59 Correct 104 ms 64196 KB Output is correct
60 Correct 256 ms 97744 KB Output is correct
61 Correct 263 ms 57036 KB Output is correct
62 Correct 246 ms 53464 KB Output is correct
63 Correct 268 ms 53600 KB Output is correct
64 Correct 560 ms 104648 KB Output is correct
65 Correct 544 ms 104312 KB Output is correct
66 Correct 308 ms 95172 KB Output is correct
67 Correct 245 ms 46396 KB Output is correct
68 Correct 363 ms 69808 KB Output is correct
69 Correct 411 ms 69964 KB Output is correct
70 Correct 385 ms 67908 KB Output is correct