Submission #36296

# Submission time Handle Problem Language Result Execution time Memory
36296 2017-12-07T06:05:03 Z funcsr Race (IOI11_race) C++14
100 / 100
1976 ms 95280 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <cassert>
#include "race.h"
using namespace std;
#define INF 1145141919
#define rep(i, n) for (int i=0; i<(n); i++)
#define _1 first
#define _2 second
#define all(x) x.begin(), x.end()
#define pb push_back
typedef pair<int, int> P;
inline void chmin(int &x, int v) { if (x > v) x = v; }

int N, K;
int ans;
bool dead[200000];
vector<P> G[200000];

multiset<int> C[1000001];
void merge(vector<vector<P> > vs) {
  if (vs.empty()) return;
  for (vector<P> &ps : vs) for (P &x : ps) {
    if (x._1 == K) chmin(ans, x._2);
  }
  if (vs.size() < 2) return;
  //for (auto &p : vs) { cout<<"{"; for (P &x : p)cout<<"("<<x._1<<","<<x._2<<"),"; cout<<"},"; } cout<<"\n";

  for (vector<P> &ps : vs) for (P &x : ps) C[x._1].insert(x._2);
  for (vector<P> &ps : vs) {
    for (P &x : ps) C[x._1].erase(C[x._1].find(x._2));
    for (P &x : ps) {
      if (!C[K-x._1].empty()) chmin(ans, x._2 + *C[K-x._1].begin());
    }
    for (P &x : ps) C[x._1].insert(x._2);
  }
  for (vector<P> &ps : vs) for (P &x : ps) C[x._1].clear();
}

int sz[200000];
int all;
void dfs2(int x, int p) {
  sz[x] = 1;
  for (P pp : G[x]) {
    int t = pp._1;
    if (dead[t] || t == p) continue;
    dfs2(t, x);
    sz[x] += sz[t];
  }
}
P dfs3(int x, int p) {
  P mp = P(INF, -1);
  int mc = all-sz[x];
  for (P pp : G[x]) {
    int t = pp._1;
    if (dead[t] || t == p) continue;
    mp = min(mp, dfs3(t, x));
    mc = max(mc, sz[t]);
  }
  return min(mp, P(mc, x));
}

int centroid(int s) {
  dfs2(s, -1);
  all = sz[s];
  return dfs3(s, -1)._2;
}

void dfs(int x, int p, int d, int r, vector<P> &ret) {
  if (d > K) return;
  ret.pb(P(d, r));
  for (P pp : G[x]) {
    int t = pp._1;
    if (dead[t] || t == p) continue;
    dfs(t, x, d+pp._2, r+1, ret);
  }
}

vector<P> solve(int s) {
  vector<P> ret;
  dfs(s, -1, 0, 0, ret);

  int g = centroid(s);
  dead[g] = true;
  vector<vector<P> > vs;
  for (P pp : G[g]) {
    int t = pp._1;
    if (dead[t]) continue;
    vector<P> cs = solve(t);
    vector<P> new_cs;
    for (P x : cs) if (x._1+pp._2 <= K) new_cs.pb(P(x._1+pp._2, x._2+1));
    vs.pb(new_cs);
  }
  merge(vs);
  return ret;
}

