結果

問題 No.1023 Cyclic Tour
ユーザー hiroaki0615hiroaki0615
提出日時 2020-04-10 22:56:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,306 bytes
コンパイル時間 1,164 ms
コンパイル使用メモリ 95,432 KB
実行使用メモリ 31,440 KB
最終ジャッジ日時 2023-10-14 03:51:49
合計ジャッジ時間 11,215 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,356 KB
testcase_04 AC 29 ms
5,368 KB
testcase_05 AC 29 ms
5,500 KB
testcase_06 AC 32 ms
5,364 KB
testcase_07 AC 30 ms
5,372 KB
testcase_08 AC 133 ms
14,952 KB
testcase_09 AC 124 ms
14,464 KB
testcase_10 AC 127 ms
14,748 KB
testcase_11 AC 143 ms
15,348 KB
testcase_12 AC 183 ms
18,588 KB
testcase_13 AC 170 ms
17,860 KB
testcase_14 AC 162 ms
17,572 KB
testcase_15 AC 180 ms
17,800 KB
testcase_16 AC 277 ms
20,416 KB
testcase_17 AC 278 ms
20,644 KB
testcase_18 AC 268 ms
19,124 KB
testcase_19 AC 275 ms
19,268 KB
testcase_20 AC 72 ms
7,356 KB
testcase_21 AC 74 ms
7,492 KB
testcase_22 AC 142 ms
10,988 KB
testcase_23 AC 156 ms
11,784 KB
testcase_24 AC 250 ms
17,260 KB
testcase_25 AC 74 ms
7,380 KB
testcase_26 AC 75 ms
7,500 KB
testcase_27 AC 63 ms
7,176 KB
testcase_28 AC 236 ms
16,532 KB
testcase_29 AC 265 ms
18,572 KB
testcase_30 AC 220 ms
15,976 KB
testcase_31 AC 196 ms
15,212 KB
testcase_32 AC 219 ms
19,112 KB
testcase_33 AC 216 ms
15,928 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 108 ms
8,536 KB
testcase_37 AC 173 ms
15,152 KB
testcase_38 AC 255 ms
18,328 KB
testcase_39 AC 172 ms
12,256 KB
testcase_40 AC 177 ms
13,384 KB
testcase_41 AC 173 ms
13,088 KB
testcase_42 AC 69 ms
7,320 KB
testcase_43 AC 253 ms
17,508 KB
testcase_44 AC 33 ms
5,448 KB
testcase_45 AC 185 ms
31,440 KB
testcase_46 AC 178 ms
31,388 KB
testcase_47 AC 27 ms
5,512 KB
testcase_48 AC 58 ms
7,364 KB
testcase_49 AC 60 ms
7,360 KB
testcase_50 AC 53 ms
7,200 KB
testcase_51 AC 56 ms
7,308 KB
testcase_52 AC 55 ms
7,204 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 (!seen[i]) {
            pos = -1;
            dfs(graph, seen, finished, i, pos);
            if (pos != -1) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }
    cout << "No" << endl;
    return 0;
}
0