結果

問題 No.1023 Cyclic Tour
ユーザー tapienru
提出日時 2020-05-17 16:41:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 2,476 ms
コンパイル使用メモリ 203,600 KB
最終ジャッジ日時 2025-01-10 12:42:56
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

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