int best_path(int n, int k, int H[][2], int L[]) {
  N = n, K = k;
  rep(i, N) G[i].clear(), dead[i] = false;
  rep(i, 1000001) C[i].clear();
  rep(i, N-1) {
    G[H[i][0]].pb(P(H[i][1], L[i]));
    G[H[i][1]].pb(P(H[i][0], L[i]));
  }
  ans = INF;

  int g = centroid(0);
  dead[g] = true;
  vector<vector<P> > vs;
  for (P pp : G[g]) {
    int t = pp._1;
    if (dead[t]) continue;
    vector<P> cs = solve(t);
    vector<P> new_cs;
    for (P x : cs) if (x._1+pp._2 <= K) new_cs.pb(P(x._1+pp._2, x._2+1));
    vs.pb(new_cs);
  }
  merge(vs);
  if (ans == INF) ans = -1;
  return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 50 ms 51960 KB Output is correct
2 Correct 60 ms 52068 KB Output is correct
3 Correct 53 ms 52144 KB Output is correct
4 Correct 56 ms 52144 KB Output is correct
5 Correct 51 ms 52220 KB Output is correct
6 Correct 52 ms 52220 KB Output is correct
7 Correct 53 ms 52256 KB Output is correct
8 Correct 51 ms 52256 KB Output is correct
9 Correct 50 ms 52280 KB Output is correct
10 Correct 54 ms 52280 KB Output is correct
11 Correct 57 ms 52280 KB Output is correct
12 Correct 60 ms 52280 KB Output is correct
13 Correct 56 ms 52280 KB Output is correct
14 Correct 57 ms 52292 KB Output is correct
15 Correct 51 ms 52304 KB Output is correct
16 Correct 52 ms 52304 KB Output is correct
17 Correct 60 ms 52304 KB Output is correct
18 Correct 55 ms 52304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 51960 KB Output is correct
2 Correct 60 ms 52068 KB Output is correct
3 Correct 53 ms 52144 KB Output is correct
4 Correct 56 ms 52144 KB Output is correct
5 Correct 51 ms 52220 KB Output is correct
6 Correct 52 ms 52220 KB Output is correct
7 Correct 53 ms 52256 KB Output is correct
8 Correct 51 ms 52256 KB Output is correct
9 Correct 50 ms 52280 KB Output is correct
10 Correct 54 ms 52280 KB Output is correct
11 Correct 57 ms 52280 KB Output is correct
12 Correct 60 ms 52280 KB Output is correct
13 Correct 56 ms 52280 KB Output is correct
14 Correct 57 ms 52292 KB Output is correct
15 Correct 51 ms 52304 KB Output is correct
16 Correct 52 ms 52304 KB Output is correct
17 Correct 60 ms 52304 KB Output is correct
18 Correct 55 ms 52304 KB Output is correct
19 Correct 52 ms 52304 KB Output is correct
20 Correct 55 ms 52304 KB Output is correct
21 Correct 58 ms 52304 KB Output is correct
22 Correct 64 ms 52332 KB Output is correct
23 Correct 55 ms 52332 KB Output is correct
24 Correct 53 ms 52460 KB Output is correct
25 Correct 52 ms 52460 KB Output is correct
26 Correct 55 ms 52460 KB Output is correct
27 Correct 51 ms 52460 KB Output is correct
28 Correct 59 ms 52460 KB Output is correct
29 Correct 55 ms 52476 KB Output is correct
30 Correct 55 ms 52476 KB Output is correct
31 Correct 54 ms 52476 KB Output is correct
32 Correct 54 ms 52476 KB Output is correct
33 Correct 55 ms 52476 KB Output is correct
34 Correct 51 ms 52476 KB Output is correct
35 Correct 52 ms 52476 KB Output is correct
36 Correct 55 ms 52476 KB Output is correct
37 Correct 58 ms 52476 KB Output is correct
38 Correct 60 ms 52476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 51960 KB Output is correct
2 Correct 60 ms 52068 KB Output is correct
3 Correct 53 ms 52144 KB Output is correct
4 Correct 56 ms 52144 KB Output is correct
5 Correct 51 ms 52220 KB Output is correct
6 Correct 52 ms 52220 KB Output is correct
7 Correct 53 ms 52256 KB Output is correct
8 Correct 51 ms 52256 KB Output is correct
9 Correct 50 ms 52280 KB Output is correct
10 Correct 54 ms 52280 KB Output is correct
11 Correct 57 ms 52280 KB Output is correct
12 Correct 60 ms 52280 KB Output is correct
13 Correct 56 ms 52280 KB Output is correct
14 Correct 57 ms 52292 KB Output is correct
15 Correct 51 ms 52304 KB Output is correct
16 Correct 52 ms 52304 KB Output is correct
17 Correct 60 ms 52304 KB Output is correct
18 Correct 55 ms 52304 KB Output is correct
19 Correct 507 ms 58356 KB Output is correct
20 Correct 484 ms 58452 KB Output is correct
21 Correct 635 ms 59784 KB Output is correct
22 Correct 577 ms 62936 KB Output is correct
23 Correct 268 ms 62936 KB Output is correct
24 Correct 225 ms 62936 KB Output is correct
25 Correct 628 ms 63264 KB Output is correct
26 Correct 657 ms 74412 KB Output is correct
27 Correct 491 ms 74412 KB Output is correct
28 Correct 804 ms 83836 KB Output is correct
29 Correct 826 ms 83836 KB Output is correct
30 Correct 528 ms 83836 KB Output is correct
31 Correct 637 ms 83836 KB Output is correct
32 Correct 623 ms 83836 KB Output is correct
33 Correct 922 ms 83836 KB Output is correct
34 Correct 599 ms 83836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 51960 KB Output is correct
2 Correct 60 ms 52068 KB Output is correct
3 Correct 53 ms 52144 KB Output is correct
4 Correct 56 ms 52144 KB Output is correct
5 Correct 51 ms 52220 KB Output is correct
6 Correct 52 ms 52220 KB Output is correct
7 Correct 53 ms 52256 KB Output is correct
8 Correct 51 ms 52256 KB Output is correct
9 Correct 50 ms 52280 KB Output is correct
10 Correct 54 ms 52280 KB Output is correct
11 Correct 57 ms 52280 KB Output is correct
12 Correct 60 ms 52280 KB Output is correct
13 Correct 56 ms 52280 KB Output is correct
14 Correct 57 ms 52292 KB Output is correct
15 Correct 51 ms 52304 KB Output is correct
16 Correct 52 ms 52304 KB Output is correct
17 Correct 60 ms 52304 KB Output is correct
18 Correct 55 ms 52304 KB Output is correct
19 Correct 52 ms 52304 KB Output is correct
20 Correct 55 ms 52304 KB Output is correct
21 Correct 58 ms 52304 KB Output is correct
22 Correct 64 ms 52332 KB Output is correct
23 Correct 55 ms 52332 KB Output is correct
24 Correct 53 ms 52460 KB Output is correct
25 Correct 52 ms 52460 KB Output is correct
26 Correct 55 ms 52460 KB Output is correct
27 Correct 51 ms 52460 KB Output is correct
28 Correct 59 ms 52460 KB Output is correct
29 Correct 55 ms 52476 KB Output is correct
30 Correct 55 ms 52476 KB Output is correct
31 Correct 54 ms 52476 KB Output is correct
32 Correct 54 ms 52476 KB Output is correct
33 Correct 55 ms 52476 KB Output is correct
34 Correct 51 ms 52476 KB Output is correct
35 Correct 52 ms 52476 KB Output is correct
36 Correct 55 ms 52476 KB Output is correct
37 Correct 58 ms 52476 KB Output is correct
38 Correct 60 ms 52476 KB Output is correct
39 Correct 507 ms 58356 KB Output is correct
40 Correct 484 ms 58452 KB Output is correct
41 Correct 635 ms 59784 KB Output is correct
42 Correct 577 ms 62936 KB Output is correct
43 Correct 268 ms 62936 KB Output is correct
44 Correct 225 ms 62936 KB Output is correct
45 Correct 628 ms 63264 KB Output is correct
46 Correct 657 ms 74412 KB Output is correct
47 Correct 491 ms 74412 KB Output is correct
48 Correct 804 ms 83836 KB Output is correct
49 Correct 826 ms 83836 KB Output is correct
50 Correct 528 ms 83836 KB Output is correct
51 Correct 637 ms 83836 KB Output is correct
52 Correct 623 ms 83836 KB Output is correct
53 Correct 922 ms 83836 KB Output is correct
54 Correct 599 ms 83836 KB Output is correct
55 Correct 74 ms 83836 KB Output is correct
56 Correct 109 ms 83836 KB Output is correct
57 Correct 688 ms 83836 KB Output is correct
58 Correct 233 ms 83836 KB Output is correct
59 Correct 546 ms 83836 KB Output is correct
60 Correct 1790 ms 95280 KB Output is correct
61 Correct 600 ms 95280 KB Output is correct
62 Correct 919 ms 95280 KB Output is correct
63 Correct 1030 ms 95280 KB Output is correct
64 Correct 1976 ms 95280 KB Output is correct
65 Correct 586 ms 95280 KB Output is correct
66 Correct 1342 ms 95280 KB Output is correct
67 Correct 550 ms 95280 KB Output is correct
68 Correct 818 ms 95280 KB Output is correct
69 Correct 755 ms 95280 KB Output is correct
70 Correct 747 ms 95280 KB Output is correct