Submission #538626

# Submission time Handle Problem Language Result Execution time Memory
538626 2022-03-17T09:17:53 Z Sam_a17 Race (IOI11_race) C++14
100 / 100
1164 ms 44876 KB
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include "temp.cpp"
#include <cstdio>
using namespace std;

#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif

#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());

#define pb push_back
#define popf pop_front
#define popb pop_back

void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}

template <class T> void print(set <T> v);
template <class T> void print(vector <T> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T, class V> void print(pair <T, V> p);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque<T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'

// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};

// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
  fastIO();

  if (str != "" && str != "input") {
    freopen((str + ".in").c_str(), "r", stdin);
    freopen((str + ".out").c_str(), "w", stdout);
  }

  if(str == "input") {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
int n, k, sz[N], compSize;
long long answ = -1;
vector<pair<int, long long>> adj[N], p;
bool used[N];
map<long long, long long> mp;

int dfs(int node, int parent) {
  sz[node] = 1; compSize++;
  for(auto i: adj[node]) {
    if(used[i.first] || i.first == parent) continue;
    sz[node] += dfs(i.first, node);
  }
  return sz[node];
}

int find_center(int node, int parent) {
  for(auto i: adj[node]) {
    if (used[i.first] || i.first == parent) continue;
    if (2 * sz[i.first] > compSize) {
      return find_center(i.first, node);
    }
  }
  return node;
}

int centroid(int u) {
  compSize = 0;
  dfs(u, 0);
  u = find_center(u, 0);
  return u;
}

void solve(int node, int parent, long long ln, int kn) {
  if(parent) {
    p.pb({kn, ln});
  }

  for(auto i: adj[node]) {
    if(i.first == parent || used[i.first]) continue;
    solve(i.first, node, ln + i.second, kn + 1);

    if(parent) continue;

    // exacly k -> Ln
    for(auto j: p) {
      long long Ln = j.second, Kn = j.first;
      if(Ln > k) continue;
      if(Ln == k) {
        if(answ == -1 || answ > Kn) {
          answ = Kn;
        }
      } else {
        auto it = mp.find(k - Ln);
        if(it != mp.end()) {
          if(answ == -1 || answ > Kn + it->second) {
            answ = Kn + it->second;
          }
        }
      }
    }

    for(auto j: p) {
      long long Ln = j.second, Kn = j.first;
      if(Ln > k) continue;

      auto it = mp.find(Ln);
      if(it == mp.end()) {
        mp[Ln] = Kn;
      } else {
        mp[Ln] = min(mp[Ln], Kn);
      }
    }
    p.clear();
  }
}

void centroid_decomposition() {
  queue<int> q;
  q.push(1);

  while(!q.empty()) {
    auto u = q.front();
    q.pop();

    int center = centroid(u);
    solve(center, 0, 0, 0);
    mp.clear();

    used[center] = true;
    for(auto i: adj[center]) {
      if(used[i.first]) continue;
      q.push(i.first);
    }
  }
}

int best_path(int M, int K, int H[][2], int L[]) {
  n = M, k = K;
  for(int i = 0; i < n - 1; i++) {
    int a = H[i][0], b = H[i][1], c = L[i]; a++, b++;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }

  centroid_decomposition();

  return answ;
}

Compilation message

race.cpp: In function 'void setIO(std::string)':
race.cpp:63:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |     freopen((str + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |     freopen((str + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:68:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |     freopen("input.txt", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:69:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     freopen("output.txt", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 4 ms 4936 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 4 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 4 ms 4936 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 4 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4968 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 5 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5136 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5160 KB Output is correct
33 Correct 4 ms 5116 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 4 ms 5160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 4 ms 4936 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 4 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 337 ms 12812 KB Output is correct
20 Correct 245 ms 12748 KB Output is correct
21 Correct 300 ms 12792 KB Output is correct
22 Correct 212 ms 12752 KB Output is correct
23 Correct 121 ms 13120 KB Output is correct
24 Correct 91 ms 11976 KB Output is correct
25 Correct 214 ms 19272 KB Output is correct
26 Correct 109 ms 19332 KB Output is correct
27 Correct 256 ms 19360 KB Output is correct
28 Correct 611 ms 33628 KB Output is correct
29 Correct 595 ms 33636 KB Output is correct
30 Correct 283 ms 19340 KB Output is correct
31 Correct 273 ms 19276 KB Output is correct
32 Correct 407 ms 19312 KB Output is correct
33 Correct 455 ms 19872 KB Output is correct
34 Correct 420 ms 19780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 4 ms 4936 KB Output is correct
5 Correct 4 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 4 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4968 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 4 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 5 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5076 KB Output is correct
28 Correct 4 ms 5136 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5160 KB Output is correct
33 Correct 4 ms 5116 KB Output is correct
34 Correct 5 ms 5076 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 3 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 4 ms 5160 KB Output is correct
39 Correct 337 ms 12812 KB Output is correct
40 Correct 245 ms 12748 KB Output is correct
41 Correct 300 ms 12792 KB Output is correct
42 Correct 212 ms 12752 KB Output is correct
43 Correct 121 ms 13120 KB Output is correct
44 Correct 91 ms 11976 KB Output is correct
45 Correct 214 ms 19272 KB Output is correct
46 Correct 109 ms 19332 KB Output is correct
47 Correct 256 ms 19360 KB Output is correct
48 Correct 611 ms 33628 KB Output is correct
49 Correct 595 ms 33636 KB Output is correct
50 Correct 283 ms 19340 KB Output is correct
51 Correct 273 ms 19276 KB Output is correct
52 Correct 407 ms 19312 KB Output is correct
53 Correct 455 ms 19872 KB Output is correct
54 Correct 420 ms 19780 KB Output is correct
55 Correct 22 ms 5844 KB Output is correct
56 Correct 16 ms 5844 KB Output is correct
57 Correct 132 ms 13096 KB Output is correct
58 Correct 54 ms 11764 KB Output is correct
59 Correct 294 ms 23944 KB Output is correct
60 Correct 1020 ms 44876 KB Output is correct
61 Correct 375 ms 19388 KB Output is correct
62 Correct 343 ms 19336 KB Output is correct
63 Correct 514 ms 19292 KB Output is correct
64 Correct 1164 ms 26160 KB Output is correct
65 Correct 486 ms 20500 KB Output is correct
66 Correct 826 ms 33656 KB Output is correct
67 Correct 145 ms 18516 KB Output is correct
68 Correct 540 ms 24896 KB Output is correct
69 Correct 507 ms 24540 KB Output is correct
70 Correct 459 ms 23964 KB Output is correct