結果

問題 No.416 旅行会社
ユーザー 👑 emthrmemthrm
提出日時 2020-11-05 19:45:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 586 ms / 4,000 ms
コード長 2,590 bytes
コンパイル時間 3,053 ms
コンパイル使用メモリ 220,460 KB
実行使用メモリ 28,200 KB
最終ジャッジ日時 2023-08-21 10:38:52
合計ジャッジ時間 9,045 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
18,144 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 2 ms
4,384 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 20 ms
5,040 KB
testcase_10 AC 191 ms
18,172 KB
testcase_11 AC 195 ms
18,368 KB
testcase_12 AC 194 ms
18,080 KB
testcase_13 AC 136 ms
18,144 KB
testcase_14 AC 586 ms
28,076 KB
testcase_15 AC 575 ms
28,072 KB
testcase_16 AC 545 ms
28,200 KB
testcase_17 AC 538 ms
28,124 KB
testcase_18 AC 543 ms
28,080 KB
testcase_19 AC 315 ms
24,768 KB
testcase_20 AC 317 ms
24,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct PartiallyPersistentUnionFind {
  PartiallyPersistentUnionFind(int n) : data(n, -1), last(n, -1), history(n, {{-1, -1}}) {}

  int root(int t, int ver) const {
    return last[ver] == -1 || t < last[ver] ? ver : root(t, data[ver]);
  }

  bool unite(int t, int u, int v) {
    u = root(t, u);
    v = root(t, v);
    if (u == v) return false;
    if (data[u] > data[v]) std::swap(u, v);
    data[u] += data[v];
    data[v] = u;
    last[v] = t;
    history[u].emplace_back(t, data[u]);
    return true;
  }

  bool same(int t, int u, int v) const { return root(t, u) == root(t, v); }

  int size(int t, int ver) const {
    ver = root(t, ver);
    return -std::prev(std::lower_bound(history[ver].begin(), history[ver].end(), std::make_pair(t, 0)))->second;
  }

private:
  std::vector<int> data, last;
  std::vector<std::vector<std::pair<int, int>>> history;
};

int main() {
  int n, m, q; cin >> n >> m >> q;
  vector<int> a(m), b(m);
  map<pair<int, int>, int> ab;
  REP(i, m) {
    cin >> a[i] >> b[i]; --a[i]; --b[i];
    ab[{a[i], b[i]}] = i;
  }
  vector<int> c(q), d(q);
  set<int> not_broken;
  REP(i, m) not_broken.emplace(i);
  REP(i, q) {
    cin >> c[i] >> d[i]; --c[i]; --d[i];
    not_broken.erase(ab[{c[i], d[i]}]);
  }
  PartiallyPersistentUnionFind uf(n);
  for (int idx : not_broken) uf.unite(0, a[idx], b[idx]);
  for (int i = q - 1; i >= 0; --i) uf.unite(q - i, c[i], d[i]);
  FOR(i, 1, n) {
    if (!uf.same(q, 0, i)) {
      cout << 0 << '\n';
      continue;
    }
    int lb = -1, ub = q;
    while (ub - lb > 1) {
      int mid = (lb + ub) / 2;
      (uf.same(mid, 0, i) ? ub : lb) = mid;
    }
    cout << (ub == 0 ? -1 : q - ub + 1) << '\n';
  }
  return 0;
}
0