結果

問題 No.1023 Cyclic Tour
ユーザー risujirohrisujiroh
提出日時 2020-04-10 22:20:07
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 1,937 bytes
コンパイル時間 5,620 ms
コンパイル使用メモリ 172,748 KB
実行使用メモリ 18,368 KB
最終ジャッジ日時 2023-10-14 01:05:45
合計ジャッジ時間 7,987 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 39 ms
10,036 KB
testcase_05 AC 39 ms
10,100 KB
testcase_06 AC 45 ms
12,020 KB
testcase_07 AC 42 ms
11,012 KB
testcase_08 AC 35 ms
7,988 KB
testcase_09 AC 35 ms
7,848 KB
testcase_10 AC 34 ms
7,796 KB
testcase_11 AC 38 ms
7,804 KB
testcase_12 AC 34 ms
7,584 KB
testcase_13 AC 31 ms
7,344 KB
testcase_14 AC 33 ms
7,776 KB
testcase_15 AC 34 ms
7,492 KB
testcase_16 AC 63 ms
12,020 KB
testcase_17 AC 63 ms
12,176 KB
testcase_18 AC 65 ms
12,876 KB
testcase_19 AC 62 ms
12,280 KB
testcase_20 AC 67 ms
12,264 KB
testcase_21 AC 68 ms
11,764 KB
testcase_22 AC 64 ms
9,912 KB
testcase_23 AC 66 ms
10,224 KB
testcase_24 AC 65 ms
10,124 KB
testcase_25 AC 68 ms
12,560 KB
testcase_26 AC 68 ms
13,456 KB
testcase_27 AC 64 ms
12,040 KB
testcase_28 AC 63 ms
10,376 KB
testcase_29 AC 59 ms
10,120 KB
testcase_30 AC 64 ms
10,532 KB
testcase_31 AC 61 ms
10,916 KB
testcase_32 AC 63 ms
13,556 KB
testcase_33 AC 67 ms
12,116 KB
testcase_34 AC 65 ms
12,792 KB
testcase_35 AC 68 ms
11,452 KB
testcase_36 AC 67 ms
12,132 KB
testcase_37 AC 64 ms
12,512 KB
testcase_38 AC 59 ms
11,420 KB
testcase_39 AC 64 ms
9,940 KB
testcase_40 AC 63 ms
10,588 KB
testcase_41 AC 64 ms
10,292 KB
testcase_42 AC 68 ms
12,524 KB
testcase_43 AC 58 ms
9,968 KB
testcase_44 AC 39 ms
9,668 KB
testcase_45 AC 43 ms
17,600 KB
testcase_46 AC 44 ms
17,784 KB
testcase_47 AC 47 ms
18,368 KB
testcase_48 AC 66 ms
11,960 KB
testcase_49 AC 65 ms
11,636 KB
testcase_50 AC 64 ms
13,208 KB
testcase_51 AC 64 ms
11,832 KB
testcase_52 AC 65 ms
12,564 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] == ce[i] + 1) {
      continue;
    }
    cout << "Yes\n";
    exit(0);
  }
  cout << "No\n";
}
0