結果

問題 No.1023 Cyclic Tour
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-04-10 21:55:01
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 327 ms / 2,000 ms
コード長 3,072 bytes
コンパイル時間 4,280 ms
コンパイル使用メモリ 114,976 KB
実行使用メモリ 29,424 KB
最終ジャッジ日時 2023-09-04 07:15:28
合計ジャッジ時間 18,939 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 185 ms
15,752 KB
testcase_05 AC 189 ms
14,732 KB
testcase_06 AC 216 ms
17,384 KB
testcase_07 AC 208 ms
15,556 KB
testcase_08 AC 156 ms
10,720 KB
testcase_09 AC 144 ms
12,112 KB
testcase_10 AC 142 ms
10,392 KB
testcase_11 AC 157 ms
12,208 KB
testcase_12 AC 137 ms
11,468 KB
testcase_13 AC 131 ms
10,124 KB
testcase_14 AC 128 ms
10,116 KB
testcase_15 AC 132 ms
12,896 KB
testcase_16 AC 255 ms
18,440 KB
testcase_17 AC 269 ms
18,144 KB
testcase_18 AC 280 ms
17,416 KB
testcase_19 AC 264 ms
19,032 KB
testcase_20 AC 323 ms
16,736 KB
testcase_21 AC 324 ms
16,276 KB
testcase_22 AC 294 ms
15,540 KB
testcase_23 AC 300 ms
16,292 KB
testcase_24 AC 286 ms
15,988 KB
testcase_25 AC 316 ms
16,556 KB
testcase_26 AC 325 ms
18,124 KB
testcase_27 AC 300 ms
19,608 KB
testcase_28 AC 275 ms
14,828 KB
testcase_29 AC 251 ms
13,992 KB
testcase_30 AC 287 ms
15,832 KB
testcase_31 AC 271 ms
15,252 KB
testcase_32 AC 275 ms
20,744 KB
testcase_33 AC 298 ms
18,088 KB
testcase_34 AC 286 ms
15,972 KB
testcase_35 AC 322 ms
16,024 KB
testcase_36 AC 312 ms
16,296 KB
testcase_37 AC 292 ms
18,924 KB
testcase_38 AC 260 ms
15,816 KB
testcase_39 AC 302 ms
15,308 KB
testcase_40 AC 290 ms
15,824 KB
testcase_41 AC 280 ms
16,232 KB
testcase_42 AC 327 ms
16,580 KB
testcase_43 AC 261 ms
15,068 KB
testcase_44 AC 192 ms
14,280 KB
testcase_45 AC 141 ms
27,888 KB
testcase_46 AC 146 ms
29,424 KB
testcase_47 AC 165 ms
28,016 KB
testcase_48 AC 279 ms
17,336 KB
testcase_49 AC 281 ms
17,288 KB
testcase_50 AC 278 ms
17,628 KB
testcase_51 AC 280 ms
17,844 KB
testcase_52 AC 280 ms
17,052 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }

int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

Tuple!(int, int[]) scc(in int[][][] graph) {
  const n = cast(int)(graph[0].length);
  int compN;
  auto compIds = new int[n];
  int[] us;
  void dfs(int s, int u, int a, int b) {
    if (compIds[u] == a) {
      compIds[u] = b;
      foreach (v; graph[s][u]) {
        dfs(s, v, a, b);
      }
      if (s == 0) {
        us ~= u;
      }
    }
  }
  foreach (u; 0 .. n) {
    dfs(0, u, 0, -1);
  }
  foreach_reverse (u; us) {
    if (compIds[u] == -1) {
      dfs(1, u, -1, compN);
      ++compN;
    }
  }
  return tuple(compN, compIds);
}


void main() {
  try {
    for (; ; ) {
      const N = readInt();
      const M = readInt();
      auto A = new int[M];
      auto B = new int[M];
      auto C = new int[M];
      foreach (i; 0 .. M) {
        A[i] = readInt() - 1;
        B[i] = readInt() - 1;
        C[i] = readInt();
      }
      
      auto graph = new int[][][](2, N);
      void ae(int u, int v) {
        graph[0][u] ~= v;
        graph[1][v] ~= u;
      }
      foreach (i; 0 .. M) {
        if (C[i] == 1) {
          ae(A[i], B[i]);
          ae(B[i], A[i]);
        } else {
          ae(A[i], B[i]);
        }
      }
      const res = scc(graph);
      
      bool ans;
      foreach (i; 0 .. M) {
        if (C[i] == 2) {
          if (res[1][A[i]] == res[1][B[i]]) {
            ans = true;
          }
        }
      }
      
      auto uf = new int[N];
      uf[] = -1;
      foreach (i; 0 .. M) {
        if (C[i] == 1) {
          if (!uf.connect(A[i], B[i])) {
            ans = true;
          }
        }
      }
      
      writeln(ans ? "Yes" : "No");
    }
  } catch (EOFException e) {
  }
}
0