結果

問題 No.74 貯金箱の退屈
ユーザー te-shte-sh
提出日時 2016-09-05 15:19:51
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,300 bytes
コンパイル時間 1,478 ms
コンパイル使用メモリ 103,892 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-09-02 21:55:59
合計ジャッジ時間 3,005 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,372 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 1 ms
4,368 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 1 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 1 ms
4,372 KB
testcase_14 AC 1 ms
4,372 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 1 ms
4,368 KB
testcase_17 AC 2 ms
4,372 KB
testcase_18 AC 2 ms
4,372 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 1 ms
4,372 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 1 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 1 ms
4,372 KB
testcase_25 AC 1 ms
4,368 KB
testcase_26 AC 2 ms
4,368 KB
testcase_27 AC 1 ms
4,368 KB
testcase_28 AC 1 ms
4,368 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 1 ms
4,368 KB
testcase_32 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.array, std.container, std.range, std.bitmanip;
import std.numeric, std.math, std.bigint, std.random, core.bitop;
import std.string, std.conv, std.stdio, std.typecons;

class UnionFind {
  int[] par;

  this(int n) {
    par = new int[](n);
    par[] = -1;
  }

  int find(int i) {
    if (par[i] < 0) {
      return i;
    } else {
      par[i] = find(par[i]);
      return par[i];
    }
  }

  void unite(int i, int j) {
    auto pi = find(i), pj = find(j);
    if (pi != pj) par[pj] = pi;
  }
}

void main()
{
  auto n = readln.chomp.to!int;
  auto di = readln.split.map!(to!int);
  auto wi = readln.split.map!(a => a.to!int == 0 ? false : true);

  auto uf = new UnionFind(n);
  int[] isolateds;

  foreach (i; 0..n) {
    auto j1 = (i + di[i]) % n;
    auto j2 = (i - (di[i] % n) + n) % n;

    if (j1 == j2)
      isolateds ~= j1;
    else
      uf.unite(j1, j2);
  }

  int[][int] h;
  foreach (i; 0..n)
    h[uf.find(i)] ~= i;

  auto r = true;
  foreach (pi; h.byValue) {
    if (pi.length == 1) {
      if (!wi[pi.front]) {
        r = false;
        break;
      }
    } else {
      auto c = pi.count!(p => !wi[p]);
      if (c % 2 != 0 && !isolateds.any!(a => pi.canFind(a))) {
        r = false;
        break;
      }
    }
  }

  writeln(r ? "Yes" : "No");
}
0