結果

問題 No.416 旅行会社
コンテスト
ユーザー nebukuro09
提出日時 2016-11-17 11:41:34
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 754 ms / 4,000 ms
コード長 1,744 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,921 ms
コンパイル使用メモリ 106,004 KB
実行使用メモリ 56,696 KB
最終ジャッジ日時 2026-03-05 08:51:07
合計ジャッジ時間 16,163 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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