結果

問題 No.1023 Cyclic Tour
ユーザー finefine
提出日時 2020-05-19 02:56:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,015 ms / 2,000 ms
コード長 2,510 bytes
コンパイル時間 1,876 ms
コンパイル使用メモリ 171,176 KB
実行使用メモリ 28,160 KB
最終ジャッジ日時 2024-04-09 21:55:16
合計ジャッジ時間 18,865 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 26 ms
6,944 KB
testcase_05 AC 28 ms
6,944 KB
testcase_06 AC 28 ms
6,940 KB
testcase_07 AC 27 ms
6,944 KB
testcase_08 AC 50 ms
11,648 KB
testcase_09 AC 76 ms
11,520 KB
testcase_10 AC 51 ms
11,392 KB
testcase_11 AC 426 ms
12,288 KB
testcase_12 AC 523 ms
13,276 KB
testcase_13 AC 493 ms
12,800 KB
testcase_14 AC 92 ms
12,160 KB
testcase_15 AC 512 ms
13,040 KB
testcase_16 AC 82 ms
14,080 KB
testcase_17 AC 82 ms
14,208 KB
testcase_18 AC 80 ms
13,440 KB
testcase_19 AC 79 ms
13,440 KB
testcase_20 AC 531 ms
11,520 KB
testcase_21 AC 517 ms
11,688 KB
testcase_22 AC 635 ms
12,160 KB
testcase_23 AC 787 ms
12,364 KB
testcase_24 AC 1,015 ms
13,528 KB
testcase_25 AC 83 ms
10,880 KB
testcase_26 AC 74 ms
10,240 KB
testcase_27 AC 338 ms
9,600 KB
testcase_28 AC 362 ms
12,928 KB
testcase_29 AC 90 ms
13,056 KB
testcase_30 AC 111 ms
12,928 KB
testcase_31 AC 100 ms
13,056 KB
testcase_32 AC 231 ms
16,508 KB
testcase_33 AC 132 ms
13,916 KB
testcase_34 AC 25 ms
6,940 KB
testcase_35 AC 51 ms
7,552 KB
testcase_36 AC 572 ms
11,776 KB
testcase_37 AC 161 ms
14,208 KB
testcase_38 AC 127 ms
14,080 KB
testcase_39 AC 726 ms
12,288 KB
testcase_40 AC 87 ms
11,904 KB
testcase_41 AC 122 ms
11,904 KB
testcase_42 AC 69 ms
10,880 KB
testcase_43 AC 415 ms
12,928 KB
testcase_44 AC 30 ms
8,696 KB
testcase_45 AC 551 ms
28,160 KB
testcase_46 AC 557 ms
27,264 KB
testcase_47 AC 31 ms
9,216 KB
testcase_48 AC 550 ms
13,044 KB
testcase_49 AC 543 ms
11,680 KB
testcase_50 AC 546 ms
13,044 KB
testcase_51 AC 529 ms
12,532 KB
testcase_52 AC 536 ms
12,532 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]) {
        cerr << cur << " " << nex << newl;
        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