結果

問題 No.416 旅行会社
ユーザー rsk0315
提出日時 2019-03-17 19:41:08
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 653 ms / 4,000 ms
コード長 2,951 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 73,596 KB
最終ジャッジ日時 2025-01-06 22:34:55
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:90:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   90 |   scanf("%zu %zu %zu", &N, &M, &Q);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:96:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   96 |     scanf("%zu %zu", &p.first, &p.second);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:104:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
  104 |     scanf("%zu %zu", &p.first, &p.second);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <cstdint>
#include <cassert>
#include <vector>
#include <utility>
#include <algorithm>
#include <set>
#include <tuple>

template <class Tp>
class semi_persistent_array {
  using Node = std::pair<size_t, Tp>;  // <timestamp, value>
  std::vector<std::vector<Node>> entity;
  size_t last = 0;

public:
  semi_persistent_array(size_t n, Tp v=Tp()):
    entity(n, std::vector<Node>(1, {0, v}))
  {}

  void update(size_t i, Tp x, size_t t) {
    assert(last <= t);
    last = t;
    if (entity[i].back().first == t) {
      entity[i].back().second = x;
    } else {
      assert(entity[i].back().first < t);
      entity[i].emplace_back(t, x);
    }
  }

  Tp get(size_t i, size_t t=-1) const {
    if (entity[i].back().first <= t) {
      // most-frequent case
      return entity[i].back().second;
    }

    size_t lb = 0;
    size_t ub = entity[i].size();
    while (ub-lb > 1) {
      size_t mid = (lb+ub)>>1;
      ((entity[i][mid].first <= t)? lb:ub) = mid;
    }
    return entity[i][lb].second;
  }
};

class semi_persistent_union_find {
  semi_persistent_array<intmax_t> tree;
  size_t last = 0;

  size_t find_root(size_t v, size_t t) const {
    intmax_t pv = tree.get(v, t);
    while (pv >= 0) {
      v = pv;
      pv = tree.get(pv, t);
    }
    return v;
  }

public:
  semi_persistent_union_find(size_t n): tree(n, -1) {}

  bool unite(size_t u, size_t v, size_t t=-1) {
    if (t+1 != 0) {
      assert(last <= t);
      last = t;
    }
    u = find_root(u, t);
    v = find_root(v, t);
    if (u == v) return false;
    size_t su = -tree.get(u, t);
    size_t sv = -tree.get(v, t);
    if (su == sv) {
      tree.update(v, -sv-1, t);
    } else if (su > sv) {
      std::swap(u, v);
    }
    tree.update(u, v, t);
    return true;
  }

  bool connected(size_t u, size_t v, size_t t=-1) const {
    return find_root(u, t) == find_root(v, t);
  }
};

int main() {
  size_t N, M, Q;
  scanf("%zu %zu %zu", &N, &M, &Q);

  using edge = std::pair<size_t, size_t>;

  std::vector<edge> ab(M);
  for (auto& p: ab) {
    scanf("%zu %zu", &p.first, &p.second);
    --p.first;
    --p.second;
  }

  std::vector<edge> cd(Q);
  std::set<edge> es;
  for (auto& p: cd) {
    scanf("%zu %zu", &p.first, &p.second);
    --p.first;
    --p.second;
    es.emplace(p.first, p.second);
  }

  semi_persistent_union_find uf(N);
  for (const auto& p: ab) {
    size_t a, b;
    std::tie(a, b) = p;
    if (es.count({a, b})) continue;
    uf.unite(a, b, 0);
  }

  for (size_t i = Q; i--;) {
    size_t c, d;
    std::tie(c, d) = cd[i];
    uf.unite(c, d, Q-i);
  }

  for (size_t i = 1; i < N; ++i) {
    if (!uf.connected(0, i, Q)) {
      puts("0");
      continue;
    }
    if (uf.connected(0, i, 0)) {
      puts("-1");
      continue;
    }

    size_t lb = 0;
    size_t ub = Q;
    while (ub-lb > 1) {
      size_t mid = (lb+ub) >> 1;
      (uf.connected(0, i, mid)? ub:lb) = mid;
    }
    printf("%zu\n", Q-lb);
  }
}
0