結果

問題 No.1023 Cyclic Tour
ユーザー simansiman
提出日時 2023-06-12 13:35:51
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,502 bytes
コンパイル時間 3,440 ms
コンパイル使用メモリ 94,956 KB
実行使用メモリ 37,452 KB
最終ジャッジ日時 2023-09-02 08:53:02
合計ジャッジ時間 37,211 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 309 ms
26,196 KB
testcase_05 AC 314 ms
26,560 KB
testcase_06 AC 364 ms
28,308 KB
testcase_07 AC 334 ms
27,260 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 325 ms
26,460 KB
testcase_12 AC 304 ms
25,948 KB
testcase_13 AC 268 ms
24,624 KB
testcase_14 AC 280 ms
24,712 KB
testcase_15 AC 299 ms
25,156 KB
testcase_16 AC 611 ms
35,048 KB
testcase_17 AC 638 ms
35,536 KB
testcase_18 AC 656 ms
34,904 KB
testcase_19 AC 633 ms
34,220 KB
testcase_20 AC 684 ms
35,708 KB
testcase_21 AC 689 ms
36,572 KB
testcase_22 AC 658 ms
35,508 KB
testcase_23 AC 676 ms
36,064 KB
testcase_24 AC 670 ms
36,856 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 721 ms
35,436 KB
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 664 ms
35,700 KB
testcase_37 AC 658 ms
37,116 KB
testcase_38 WA -
testcase_39 AC 645 ms
35,540 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 255 ms
34,216 KB
testcase_46 AC 256 ms
34,780 KB
testcase_47 AC 272 ms
34,196 KB
testcase_48 AC 694 ms
37,128 KB
testcase_49 AC 696 ms
37,452 KB
testcase_50 AC 689 ms
37,228 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stack>
#include <string.h>
#include <vector>
#include <map>

using namespace std;

typedef vector <vector<int>> Graph;
const int MAX_N = 100000;

int root;
bool visited[MAX_N];
bool finished[MAX_N];

void dfs(int u, int parent, const Graph &G, stack<int> &path) {
  visited[u] = true;
  path.push(u);

  for (int v : G[u]) {
    if (v == parent) continue;
    if (finished[v]) continue;

    if (visited[v] && !finished[v]) {
      root = v;
      return;
    }

    dfs(v, u, G, path);

    if (root != -1) return;
  }

  path.pop();
  finished[u] = true;
}

vector<int> cycle_detection(int start, const Graph &G) {
  root = -1;
  memset(visited, false, sizeof(visited));
  memset(finished, false, sizeof(finished));
  stack<int> path;

  dfs(start, -1, G, path);

  vector<int> res;

  while (!path.empty()) {
    res.push_back(path.top());
    path.pop();
  }

  return res;
}

int main() {
  int N, M;
  cin >> N >> M;
  Graph G(N + 1);
  map<int, map<int, int>> E;

  bool ok = false;
  int a, b, c;
  for (int i = 0; i < M; ++i) {
    cin >> a >> b >> c;

    G[a].push_back(b);
    if (c == 1) {
      G[b].push_back(a);
      E[a][b] += 1;
      E[b][a] += 1;
    } else {
      E[a][b] += 2;
    }
    if (E[a][b] >= 2 && E[b][a] >= 2) {
      ok = true;
    }
  }

  vector<int> path = cycle_detection(1, G);

  if (ok) {
    cout << "Yes" << endl;
  } else if (path.empty()) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }

  return 0;
}

0