結果

問題 No.1023 Cyclic Tour
ユーザー ashipanashipan
提出日時 2020-04-10 22:50:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,810 bytes
コンパイル時間 1,014 ms
コンパイル使用メモリ 89,804 KB
実行使用メモリ 24,144 KB
最終ジャッジ日時 2023-10-14 03:39:23
合計ジャッジ時間 11,345 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 95 ms
8,248 KB
testcase_05 AC 96 ms
8,256 KB
testcase_06 AC 109 ms
8,620 KB
testcase_07 AC 99 ms
8,636 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 94 ms
7,788 KB
testcase_12 AC 85 ms
7,472 KB
testcase_13 AC 81 ms
7,296 KB
testcase_14 WA -
testcase_15 AC 85 ms
7,376 KB
testcase_16 AC 156 ms
8,304 KB
testcase_17 AC 160 ms
8,356 KB
testcase_18 AC 167 ms
7,792 KB
testcase_19 AC 156 ms
7,788 KB
testcase_20 AC 183 ms
8,520 KB
testcase_21 AC 180 ms
8,780 KB
testcase_22 AC 174 ms
8,692 KB
testcase_23 AC 176 ms
8,568 KB
testcase_24 AC 174 ms
8,352 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 179 ms
9,304 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 178 ms
8,520 KB
testcase_37 AC 178 ms
12,368 KB
testcase_38 WA -
testcase_39 AC 170 ms
8,312 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 105 ms
19,968 KB
testcase_46 AC 134 ms
24,144 KB
testcase_47 AC 114 ms
19,992 KB
testcase_48 AC 182 ms
8,788 KB
testcase_49 AC 172 ms
9,180 KB
testcase_50 AC 180 ms
8,972 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <set>
#include <stack>
using namespace std;
using Graph = vector<vector<int>>;

// 探索
vector<bool> seen, finished;

// サイクル復元のための情報
int pos = -1; // サイクル中に含まれる頂点 pos
stack<int> hist; // 訪問履歴

void dfs(const Graph &G, int v, int p) {
    seen[v] = true;
    hist.push(v);
    for (auto nv : G[v]) {
        if (nv == p) continue; // 逆流を禁止する

        // 完全終了した頂点はスルー
        if (finished[nv]) continue;

        // サイクルを検出
        if (seen[nv] && !finished[nv]) {
            pos = nv;
            return;
        }

        // 再帰的に探索
        dfs(G, nv, v);

        // サイクル検出したならば真っ直ぐに抜けていく
        if (pos != -1) return;
    }
    hist.pop();
    finished[v] = true;
}

int main() {
    // 頂点数 (サイクルを一つ含むグラフなので辺数は N で確定)
    int N; cin >> N;
    int M; cin >> M;
    // グラフ入力受取
    Graph G(N);
    for (int i = 0; i < M; ++i) {
        int a, b, c;
        cin >> a >> b >> c;
        --a; --b; // 頂点番号が 1-indexed で与えられるので 0-indexed にする
        G[a].push_back(b);
        if(c == 1) G[b].push_back(a);
    }

    // 探索
    seen.assign(N, false); finished.assign(N, false);
    pos = -1;
    dfs(G, 0, -1);

    // サイクルを復元
    set<int> cycle;
    while (!hist.empty()) {
        int t = hist.top();
        cycle.insert(t);
        hist.pop();
        if (t == pos) break;
    }

    // クエリに答える
    for(int i = 0; i < N; i++){
        if (cycle.count(i)){
             cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}
0