Submission #1261999

#TimeUsernameProblemLanguageResultExecution timeMemory
1261999madamadam3이주 (IOI25_migrations)C++20
0 / 100
39 ms1204 KiB
#include "migrations.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
using pi = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

void dfs(int u, int p, vi &dist, vvi &adj) {
  for (auto &v : adj[u]) {
    if (v == p) continue;
    dist[v] = dist[u] + 1;
    dfs(v, u, dist, adj);
  }
}

int furthest_from(int u, int n, vvi &adj) {
  vector<int> dist(n);
  dfs(u, u, dist, adj);

  int f = 0; for (int i = 0; i < n; i++) if (dist[i] > dist[f]) f = i;
  return f;
}

int dist_between(int n, int u, int v, vvi &adj) {
  vector<int> dist(n);
  dfs(u, u, dist, adj);

  return dist[v];
}

pi furthest_pair(int n, vvi &adj) {
  vector<int> dist(n);
  dfs(0, 0, dist, adj);
  int f1 = 0; for (int i = 0; i < n; i++) if (dist[i] > dist[f1]) f1 = i;
  dist = vi(n, 0);
  dfs(f1, f1, dist, adj);
  int f2 = 0; for (int i = 0; i < n; i++) if (dist[i] > dist[f2]) f2 = i;

  return {min(f1, f2), max(f1, f2)};
}

vi to_base(ll A, ll b, int pad = 0) {
  vi ans;
  while (A > 0LL) {
    ans.push_back(A % b);
    A /= b;
  }
  
  while (ans.size() < pad) ans.push_back(0);
  return ans;
}

ll from_base(vi bx, ll b) {
  ll po = 1;
  ll ans = 0;

  for (auto &el : bx) {
    ans += ll(el) * po;
    po *= b;
  }

  return ans;
}

vector<bool> bsearch(int x) { // record decision log of binary search to find x
  vector<bool> log;
  
  int lo = 0, hi = 10000;
  while (lo < hi) {
    int mid = lo + (hi - lo) / 2;
    if (mid < x) {
      log.push_back(true);
      lo = mid + 1;
    } else {
      hi = mid;
      log.push_back(false);
    }
  }

  return log;
}

vvi adj;
pi fr;
int diam = 0,  prev_change = 0, cpos = 0;
vector<int> change_types;
vector<int> diam_history;

int x = 0;
int send_message(int n, int i, int Pi) {
  if (i <= 1) adj.assign(n+1, vi());
  x = n - 24;

  adj[Pi].push_back(i);
  adj[i].push_back(Pi);

  if (i < x) return 0;
  int step = i - x;

  if (step == 0) {fr = furthest_pair(n, adj); diam = dist_between(n, fr.first, fr.second, adj); prev_change = fr.second; diam_history.push_back(diam);}

  pi cfr = furthest_pair(n, adj);
  int u = cfr.first, v = cfr.second;
  int cdiam = dist_between(n, u, v, adj);
  diam_history.push_back(cdiam);

  if (cdiam == diam) {
    change_types.push_back(0);
  } else if (dist_between(n, i, prev_change, adj) == cdiam) {
    change_types.push_back(1);
  } else {
    change_types.push_back(2);
  }

  const int states[3][3] = {
    {0, 1, 2},
    {3, 4, 1},
    {5, 4, 2}
  };

  vi u5 = to_base(fr.first, 5, 6), v5 = to_base(fr.second, 5, 6);
  if (step < 12) {
    if (step % 2 == 0) return u5[step / 2];
    return states[change_types[step-1]][change_types[step]];
  } else {
    if (step % 2 == 0) return v5[(step - 12) / 2];
    return states[change_types[step-1]][change_types[step]];
  }

  return 0;
}

pi longest_path(vi S) {
  int n = S.size();
  int x = n - 24;

  int u = 0, v = 0;
  vector<pi> switches;

  for (int i = x; i < x + 12; i+=2) {
    u += S[i] * pow(5, (i-x)/2);
  }

  for (int i = x+12; i < x + 24; i+=2) {
    v += S[i] * pow(5, (i-x-12)/2);
  }

  for (int i = x+1; i < n; i += 2) {
    if (S[i] == 0) continue;
    else if (S[i] == 1) switches.push_back({i, 0});
    else if (S[i] == 2) switches.push_back({i, 1});
    else if (S[i] == 3) switches.push_back({i-1, 0});
    else if (S[i] == 4) switches.push_back({i-1, 0}), switches.push_back({i, 0});
    else switches.push_back({i-1, 1});
  }

  int A = u, B = v;
  int L = v; 

  for (auto &el : switches) {
    int w = el.first;
    if (!el.second) {
      (L == A ? B : A) = w;
    } else {
      (L == A ? A : B) = w;
    }
    L = w; 
  }

  // pi rb = furthest_pair(n, adj);
  // cerr << rb.first << " " << rb.second << " " << dist_between(n, rb.first, rb.second, adj) << "\n";
  // cerr << A << " " << B << " " << dist_between(n, A, B, adj) << "\n";
  return {A, B}; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...