結果

問題 No.1023 Cyclic Tour
ユーザー hiroaki0615hiroaki0615
提出日時 2020-04-11 18:15:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 278 ms / 2,000 ms
コード長 4,297 bytes
コンパイル時間 1,121 ms
コンパイル使用メモリ 95,468 KB
実行使用メモリ 33,816 KB
最終ジャッジ日時 2023-10-19 09:39:02
合計ジャッジ時間 11,689 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 46 ms
9,596 KB
testcase_05 AC 44 ms
9,596 KB
testcase_06 AC 50 ms
10,140 KB
testcase_07 AC 46 ms
9,876 KB
testcase_08 AC 140 ms
18,572 KB
testcase_09 AC 132 ms
18,044 KB
testcase_10 AC 134 ms
18,488 KB
testcase_11 AC 155 ms
18,852 KB
testcase_12 AC 181 ms
21,408 KB
testcase_13 AC 177 ms
20,420 KB
testcase_14 AC 161 ms
20,156 KB
testcase_15 AC 176 ms
20,420 KB
testcase_16 AC 278 ms
23,060 KB
testcase_17 AC 265 ms
23,324 KB
testcase_18 AC 266 ms
22,004 KB
testcase_19 AC 266 ms
21,740 KB
testcase_20 AC 108 ms
12,764 KB
testcase_21 AC 111 ms
13,180 KB
testcase_22 AC 178 ms
16,460 KB
testcase_23 AC 193 ms
16,988 KB
testcase_24 AC 263 ms
21,212 KB
testcase_25 AC 107 ms
12,764 KB
testcase_26 AC 115 ms
12,984 KB
testcase_27 AC 100 ms
12,432 KB
testcase_28 AC 262 ms
20,420 KB
testcase_29 AC 270 ms
21,740 KB
testcase_30 AC 241 ms
20,420 KB
testcase_31 AC 222 ms
20,156 KB
testcase_32 AC 226 ms
23,324 KB
testcase_33 AC 224 ms
20,948 KB
testcase_34 AC 70 ms
10,388 KB
testcase_35 AC 72 ms
10,916 KB
testcase_36 AC 138 ms
14,084 KB
testcase_37 AC 195 ms
19,892 KB
testcase_38 AC 248 ms
21,740 KB
testcase_39 AC 189 ms
17,252 KB
testcase_40 AC 199 ms
17,780 KB
testcase_41 AC 194 ms
17,780 KB
testcase_42 AC 108 ms
12,904 KB
testcase_43 AC 245 ms
20,948 KB
testcase_44 AC 63 ms
10,932 KB
testcase_45 AC 161 ms
33,816 KB
testcase_46 AC 165 ms
33,816 KB
testcase_47 AC 63 ms
23,520 KB
testcase_48 AC 85 ms
13,584 KB
testcase_49 AC 86 ms
13,444 KB
testcase_50 AC 78 ms
13,584 KB
testcase_51 AC 81 ms
13,584 KB
testcase_52 AC 79 ms
13,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#define rep(i, start, end) for (int i = (int)start; i < (int)end; ++i)
#define rrep(i, start, end) for (int i = (int)start - 1; i >= (int)end; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
template<typename T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return 0;}
template<typename T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return 0;}

class UnionFind {
private:
    vector<int> parent_;
    vector<int> node_rank_;
    vector<int> sizes_;
public:
    UnionFind(int node_num):
    parent_(vector<int>(node_num)), node_rank_(vector<int>(node_num)), sizes_(vector<int>(node_num)) {
        for (int i = 0; i < node_num; ++i) {
            parent_[i] = i;
            node_rank_[i] = 0;
            sizes_[i] = 1;
        }
    }
    int getRoot(int u) {
        return parent_[u] == u ? u : parent_[u] = getRoot(parent_[u]);
    }
    bool isSame(int u, int v) {
        return getRoot(u) == getRoot(v);
    }
    void unite(int u, int v) {
        u = getRoot(u);
        v = getRoot(v);
        if (u == v) return;
        if (node_rank_[u] < node_rank_[v]) {
            parent_[u] = v;
            sizes_[v] += sizes_[u];
        }
        else {
            parent_[v] = u;
            sizes_[u] += sizes_[v];
            if (node_rank_[u] == node_rank_[v]) {
                node_rank_[u]++;
            }
        }
    }
    int getSize(int u) {
        return sizes_[getRoot(u)];
    }
};

bool dfs1(const vector<vector<int>>& graph, vector<bool>& seen, vector<bool>& finished, int node, int parent) {
    seen[node] = true;
    for (auto& next_node : graph[node]) {
        if (next_node == parent) {
            continue;
        }
        if (finished[next_node]) {
            continue;
        }
        if (seen[next_node] && !finished[next_node]) {
            return true;
        }
        if (dfs1(graph, seen, finished, next_node, node)) {
            return true;
        }
    }
    finished[node] = true;
    return false;
}

bool dfs2(const vector<vector<int>>& graph, vector<bool>& seen, vector<bool>& finished, int node) {
    seen[node] = true;
    for (auto& next_node : graph[node]) {
        if (finished[next_node]) {
            continue;
        }
        if (seen[next_node] && !finished[next_node]) {
            return true;
        }
        if (dfs2(graph, seen, finished, next_node)) {
            return true;
        }
    }
    finished[node] = true;
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    vector<int> A(M), B(M), C(M);
    rep(i, 0, M) {
        cin >> A[i] >> B[i] >> C[i];
        --A[i], --B[i];
    }
    // 無向辺のみで構築されるグラフに閉路があるかを確かめる
    vector<vector<int>> graph1(N);
    rep(i, 0, M) {
        if (C[i] == 1) {
            graph1[A[i]].push_back(B[i]);
            graph1[B[i]].push_back(A[i]);
        }
    }
    vector<bool> seen1(N, false), finished1(N, false);
    rep(u, 0, N) {
        if (!seen1[u] && dfs1(graph1, seen1, finished1, u, -1)) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    // 無向辺で連結される頂点をまとめる
    UnionFind uf(N);
    rep(i, 0, M) {
        if (C[i] == 1) {
            uf.unite(A[i], B[i]);
        }
    }
    set<int> S;
    rep(i, 0, N) {
        S.insert(uf.getRoot(i));
    }
    map<int, int> node_map;
    int node_num = 0;
    for (auto s : S) {
        node_map[s] = node_num++;
    }
    // まとめた頂点と有向辺でグラフを構築する
    vector<vector<int>> graph2(node_num);
    rep(i, 0, M) {
        if (C[i] == 2) {
            int u = node_map[uf.getRoot(A[i])];
            int v = node_map[uf.getRoot(B[i])];
            graph2[u].push_back(v);
        }
    }
    // 新しく構築したグラフにおいて閉路検出を行う
    vector<bool> seen2(node_num, false);
    vector<bool> finished2(node_num, false);
    rep(u, 0, node_num) {
        if (!seen2[u] && dfs2(graph2, seen2, finished2, u)) {
                cout << "Yes" << endl;
                return 0;
            }
    }
    cout << "No" << endl;
    return 0;
}
0