結果

問題 No.416 旅行会社
ユーザー nebukuro09nebukuro09
提出日時 2016-11-17 11:41:34
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 661 ms / 4,000 ms
コード長 1,744 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 105,136 KB
実行使用メモリ 50,140 KB
最終ジャッジ日時 2023-09-02 22:56:17
合計ジャッジ時間 11,175 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 331 ms
43,024 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 1 ms
4,368 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 3 ms
4,372 KB
testcase_08 AC 8 ms
4,368 KB
testcase_09 AC 33 ms
7,272 KB
testcase_10 AC 344 ms
42,748 KB
testcase_11 AC 346 ms
41,956 KB
testcase_12 AC 356 ms
42,736 KB
testcase_13 AC 333 ms
42,020 KB
testcase_14 AC 649 ms
45,988 KB
testcase_15 AC 650 ms
49,228 KB
testcase_16 AC 661 ms
50,140 KB
testcase_17 AC 657 ms
46,160 KB
testcase_18 AC 650 ms
47,540 KB
testcase_19 AC 424 ms
33,084 KB
testcase_20 AC 423 ms
33,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.array;
import std.string;
import std.conv;
import std.algorithm;
import std.typecons;
import std.range;
import std.random;
import std.math;
import std.container;
import std.numeric;

struct UnionFind {
  int[] n_group;
  int[][] group_n;
  
  this (int n) {
    n_group = iota(n).array;
    group_n = iota(n).map!(i => [i].array).array;
  }

  void unite(int x, int y) {
    int gx = find(x);
    int gy = find(y);
    if (gx == gy) return;
    if (group_n[gx].length < group_n[gy].length) swap(gx, gy);
    foreach (ye; group_n[gy]) {
      n_group[ye] = gx;
      group_n[gx] ~= ye;
    }
  }

  int find(int x) {
    return n_group[x];
  }

  bool is_peer(int x, int y) {
    return find(x) == find(y);
  }
}


void main() {
  auto input = readln.split.map!(to!int);
  auto N = input[0];
  auto M = input[1];
  auto Q = input[2];
  auto AB = iota(M).map!(_ => readln.split.map!(x => x.to!int-1).array).array;
  auto CD = iota(Q).map!(_ => readln.split.map!(x => x.to!int-1).array).array;
  
  bool[int][int] destroyed;
  foreach (cd; CD)
    destroyed[cd[0]][cd[1]] = true;
  
  auto uf = UnionFind(N);

  foreach(ab; AB) {
    int a = ab[0];
    int b = ab[1];
    if (a in destroyed && b in destroyed[a]) continue;
    uf.unite(a, b);
  }

  auto ans = new int[](N);
  uf.group_n[uf.find(0)].each!(n => ans[n] = -1);

  foreach (q; iota(Q-1, -1, -1)) {
    int c = CD[q][0];
    int d = CD[q][1];
    if (uf.find(c) == uf.find(d))
      continue;
    else if (uf.find(0) == uf.find(c))
      foreach (n; uf.group_n[uf.find(d)])
        ans[n] = q+1;
    else if (uf.find(0) == uf.find(d))
      foreach (n; uf.group_n[uf.find(c)])
        ans[n] = q+1;
    uf.unite(c, d);
  }
  
  ans[1..$].each!(writeln);
}
0