結果

問題 No.1023 Cyclic Tour
ユーザー finefine
提出日時 2020-05-19 02:58:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 2,467 bytes
コンパイル時間 1,833 ms
コンパイル使用メモリ 170,944 KB
実行使用メモリ 23,288 KB
最終ジャッジ日時 2024-04-09 21:56:21
合計ジャッジ時間 6,716 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 24 ms
6,940 KB
testcase_05 AC 26 ms
6,940 KB
testcase_06 AC 27 ms
6,944 KB
testcase_07 AC 26 ms
6,940 KB
testcase_08 AC 43 ms
11,520 KB
testcase_09 AC 44 ms
11,264 KB
testcase_10 AC 40 ms
11,392 KB
testcase_11 AC 48 ms
12,544 KB
testcase_12 AC 49 ms
13,440 KB
testcase_13 AC 45 ms
12,672 KB
testcase_14 AC 43 ms
12,160 KB
testcase_15 AC 46 ms
12,928 KB
testcase_16 AC 73 ms
14,080 KB
testcase_17 AC 77 ms
14,256 KB
testcase_18 AC 76 ms
13,440 KB
testcase_19 AC 72 ms
13,568 KB
testcase_20 AC 62 ms
11,532 KB
testcase_21 AC 66 ms
11,772 KB
testcase_22 AC 74 ms
12,160 KB
testcase_23 AC 78 ms
12,288 KB
testcase_24 AC 85 ms
13,672 KB
testcase_25 AC 65 ms
10,924 KB
testcase_26 AC 65 ms
10,112 KB
testcase_27 AC 60 ms
9,472 KB
testcase_28 AC 79 ms
12,928 KB
testcase_29 AC 78 ms
13,056 KB
testcase_30 AC 77 ms
12,928 KB
testcase_31 AC 70 ms
12,800 KB
testcase_32 AC 76 ms
15,012 KB
testcase_33 AC 78 ms
13,312 KB
testcase_34 AC 25 ms
6,944 KB
testcase_35 AC 53 ms
7,552 KB
testcase_36 AC 72 ms
11,680 KB
testcase_37 AC 75 ms
13,440 KB
testcase_38 AC 70 ms
13,440 KB
testcase_39 AC 76 ms
12,288 KB
testcase_40 AC 72 ms
11,776 KB
testcase_41 AC 74 ms
11,776 KB
testcase_42 AC 65 ms
11,008 KB
testcase_43 AC 71 ms
12,928 KB
testcase_44 AC 29 ms
8,448 KB
testcase_45 AC 60 ms
23,288 KB
testcase_46 AC 65 ms
22,756 KB
testcase_47 AC 29 ms
9,216 KB
testcase_48 AC 66 ms
12,916 KB
testcase_49 AC 63 ms
11,680 KB
testcase_50 AC 66 ms
13,040 KB
testcase_51 AC 67 ms
12,532 KB
testcase_52 AC 72 ms
12,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using Graph = vector< vector<int> >;

constexpr char newl = '\n';

struct UnionFind {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;

    UnionFind(int sz) : data(sz, -1) {}

    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            data[y] = x;
        }
        return is_union;
    }

    int find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int size(int x) {
        return -data[find(x)];
    }
};

bool dfs(int cur, vector<int>& memo, Graph& g, vector<int>& order) {
    memo[cur] = 1;
    for (int nex : g[cur]) {
        if (memo[nex] == 2) continue;
        if (memo[nex] == 1) return false;
        if (!dfs(nex, memo, g, order)) return false;
    }
    memo[cur] = 2;
    order.push_back(cur);
    return true;
}

bool tsort(Graph& g, vector<int>& order) {
    vector<int> memo(g.size(), 0);
    for (int i = 0; i < g.size(); i++) {
        if (memo[i] != 0) continue;
        if (!dfs(i, memo, g, order)) return false;
    }
    reverse(order.begin(), order.end());
    return true;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    Graph g(n);
    UnionFind uf(n);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        --a; --b;

        if (c == 1) {
            if (uf.same(a, b)) {
                cout << "Yes\n";
                return 0;
            }
            uf.unite(a, b);
        } else {
            g[a].push_back(b);
        }
    }

    Graph g2(n);
    for (int i = 0; i < n; i++) {
        int i2 = uf.find(i);
        for (int j : g[i]) {
            g2[i2].push_back(uf.find(j));
        }
    }

    vector<int> order;
    if (!tsort(g2, order)) {
        cout << "Yes\n";
        return 0;
    }

    cout << "No\n";
    return 0;
}
0