結果

問題 No.416 旅行会社
ユーザー rsk0315rsk0315
提出日時 2019-03-17 20:17:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 4,000 ms
コード長 2,032 bytes
コンパイル時間 1,052 ms
コンパイル使用メモリ 79,368 KB
実行使用メモリ 31,140 KB
最終ジャッジ日時 2024-05-08 15:42:11
合計ジャッジ時間 5,330 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 107 ms
20,736 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 14 ms
5,376 KB
testcase_10 AC 140 ms
20,728 KB
testcase_11 AC 141 ms
20,728 KB
testcase_12 AC 140 ms
20,860 KB
testcase_13 AC 109 ms
20,732 KB
testcase_14 AC 330 ms
31,132 KB
testcase_15 AC 332 ms
31,136 KB
testcase_16 AC 329 ms
31,140 KB
testcase_17 AC 334 ms
31,136 KB
testcase_18 AC 340 ms
31,132 KB
testcase_19 AC 198 ms
21,752 KB
testcase_20 AC 198 ms
21,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class quick_find {
  std::vector<std::vector<size_t>> groups;
  std::vector<size_t> index;

public:
  quick_find(size_t n): index(n) {
    std::iota(index.begin(), index.end(), 0);
    for (size_t i = 0; i < n; ++i)
      groups.emplace_back(1, i);
  }

  bool unite(size_t u, size_t v) {
    if (index[u] == index[v]) return false;

    if (groups[index[u]].size() > groups[index[v]].size())
      std::swap(u, v);

    size_t pi = index[u];
    for (size_t i: groups[index[u]]) {
      index[i] = index[v];
      groups[index[v]].push_back(i);
    }
    groups[pi].clear();

    return true;
  }

  const std::vector<size_t>& components(size_t v) const {
    return groups[index[v]];
  }

  bool connected(size_t u, size_t v) const {
    return index[u] == index[v];
  }
};

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);
  }

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

  std::vector<int> res(N, 0);
  for (size_t u: qf.components(0))
    res[u] = -1;

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

    if (qf.connected(c, d)) continue;

    size_t u = 0;
    if (qf.connected(0, c)) {
      u = d;
    } else if (qf.connected(0, d)) {
      u = c;
    }

    if (u != 0) {
      const auto& cmp = qf.components(u);
      for (size_t v: cmp) res[v] = i+1;
    }

    qf.unite(c, d);
  }

  res.erase(res.begin());
  for (auto ri: res) printf("%d\n", ri);
}
0