結果

問題 No.1023 Cyclic Tour
ユーザー TAISA_TAISA_
提出日時 2020-04-10 22:16:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 2,903 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 214,712 KB
実行使用メモリ 27,296 KB
最終ジャッジ日時 2023-10-14 00:56:25
合計ジャッジ時間 9,241 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 30 ms
7,348 KB
testcase_05 AC 29 ms
7,208 KB
testcase_06 AC 31 ms
7,520 KB
testcase_07 AC 31 ms
7,276 KB
testcase_08 AC 38 ms
8,536 KB
testcase_09 AC 75 ms
20,084 KB
testcase_10 AC 71 ms
20,196 KB
testcase_11 AC 77 ms
21,280 KB
testcase_12 AC 84 ms
22,592 KB
testcase_13 AC 81 ms
21,412 KB
testcase_14 AC 73 ms
21,276 KB
testcase_15 AC 79 ms
21,468 KB
testcase_16 AC 128 ms
23,308 KB
testcase_17 AC 124 ms
23,716 KB
testcase_18 AC 123 ms
21,804 KB
testcase_19 AC 125 ms
22,472 KB
testcase_20 AC 73 ms
20,188 KB
testcase_21 AC 77 ms
20,788 KB
testcase_22 AC 91 ms
21,524 KB
testcase_23 AC 98 ms
21,928 KB
testcase_24 AC 118 ms
23,696 KB
testcase_25 AC 75 ms
20,448 KB
testcase_26 AC 75 ms
20,200 KB
testcase_27 AC 70 ms
19,284 KB
testcase_28 AC 106 ms
22,900 KB
testcase_29 AC 120 ms
23,572 KB
testcase_30 AC 111 ms
23,552 KB
testcase_31 AC 106 ms
22,872 KB
testcase_32 AC 112 ms
22,268 KB
testcase_33 AC 113 ms
23,292 KB
testcase_34 AC 23 ms
8,092 KB
testcase_35 AC 46 ms
8,292 KB
testcase_36 AC 82 ms
20,716 KB
testcase_37 AC 108 ms
22,232 KB
testcase_38 AC 119 ms
22,484 KB
testcase_39 AC 102 ms
21,956 KB
testcase_40 AC 101 ms
21,952 KB
testcase_41 AC 111 ms
21,812 KB
testcase_42 AC 59 ms
9,064 KB
testcase_43 AC 57 ms
9,620 KB
testcase_44 AC 46 ms
18,324 KB
testcase_45 AC 102 ms
27,296 KB
testcase_46 AC 92 ms
24,684 KB
testcase_47 AC 47 ms
18,756 KB
testcase_48 AC 72 ms
20,728 KB
testcase_49 AC 76 ms
20,760 KB
testcase_50 AC 73 ms
20,804 KB
testcase_51 AC 54 ms
8,544 KB
testcase_52 AC 55 ms
9,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
#define pb push_back
#define eb emplace_back
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using V = vector<int>;
using VL = vector<ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll MOD = 1e9 + 7;
constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
template <class T>
void chmin(T &a, T b) { a = min(a, b); }
template <class T>
void chmax(T &a, T b) { a = max(a, b); }
void ok() { cerr << "ok" << endl; }
struct UnionFind {
    vector<int> par, sz;
    UnionFind(int n) {
        par.resize(n);
        sz.resize(n, 1);
        iota(all(par), 0);
    }
    inline int find(int x) {
        if (x == par[x]) return x;
        return par[x] = find(par[x]);
    }
    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;
        if (sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
    }
    inline bool same(int u, int v) {
        return find(u) == find(v);
    }
};
vector<vector<int>> G;
struct SCC {
    int n;
    vector<int> ord, vis, cm;
    vector<vector<int>> bl, rG;
    SCC(int n) : n(n), bl(n), rG(n), cm(n), vis(n) {}
    void dfs(int i) {
        vis[i] = 1;
        for (auto &e : G[i]) {
            if (vis[e]) continue;
            dfs(e);
        }
        ord.eb(i);
    }
    void rdfs(int i, int c) {
        vis[i] = 1;
        bl[c].eb(i);
        cm[i] = c;
        for (auto &e : rG[i]) {
            if (vis[e]) continue;
            rdfs(e, c);
        }
    }
    void build() {
        for (int i = 0; i < n; i++) {
            for (auto &e : G[i]) {
                rG[e].eb(i);
            }
        }
        for (int i = 0; i < n; i++) {
            if (!vis[i]) dfs(i);
        }
        reverse(all(ord));
        vis.assign(n, 0);
        int k = 0;
        for (auto &e : ord) {
            if (!vis[e]) {
                bl.eb(vector<int>());
                rdfs(e, k);
                k++;
            }
        }
    }
};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    UnionFind uf(n);
    V a(m), b(m), c(m);
    G.resize(n);
    for (int i = 0; i < m; i++) {
        cin >> a[i] >> b[i] >> c[i];
        --a[i];
        --b[i];
        if (c[i] == 1) {
            if (uf.same(a[i], b[i])) {
                cout << "Yes\n";
                return 0;
            }
            uf.unite(a[i], b[i]);
        }
    }
    for (int i = 0; i < m; i++) {
        if (c[i] == 2) {
            if (uf.same(a[i], b[i])) {
                cout << "Yes\n";
                return 0;
            }
            G[uf.find(a[i])].eb(uf.find(b[i]));
        }
    }
    SCC scc(n);
    scc.build();
    for (int i = 0; i < n; i++) {
        if (scc.bl[i].size() > 1) {
            cout << "Yes\n";
            return 0;
        }
    }
    cout << "No\n";
    return 0;
}
0