結果

問題 No.1023 Cyclic Tour
ユーザー tapienrutapienru
提出日時 2020-05-17 16:41:10
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 2,296 ms
コンパイル使用メモリ 212,320 KB
実行使用メモリ 17,292 KB
最終ジャッジ日時 2023-10-26 01:16:17
合計ジャッジ時間 10,143 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 27 ms
4,348 KB
testcase_05 AC 27 ms
4,348 KB
testcase_06 AC 28 ms
4,348 KB
testcase_07 AC 27 ms
4,348 KB
testcase_08 AC 36 ms
9,000 KB
testcase_09 AC 35 ms
8,816 KB
testcase_10 AC 35 ms
8,852 KB
testcase_11 AC 44 ms
8,848 KB
testcase_12 AC 44 ms
9,384 KB
testcase_13 AC 40 ms
9,096 KB
testcase_14 AC 35 ms
9,452 KB
testcase_15 AC 43 ms
9,100 KB
testcase_16 AC 65 ms
11,204 KB
testcase_17 AC 66 ms
11,268 KB
testcase_18 AC 68 ms
10,896 KB
testcase_19 AC 65 ms
10,804 KB
testcase_20 AC 59 ms
8,056 KB
testcase_21 AC 60 ms
8,068 KB
testcase_22 AC 66 ms
8,856 KB
testcase_23 AC 71 ms
9,100 KB
testcase_24 AC 80 ms
10,168 KB
testcase_25 AC 57 ms
8,436 KB
testcase_26 AC 57 ms
8,468 KB
testcase_27 AC 56 ms
8,176 KB
testcase_28 AC 72 ms
10,268 KB
testcase_29 AC 69 ms
10,624 KB
testcase_30 AC 70 ms
10,300 KB
testcase_31 AC 65 ms
10,224 KB
testcase_32 AC 69 ms
12,084 KB
testcase_33 AC 69 ms
10,768 KB
testcase_34 AC 21 ms
4,348 KB
testcase_35 AC 47 ms
4,636 KB
testcase_36 AC 63 ms
8,324 KB
testcase_37 AC 65 ms
10,728 KB
testcase_38 AC 65 ms
10,948 KB
testcase_39 AC 70 ms
9,092 KB
testcase_40 AC 67 ms
9,560 KB
testcase_41 AC 67 ms
9,524 KB
testcase_42 AC 59 ms
8,464 KB
testcase_43 AC 71 ms
10,268 KB
testcase_44 AC 29 ms
7,072 KB
testcase_45 AC 54 ms
16,784 KB
testcase_46 AC 46 ms
17,292 KB
testcase_47 AC 31 ms
6,628 KB
testcase_48 AC 58 ms
8,336 KB
testcase_49 AC 59 ms
8,336 KB
testcase_50 AC 56 ms
8,336 KB
testcase_51 AC 56 ms
8,828 KB
testcase_52 AC 57 ms
8,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n, m; cin >> n >> m;
    vector<pair<int, int>> E;
    vector<int> p(n + 1, -1);
    function<int(int)> find = [&] (int x) {
        return p[x] < 0 ? x : p[x] = find(p[x]);
    };
    auto merge = [&] (int x, int y) {
        if ((x = find(x)) == (y = find(y))) return 1;
        if (p[x] > p[y]) swap(x, y);
        p[x] += p[y];
        p[y] = x;
        return 0;
    };
    for (int i = 0; i < m; ++i) {
        int a, b, c; cin >> a >> b >> c;
        if (c == 1) {
            if (merge(a, b)) return cout << "Yes\n", 0;
        }
        else {
            E.emplace_back(a, b);
        }
    }
    vector<vector<int>> G(n + 1);
    vector<int> vis(n + 1), stk(n + 1);
    for (auto& [a, b] : E) {
        G[find(a)].emplace_back(find(b));
    }
    function<void(int)> dfs = [&] (int u) {
        vis[u] = stk[u] = 1;
        for (int v : G[u]) {
            if (vis[v] && stk[v]) cout << "Yes\n", exit(0);
            if (!vis[v]) dfs(v);
        }
        stk[u] = 0;
    };
    for (int i = 1; i <= n; ++i)
        if (!vis[find(i)]) dfs(find(i));
    cout << "No\n";
}
0