結果
| 問題 | 
                            No.1023 Cyclic Tour
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-04-24 19:14:03 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 142 ms / 2,000 ms | 
| コード長 | 3,757 bytes | 
| コンパイル時間 | 1,662 ms | 
| コンパイル使用メモリ | 129,808 KB | 
| 実行使用メモリ | 30,328 KB | 
| 最終ジャッジ日時 | 2024-10-15 02:05:57 | 
| 合計ジャッジ時間 | 8,211 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 49 | 
ソースコード
#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;
}