Submission #116287

# Submission time Handle Problem Language Result Execution time Memory
116287 2019-06-12T03:03:01 Z FieryPhoenix Race (IOI11_race) C++11
100 / 100
560 ms 33004 KB
#include "race.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 200001
#define MOD 1000000007

int N,K;
int H[200001][2];
int L[200001];
int subsize[200001];
vector<pair<int,int>>adj[200001];
bool dead[200001];
int dp[1000001]; //min edges to obtain length i
int temp[1000001];
int ans=INF;

int dfs(int node, int par){
  subsize[node]=1;
  for (int i=0;i<(int)adj[node].size();i++){
    int v=adj[node][i].first;
    if (!dead[v] && v!=par)
      subsize[node]+=dfs(v,node);
  }
  return subsize[node];
}

int dfs(int node, int par, int size){
  for (int i=0;i<(int)adj[node].size();i++){
    int v=adj[node][i].first;
    if (!dead[v] && v!=par && subsize[v]>size/2)
      return dfs(v,node,size);
  }
  return node;
}

vector<int>vec;
vector<int>vec2;

void dfs2(int node, int par, int numEdges, ll dis){
  if (dis>K)
    return;
  vec.push_back(dis);
  vec2.push_back(dis);
  temp[dis]=min(temp[dis],(int)numEdges);
  for (int i=0;i<(int)adj[node].size();i++){
    int v=adj[node][i].first;
    if (!dead[v] && v!=par)
      dfs2(v,node,numEdges+1,dis+adj[node][i].second);
  }
}

void solve(int node){
  int size=dfs(node,node);
  int centroid=dfs(node,node,size);
  dead[centroid]=true;
  for (int i=0;i<(int)vec2.size();i++)
    dp[vec2[i]]=INF;
  vec2.clear();
  for (int i=0;i<(int)adj[centroid].size();i++){
    int v=adj[centroid][i].first;
    if (!dead[v]){
      dfs2(v,centroid,1,adj[centroid][i].second);
      for (int j=0;j<(int)vec.size();j++)
	ans=min(ans,temp[vec[j]]+dp[K-vec[j]]);
      for (int j=0;j<(int)vec.size();j++){
	dp[vec[j]]=min(dp[vec[j]],temp[vec[j]]);
	temp[vec[j]]=INF;
      }
      vec.clear();
    }
  }
  for (int i=0;i<(int)adj[centroid].size();i++){
    int v=adj[centroid][i].first;
    if (!dead[v])
      solve(v);
  }
}

