結果

問題 No.1023 Cyclic Tour
ユーザー hiroaki0615hiroaki0615
提出日時 2020-04-10 23:10:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,310 bytes
コンパイル時間 1,072 ms
コンパイル使用メモリ 96,772 KB
実行使用メモリ 31,324 KB
最終ジャッジ日時 2023-10-14 04:35:45
合計ジャッジ時間 10,574 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 29 ms
5,344 KB
testcase_05 AC 29 ms
5,400 KB
testcase_06 AC 32 ms
5,444 KB
testcase_07 AC 30 ms
5,472 KB
testcase_08 AC 129 ms
14,864 KB
testcase_09 AC 119 ms
14,524 KB
testcase_10 AC 119 ms
14,576 KB
testcase_11 AC 133 ms
15,228 KB
testcase_12 AC 179 ms
18,668 KB
testcase_13 AC 157 ms
17,744 KB
testcase_14 AC 150 ms
17,504 KB
testcase_15 AC 170 ms
17,808 KB
testcase_16 AC 255 ms
20,396 KB
testcase_17 AC 265 ms
20,612 KB
testcase_18 AC 259 ms
19,140 KB
testcase_19 AC 255 ms
19,112 KB
testcase_20 AC 72 ms
7,292 KB
testcase_21 AC 75 ms
7,400 KB
testcase_22 AC 140 ms
10,960 KB
testcase_23 AC 156 ms
11,956 KB
testcase_24 AC 230 ms
17,212 KB
testcase_25 AC 75 ms
7,408 KB
testcase_26 AC 76 ms
7,460 KB
testcase_27 AC 64 ms
7,064 KB
testcase_28 AC 224 ms
16,472 KB
testcase_29 AC 244 ms
18,644 KB
testcase_30 AC 210 ms
15,972 KB
testcase_31 AC 206 ms
15,308 KB
testcase_32 AC 202 ms
19,192 KB
testcase_33 AC 207 ms
15,872 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 107 ms
8,616 KB
testcase_37 AC 168 ms
15,108 KB
testcase_38 AC 238 ms
18,400 KB
testcase_39 AC 178 ms
12,332 KB
testcase_40 AC 174 ms
13,248 KB
testcase_41 AC 171 ms
13,056 KB
testcase_42 AC 70 ms
7,308 KB
testcase_43 AC 229 ms
17,192 KB
testcase_44 AC 32 ms
5,464 KB
testcase_45 AC 170 ms
31,304 KB
testcase_46 AC 177 ms
31,324 KB
testcase_47 AC 27 ms
5,312 KB
testcase_48 AC 57 ms
7,308 KB
testcase_49 AC 59 ms
7,380 KB
testcase_50 AC 53 ms
7,196 KB
testcase_51 AC 57 ms
7,316 KB
testcase_52 AC 55 ms
7,452 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)];
        }
};

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

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];
    }
    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 = 0;
    for (auto s : S) {
        node_map[s] = node++;
    }
    vector<vector<int>> graph(node);
    rep(i, 0, M) {
        if (C[i] == 2) {
            int u = node_map[uf.getRoot(A[i])];
            int v = node_map[uf.getRoot(B[i])];
            graph[u].push_back(v);
        }
    }
    vector<bool> seen(node, false), finished(node, false);
    int pos = -1;
    rep(i, 0, node) {
        if (!finished[i]) {
            pos = -1;
            dfs(graph, seen, finished, i, pos);
            if (pos != -1) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }
    cout << "No" << endl;
    return 0;
}
0