Submission #133135

# Submission time Handle Problem Language Result Execution time Memory
133135 2019-07-20T07:54:39 Z briansu Race (IOI11_race) C++14
100 / 100
955 ms 103548 KB
#include "race.h"

//{
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
typedef double lf;
typedef pair<ll,ll> ii;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(i,n) memset(i,n,sizeof i)
#define X first
#define Y second
#define SZ(_a) (int)_a.size()
#define ALL(_a) _a.begin(),_a.end()
#define pb push_back
#ifdef brian
#define debug(...) do{\
    fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\
    _do(__VA_ARGS__);\
}while(0)
template<typename T>void _do(T &&_x){cerr<<_x<<endl;}
template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<" ,";_do(_t...);}
template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";}
template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb)
{
    _s<<"{";
    for(It _it=_ita;_it!=_itb;_it++)
    {
        _s<<(_it==_ita?"":",")<<*_it;
    }
    _s<<"}";
    return _s;
}
template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));}
template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));}
template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;}
#define IOS()
#else
#define debug(...)
#define pary(...)
#define endl '\n'
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#endif // brian
//}


const ll MAXn=2e5+5,MAXlg=__lg(MAXn)+2;
const ll MOD=1000000007;
const ll INF=ll(1e9);

namespace pre{
  vector<ii> v[MAXn];
  ii p[MAXn];
  ll lf[MAXn];
  void dfs(ll now, ll f)
  {
    lf[now] = 1;
    for(ii k:v[now])if(k.X  != f)
    {
      p[k.X] = ii(now, k.Y);
      lf[now] = 0;
      dfs(k.X, now);
    }
  }
};

ll ans = INF, n, k;

void cal(vector<ii> &a, vector<ii> &b)
{
  ll it = SZ(b) - 1;
  REP(i, SZ(a))
  {
    while(it >= 0 && a[i].X + b[it].X > k)it --;
    if(it >= 0 && a[i].X + b[it].X == k)ans = min(ans, a[i].Y + b[it].Y);
  }
}

vector<ii> mg(vector<ii> &a, vector<ii> &b, ii d)// merge (a, b + d)
{
  for(auto &p:b)p.X = min(INF, p.X + d.X), p.Y += d.Y;
  vector<ii> c(SZ(a) + SZ(b));
  merge(ALL(a), ALL(b), c.begin());
  vector<ii> ret;
  for(auto &p:c)if(!SZ(ret) || p.X != ret.back().X)ret.pb(p);
  return ret;
}

ll u[MAXn], gct, rt, nct = 0;
ii dis[MAXn];
vector<ll> v[MAXn];
vector<ii> dt[MAXn][2];

#define CLR(_a) (_a).clear(),(_a).shrink_to_fit()

void dfs(ll now)
{
  nct ++;
  u[now] = 0;
  for(ll t:v[now])dfs(t);
  if(SZ(v[now]) == 1)
  {
    ll t = v[now][0];
    if(u[t] || now == rt)return;
    u[t] = u[now] = 1;
    gct --;
    cal(dt[t][0], dt[now][1]);
    dt[now][0] = mg(dt[now][0], dt[t][0], dis[now]);
    if(SZ(v[t])){
      dt[now][1] = mg(dt[t][1], dt[now][1], dis[t]);
      dis[now] = {dis[now].X + dis[t].X, dis[now].Y + dis[t].Y};
    }
    v[now] = v[t];
    CLR(v[t]);CLR(dt[t][0]);CLR(dt[t][1]);
    return;
  }
  vector<ll> a, b, nv;
  for(ll t:v[now])
  {
    if(u[t])nv.pb(t);
    else if(!SZ(v[t]))a.pb(t);
    else b.pb(t);
  }
  for(int i = 0;i < SZ(a);)
  {
    ll x, y;
    if(i < SZ(b))x = b[i], y = a[i], i ++;
    else if(i < SZ(a) - 1)x = a[i + 1], y = a[i], i += 2;
    else{
      nv.pb(a[i]);
      break;
    }
    u[x] = u[y] = 1;
    gct --;
    nv.pb(x);
    cal(dt[x][0], dt[y][0]);
    dt[x][0] = mg(dt[x][0], dt[y][0], {0, 0});
    if(SZ(v[x]))dt[x][1] = mg(dt[x][1], dt[y][0], dis[x]);
    CLR(v[y]);CLR(dt[y][0]);CLR(dt[y][1]);
  }
  if(SZ(b) > SZ(a))for(int i = SZ(a);i < SZ(b);i ++)nv.pb(b[i]);
  v[now] = nv;
}