int best_path(int n, int k, int H[][2], int L[]){
  N=n;
  K=k;
  for (int i=0;i+1<N;i++){
    adj[H[i][0]].push_back({H[i][1],L[i]});
    adj[H[i][1]].push_back({H[i][0],L[i]});
  }
  for (int i=1;i<=K;i++)
    dp[i]=INF;
  for (int i=1;i<=K;i++)
    temp[i]=INF;
  solve(0);
  if (ans==INF)
    return -1;
  else
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 5 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 5 ms 4992 KB Output is correct
19 Correct 5 ms 4992 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 12 ms 12288 KB Output is correct
23 Correct 11 ms 11008 KB Output is correct
24 Correct 11 ms 11904 KB Output is correct
25 Correct 12 ms 11776 KB Output is correct
26 Correct 9 ms 7808 KB Output is correct
27 Correct 11 ms 11392 KB Output is correct
28 Correct 8 ms 6656 KB Output is correct
29 Correct 8 ms 7680 KB Output is correct
30 Correct 9 ms 8064 KB Output is correct
31 Correct 11 ms 10240 KB Output is correct
32 Correct 11 ms 10752 KB Output is correct
33 Correct 12 ms 11264 KB Output is correct
34 Correct 10 ms 9728 KB Output is correct
35 Correct 12 ms 11520 KB Output is correct
36 Correct 12 ms 12416 KB Output is correct
37 Correct 11 ms 11392 KB Output is correct
38 Correct 10 ms 9088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 5 ms 4992 KB Output is correct
19 Correct 141 ms 10596 KB Output is correct
20 Correct 147 ms 10488 KB Output is correct
21 Correct 149 ms 10764 KB Output is correct
22 Correct 154 ms 11284 KB Output is correct
23 Correct 109 ms 10680 KB Output is correct
24 Correct 73 ms 10592 KB Output is correct
25 Correct 171 ms 12492 KB Output is correct
26 Correct 94 ms 15472 KB Output is correct
27 Correct 225 ms 15992 KB Output is correct
28 Correct 413 ms 24056 KB Output is correct
29 Correct 459 ms 23544 KB Output is correct
30 Correct 235 ms 16120 KB Output is correct
31 Correct 239 ms 16112 KB Output is correct
32 Correct 283 ms 16032 KB Output is correct
33 Correct 337 ms 14712 KB Output is correct
34 Correct 289 ms 14748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 4992 KB Output is correct
2 Correct 6 ms 5120 KB Output is correct
3 Correct 6 ms 5120 KB Output is correct
4 Correct 5 ms 4992 KB Output is correct
5 Correct 5 ms 4992 KB Output is correct
6 Correct 6 ms 4992 KB Output is correct
7 Correct 6 ms 4992 KB Output is correct
8 Correct 6 ms 5120 KB Output is correct
9 Correct 6 ms 5120 KB Output is correct
10 Correct 6 ms 4992 KB Output is correct
11 Correct 6 ms 5120 KB Output is correct
12 Correct 6 ms 5120 KB Output is correct
13 Correct 6 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 4992 KB Output is correct
16 Correct 6 ms 5120 KB Output is correct
17 Correct 6 ms 4992 KB Output is correct
18 Correct 5 ms 4992 KB Output is correct
19 Correct 5 ms 4992 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 7 ms 5120 KB Output is correct
22 Correct 12 ms 12288 KB Output is correct
23 Correct 11 ms 11008 KB Output is correct
24 Correct 11 ms 11904 KB Output is correct
25 Correct 12 ms 11776 KB Output is correct
26 Correct 9 ms 7808 KB Output is correct
27 Correct 11 ms 11392 KB Output is correct
28 Correct 8 ms 6656 KB Output is correct
29 Correct 8 ms 7680 KB Output is correct
30 Correct 9 ms 8064 KB Output is correct
31 Correct 11 ms 10240 KB Output is correct
32 Correct 11 ms 10752 KB Output is correct
33 Correct 12 ms 11264 KB Output is correct
34 Correct 10 ms 9728 KB Output is correct
35 Correct 12 ms 11520 KB Output is correct
36 Correct 12 ms 12416 KB Output is correct
37 Correct 11 ms 11392 KB Output is correct
38 Correct 10 ms 9088 KB Output is correct
39 Correct 141 ms 10596 KB Output is correct
40 Correct 147 ms 10488 KB Output is correct
41 Correct 149 ms 10764 KB Output is correct
42 Correct 154 ms 11284 KB Output is correct
43 Correct 109 ms 10680 KB Output is correct
44 Correct 73 ms 10592 KB Output is correct
45 Correct 171 ms 12492 KB Output is correct
46 Correct 94 ms 15472 KB Output is correct
47 Correct 225 ms 15992 KB Output is correct
48 Correct 413 ms 24056 KB Output is correct
49 Correct 459 ms 23544 KB Output is correct
50 Correct 235 ms 16120 KB Output is correct
51 Correct 239 ms 16112 KB Output is correct
52 Correct 283 ms 16032 KB Output is correct
53 Correct 337 ms 14712 KB Output is correct
54 Correct 289 ms 14748 KB Output is correct
55 Correct 15 ms 5760 KB Output is correct
56 Correct 16 ms 5760 KB Output is correct
57 Correct 81 ms 11380 KB Output is correct
58 Correct 44 ms 11520 KB Output is correct
59 Correct 101 ms 16988 KB Output is correct
60 Correct 560 ms 33004 KB Output is correct
61 Correct 224 ms 16480 KB Output is correct
62 Correct 241 ms 24824 KB Output is correct
63 Correct 319 ms 24824 KB Output is correct
64 Correct 548 ms 21160 KB Output is correct
65 Correct 295 ms 16076 KB Output is correct
66 Correct 555 ms 29808 KB Output is correct
67 Correct 149 ms 25956 KB Output is correct
68 Correct 351 ms 24544 KB Output is correct
69 Correct 333 ms 24652 KB Output is correct
70 Correct 330 ms 24180 KB Output is correct