結果

問題 No.1023 Cyclic Tour
ユーザー risujirohrisujiroh
提出日時 2020-04-10 22:16:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,944 bytes
コンパイル時間 1,711 ms
コンパイル使用メモリ 173,128 KB
実行使用メモリ 18,776 KB
最終ジャッジ日時 2023-10-14 00:56:06
合計ジャッジ時間 7,341 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 38 ms
10,064 KB
testcase_05 AC 38 ms
10,112 KB
testcase_06 AC 44 ms
12,104 KB
testcase_07 AC 41 ms
11,036 KB
testcase_08 AC 34 ms
7,976 KB
testcase_09 AC 35 ms
7,856 KB
testcase_10 AC 34 ms
7,912 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 32 ms
7,672 KB
testcase_15 WA -
testcase_16 AC 59 ms
12,132 KB
testcase_17 AC 61 ms
12,252 KB
testcase_18 AC 64 ms
12,880 KB
testcase_19 AC 60 ms
12,224 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 65 ms
11,220 KB
testcase_26 AC 68 ms
12,304 KB
testcase_27 AC 65 ms
12,032 KB
testcase_28 AC 63 ms
10,420 KB
testcase_29 AC 60 ms
10,124 KB
testcase_30 AC 63 ms
10,620 KB
testcase_31 AC 62 ms
11,204 KB
testcase_32 AC 64 ms
13,540 KB
testcase_33 AC 68 ms
11,924 KB
testcase_34 AC 61 ms
11,940 KB
testcase_35 AC 66 ms
12,012 KB
testcase_36 WA -
testcase_37 AC 63 ms
12,480 KB
testcase_38 AC 59 ms
11,372 KB
testcase_39 WA -
testcase_40 AC 62 ms
10,368 KB
testcase_41 AC 63 ms
10,416 KB
testcase_42 AC 66 ms
12,144 KB
testcase_43 AC 58 ms
10,032 KB
testcase_44 AC 38 ms
9,804 KB
testcase_45 AC 41 ms
17,612 KB
testcase_46 AC 43 ms
17,656 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 66 ms
11,956 KB
testcase_52 AC 65 ms
12,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif

struct scc {
  const int n;
  vector<pair<int, int>> edge_pool;
  vector<int> head, g, ord, low, st, cmp;
  int t = 0, id = 0;
  scc(int _n) : n(_n), head(n + 1), ord(n, -1), low(n), cmp(n, -1) {}
  void add(int from, int to) {
    edge_pool.emplace_back(from, to);
    ++head[from];
  }
  void build() {
    for (int v = 0; v < n; ++v) head[v + 1] += head[v];
    g.resize(head[n]);
    for (auto e : edge_pool) g[--head[e.first]] = e.second;
  }
  void dfs(int v) {
    ord[v] = low[v] = t++, st.push_back(v);
    for (int i = head[v]; i < head[v + 1]; ++i) {
      int to = g[i];
      if (ord[to] == -1) {
        dfs(to);
        low[v] = min(low[v], low[to]);
      } else if (cmp[to] == -1) {
        low[v] = min(low[v], ord[to]);
      }
    }
    if (ord[v] == low[v]) {
      while (true) {
        int u = st.back();
        st.pop_back();
        cmp[u] = id;
        if (u == v) break;
      }
      ++id;
    }
  }
  void run() {
    build();
    for (int v = 0; v < n; ++v) if (ord[v] == -1) dfs(v);
    for (auto&& e : cmp) e = id - e - 1;
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  scc g(n);
  vector<int> a(m), b(m), c(m);
  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    --a[i], --b[i];
    if (c[i] == 1) {
      g.add(a[i], b[i]);
      g.add(b[i], a[i]);
    } else {
      g.add(a[i], b[i]);
    }
  }
  g.run();
  vector<int> cv(g.id), ce(g.id);
  for (int v = 0; v < n; ++v) {
    ++cv[g.cmp[v]];
    DEBUG(v, g.cmp[v]);
  }
  for (int i = 0; i < m; ++i) {
    if (g.cmp[a[i]] == g.cmp[b[i]]) {
      ++ce[g.cmp[a[i]]];
    }
  }
  for (int i = 0; i < g.id; ++i) {
    if (cv[i] == 1) {
      continue;
    }
    if (cv[i] == 2 and ce[i] == 1) {
      continue;
    }
    cout << "Yes\n";
    exit(0);
  }
  cout << "No\n";
}
0