結果

問題 No.1023 Cyclic Tour
ユーザー 👑 jupirojupiro
提出日時 2020-04-24 19:14:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 3,757 bytes
コンパイル時間 1,568 ms
コンパイル使用メモリ 129,524 KB
実行使用メモリ 30,332 KB
最終ジャッジ日時 2024-04-23 02:41:53
合計ジャッジ時間 7,202 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 29 ms
6,016 KB
testcase_05 AC 26 ms
6,016 KB
testcase_06 AC 28 ms
6,144 KB
testcase_07 AC 27 ms
5,888 KB
testcase_08 AC 34 ms
8,144 KB
testcase_09 AC 61 ms
19,572 KB
testcase_10 AC 59 ms
19,600 KB
testcase_11 AC 67 ms
20,628 KB
testcase_12 AC 73 ms
22,976 KB
testcase_13 AC 68 ms
21,968 KB
testcase_14 AC 67 ms
21,636 KB
testcase_15 AC 72 ms
22,184 KB
testcase_16 AC 126 ms
23,924 KB
testcase_17 AC 118 ms
24,060 KB
testcase_18 AC 120 ms
22,408 KB
testcase_19 AC 116 ms
22,420 KB
testcase_20 AC 61 ms
17,020 KB
testcase_21 AC 62 ms
17,248 KB
testcase_22 AC 84 ms
19,136 KB
testcase_23 AC 89 ms
19,804 KB
testcase_24 AC 107 ms
23,548 KB
testcase_25 AC 65 ms
16,920 KB
testcase_26 AC 62 ms
16,860 KB
testcase_27 AC 58 ms
16,120 KB
testcase_28 AC 104 ms
22,924 KB
testcase_29 AC 112 ms
24,076 KB
testcase_30 AC 104 ms
22,776 KB
testcase_31 AC 100 ms
21,872 KB
testcase_32 AC 105 ms
22,580 KB
testcase_33 AC 103 ms
22,396 KB
testcase_34 AC 21 ms
6,524 KB
testcase_35 AC 46 ms
7,220 KB
testcase_36 AC 73 ms
17,672 KB
testcase_37 AC 88 ms
20,764 KB
testcase_38 AC 105 ms
22,860 KB
testcase_39 AC 89 ms
20,080 KB
testcase_40 AC 95 ms
20,596 KB
testcase_41 AC 94 ms
20,464 KB
testcase_42 AC 53 ms
7,212 KB
testcase_43 AC 54 ms
8,428 KB
testcase_44 AC 35 ms
13,992 KB
testcase_45 AC 100 ms
30,332 KB
testcase_46 AC 101 ms
25,340 KB
testcase_47 AC 35 ms
14,300 KB
testcase_48 AC 60 ms
17,408 KB
testcase_49 AC 60 ms
17,524 KB
testcase_50 AC 58 ms
17,276 KB
testcase_51 AC 50 ms
7,548 KB
testcase_52 AC 50 ms
7,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
//constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;


using  UnWeightedGraph = vector<vector<int>>;
template< typename G >
struct StronglyConnectedComponents {
    const G& g;
    UnWeightedGraph gg, rg;
    vector< int > comp, order, used;

    StronglyConnectedComponents(G& g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
        for (int i = 0; i < g.size(); i++) {
            for (auto e : g[i]) {
                gg[i].emplace_back((int)e);
                rg[(int)e].emplace_back(i);
            }
        }
    }

    int operator[](int k) {
        return comp[k];
    }

    void dfs(int idx) {
        if (used[idx]) return;
        used[idx] = true;
        for (int to : gg[idx]) dfs(to);
        order.push_back(idx);
    }

    void rdfs(int idx, int cnt) {
        if (comp[idx] != -1) return;
        comp[idx] = cnt;
        for (int to : rg[idx]) rdfs(to, cnt);
    }

    void build(UnWeightedGraph& t) {
        for (int i = 0; i < gg.size(); i++) dfs(i);
        reverse(begin(order), end(order));
        int ptr = 0;
        for (int i : order) if (comp[i] == -1) rdfs(i, ptr), ptr++;

        t.resize(ptr);
        for (int i = 0; i < g.size(); i++) {
            for (auto& to : g[i]) {
                int x = comp[i], y = comp[to];
                if (x == y) continue;
                t[x].push_back(y);
            }
        }
    }
};

class UnionFind {
public:
    vector<int> Parent;
    UnionFind(int N) {
        Parent = vector<int>(N, -1);
    }
    int root(int A) {
        if (Parent[A] < 0) {
            return A;
        }
        return Parent[A] = root(Parent[A]);
    }

    long long size(int A) {
        return -(long long)Parent[root(A)];
    }

    bool connect(int A, int B) {
        A = root(A);
        B = root(B);
        if (A == B) {
            return false;
        }
        if (size(A) < size(B)) {
            swap(A, B);
        }

        Parent[A] += Parent[B];
        Parent[B] = A;

        return true;
    }

};

int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int n, m; scanf("%d %d", &n, &m);
	vector<vector<int>> g(n);
    vector<pair<int, int>> v;
    UnionFind uni(n);
    int cnt = 0;
    int edges = 0;
    for (int i = 0; i < m; i++) {
        int a, b, c; scanf("%d %d %d", &a, &b, &c);
        a--; b--;
        if (c == 1) {
            if (!uni.connect(a, b)) {
                puts("Yes");
                return 0;
            }
        }
        else v.emplace_back(a, b);
    }
    for (auto p : v) {
        if (uni.root(p.first) == uni.root(p.second)) {
            puts("Yes");
            return 0;
        }
        g[uni.root(p.first)].push_back(uni.root(p.second));
    }
    StronglyConnectedComponents<vector<vector<int>>> scc(g);
    vector<vector<int>> t;
    scc.build(t);
    if (t.size() < n) puts("Yes");
    else puts("No");
	return 0;
}
 
0