int best_path(int N, int K, int H[][2], int L[])
{
  srand(chrono::system_clock::now().time_since_epoch().count());
  k = K;
  n = N;
  REP(i, n - 1)
  {
    pre::v[H[i][0]].pb(ii(H[i][1], L[i]));
    pre::v[H[i][1]].pb(ii(H[i][0], L[i]));
  }
  rt = rand() % n;
  pre::dfs(rt, -1);
  for(int i = 0;i < n;i ++)if(i != rt)
  {
    v[pre::p[i].X].pb(i);
    if(pre::p[i].Y == k)ans = 1;
    dis[i] = {pre::p[i].Y, 1};
    dt[i][0].pb({pre::p[i].Y, 1});
    if(!pre::lf[i])dt[i][1].pb({pre::p[i].Y, 1});
  }
  gct = n - 1;
  ll iter = 0;
  while(gct > 1)
  {
    dfs(rt);
    iter ++;
  }
  debug(iter, nct);
  assert(iter <= 35);
  assert(nct <= 10 * n);
  if(ans == INF)return -1;
  else return ans;
}

# Verdict Execution time Memory Grader output
1 Correct 20 ms 19220 KB Output is correct
2 Correct 20 ms 19192 KB Output is correct
3 Correct 20 ms 19192 KB Output is correct
4 Correct 24 ms 19192 KB Output is correct
5 Correct 21 ms 19216 KB Output is correct
6 Correct 19 ms 19192 KB Output is correct
7 Correct 21 ms 19192 KB Output is correct
8 Correct 19 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 20 ms 19192 KB Output is correct
11 Correct 24 ms 19192 KB Output is correct
12 Correct 19 ms 19192 KB Output is correct
13 Correct 24 ms 19192 KB Output is correct
14 Correct 23 ms 19192 KB Output is correct
15 Correct 19 ms 19192 KB Output is correct
16 Correct 24 ms 19192 KB Output is correct
17 Correct 24 ms 19192 KB Output is correct
18 Correct 19 ms 19192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19220 KB Output is correct
2 Correct 20 ms 19192 KB Output is correct
3 Correct 20 ms 19192 KB Output is correct
4 Correct 24 ms 19192 KB Output is correct
5 Correct 21 ms 19216 KB Output is correct
6 Correct 19 ms 19192 KB Output is correct
7 Correct 21 ms 19192 KB Output is correct
8 Correct 19 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 20 ms 19192 KB Output is correct
11 Correct 24 ms 19192 KB Output is correct
12 Correct 19 ms 19192 KB Output is correct
13 Correct 24 ms 19192 KB Output is correct
14 Correct 23 ms 19192 KB Output is correct
15 Correct 19 ms 19192 KB Output is correct
16 Correct 24 ms 19192 KB Output is correct
17 Correct 24 ms 19192 KB Output is correct
18 Correct 19 ms 19192 KB Output is correct
19 Correct 19 ms 19192 KB Output is correct
20 Correct 19 ms 19192 KB Output is correct
21 Correct 21 ms 19320 KB Output is correct
22 Correct 21 ms 19320 KB Output is correct
23 Correct 22 ms 19320 KB Output is correct
24 Correct 21 ms 19324 KB Output is correct
25 Correct 21 ms 19324 KB Output is correct
26 Correct 20 ms 19320 KB Output is correct
27 Correct 21 ms 19320 KB Output is correct
28 Correct 21 ms 19320 KB Output is correct
29 Correct 21 ms 19384 KB Output is correct
30 Correct 21 ms 19320 KB Output is correct
31 Correct 21 ms 19320 KB Output is correct
32 Correct 21 ms 19320 KB Output is correct
33 Correct 21 ms 19320 KB Output is correct
34 Correct 21 ms 19420 KB Output is correct
35 Correct 20 ms 19448 KB Output is correct
36 Correct 20 ms 19448 KB Output is correct
37 Correct 21 ms 19448 KB Output is correct
38 Correct 25 ms 19452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19220 KB Output is correct
2 Correct 20 ms 19192 KB Output is correct
3 Correct 20 ms 19192 KB Output is correct
4 Correct 24 ms 19192 KB Output is correct
5 Correct 21 ms 19216 KB Output is correct
6 Correct 19 ms 19192 KB Output is correct
7 Correct 21 ms 19192 KB Output is correct
8 Correct 19 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 20 ms 19192 KB Output is correct
11 Correct 24 ms 19192 KB Output is correct
12 Correct 19 ms 19192 KB Output is correct
13 Correct 24 ms 19192 KB Output is correct
14 Correct 23 ms 19192 KB Output is correct
15 Correct 19 ms 19192 KB Output is correct
16 Correct 24 ms 19192 KB Output is correct
17 Correct 24 ms 19192 KB Output is correct
18 Correct 19 ms 19192 KB Output is correct
19 Correct 264 ms 34528 KB Output is correct
20 Correct 281 ms 34668 KB Output is correct
21 Correct 266 ms 34424 KB Output is correct
22 Correct 317 ms 34232 KB Output is correct
23 Correct 282 ms 34296 KB Output is correct
24 Correct 216 ms 32376 KB Output is correct
25 Correct 278 ms 56144 KB Output is correct
26 Correct 148 ms 50072 KB Output is correct
27 Correct 363 ms 49892 KB Output is correct
28 Correct 929 ms 95816 KB Output is correct
29 Correct 867 ms 103548 KB Output is correct
30 Correct 357 ms 49912 KB Output is correct
31 Correct 355 ms 50040 KB Output is correct
32 Correct 585 ms 50020 KB Output is correct
33 Correct 746 ms 54732 KB Output is correct
34 Correct 757 ms 55588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 19220 KB Output is correct
2 Correct 20 ms 19192 KB Output is correct
3 Correct 20 ms 19192 KB Output is correct
4 Correct 24 ms 19192 KB Output is correct
5 Correct 21 ms 19216 KB Output is correct
6 Correct 19 ms 19192 KB Output is correct
7 Correct 21 ms 19192 KB Output is correct
8 Correct 19 ms 19192 KB Output is correct
9 Correct 18 ms 19192 KB Output is correct
10 Correct 20 ms 19192 KB Output is correct
11 Correct 24 ms 19192 KB Output is correct
12 Correct 19 ms 19192 KB Output is correct
13 Correct 24 ms 19192 KB Output is correct
14 Correct 23 ms 19192 KB Output is correct
15 Correct 19 ms 19192 KB Output is correct
16 Correct 24 ms 19192 KB Output is correct
17 Correct 24 ms 19192 KB Output is correct
18 Correct 19 ms 19192 KB Output is correct
19 Correct 19 ms 19192 KB Output is correct
20 Correct 19 ms 19192 KB Output is correct
21 Correct 21 ms 19320 KB Output is correct
22 Correct 21 ms 19320 KB Output is correct
23 Correct 22 ms 19320 KB Output is correct
24 Correct 21 ms 19324 KB Output is correct
25 Correct 21 ms 19324 KB Output is correct
26 Correct 20 ms 19320 KB Output is correct
27 Correct 21 ms 19320 KB Output is correct
28 Correct 21 ms 19320 KB Output is correct
29 Correct 21 ms 19384 KB Output is correct
30 Correct 21 ms 19320 KB Output is correct
31 Correct 21 ms 19320 KB Output is correct
32 Correct 21 ms 19320 KB Output is correct
33 Correct 21 ms 19320 KB Output is correct
34 Correct 21 ms 19420 KB Output is correct
35 Correct 20 ms 19448 KB Output is correct
36 Correct 20 ms 19448 KB Output is correct
37 Correct 21 ms 19448 KB Output is correct
38 Correct 25 ms 19452 KB Output is correct
39 Correct 264 ms 34528 KB Output is correct
40 Correct 281 ms 34668 KB Output is correct
41 Correct 266 ms 34424 KB Output is correct
42 Correct 317 ms 34232 KB Output is correct
43 Correct 282 ms 34296 KB Output is correct
44 Correct 216 ms 32376 KB Output is correct
45 Correct 278 ms 56144 KB Output is correct
46 Correct 148 ms 50072 KB Output is correct
47 Correct 363 ms 49892 KB Output is correct
48 Correct 929 ms 95816 KB Output is correct
49 Correct 867 ms 103548 KB Output is correct
50 Correct 357 ms 49912 KB Output is correct
51 Correct 355 ms 50040 KB Output is correct
52 Correct 585 ms 50020 KB Output is correct
53 Correct 746 ms 54732 KB Output is correct
54 Correct 757 ms 55588 KB Output is correct
55 Correct 38 ms 20816 KB Output is correct
56 Correct 41 ms 20800 KB Output is correct
57 Correct 169 ms 34212 KB Output is correct
58 Correct 110 ms 32288 KB Output is correct
59 Correct 214 ms 55544 KB Output is correct
60 Correct 955 ms 83084 KB Output is correct
61 Correct 375 ms 50044 KB Output is correct
62 Correct 343 ms 49884 KB Output is correct
63 Correct 567 ms 49968 KB Output is correct
64 Correct 778 ms 50552 KB Output is correct
65 Correct 738 ms 50320 KB Output is correct
66 Correct 864 ms 82908 KB Output is correct
67 Correct 272 ms 46436 KB Output is correct
68 Correct 561 ms 48788 KB Output is correct
69 Correct 562 ms 49028 KB Output is correct
70 Correct 512 ms 47352 KB Output is correct