結果

問題 No.1023 Cyclic Tour
ユーザー outlineoutline
提出日時 2021-03-11 23:51:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,555 bytes
コンパイル時間 1,733 ms
コンパイル使用メモリ 154,620 KB
実行使用メモリ 33,028 KB
最終ジャッジ日時 2024-04-21 12:17:21
合計ジャッジ時間 15,946 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 142 ms
21,744 KB
testcase_05 AC 133 ms
21,688 KB
testcase_06 AC 142 ms
24,500 KB
testcase_07 AC 119 ms
23,052 KB
testcase_08 AC 117 ms
22,916 KB
testcase_09 AC 112 ms
22,460 KB
testcase_10 AC 110 ms
22,368 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 100 ms
22,856 KB
testcase_15 WA -
testcase_16 AC 241 ms
29,684 KB
testcase_17 AC 231 ms
30,084 KB
testcase_18 AC 231 ms
29,368 KB
testcase_19 AC 215 ms
28,580 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 245 ms
27,824 KB
testcase_26 AC 243 ms
28,788 KB
testcase_27 AC 218 ms
27,792 KB
testcase_28 AC 240 ms
30,236 KB
testcase_29 AC 226 ms
30,376 KB
testcase_30 AC 233 ms
30,908 KB
testcase_31 AC 216 ms
29,908 KB
testcase_32 AC 247 ms
30,536 KB
testcase_33 AC 281 ms
31,092 KB
testcase_34 AC 229 ms
27,156 KB
testcase_35 AC 281 ms
28,252 KB
testcase_36 WA -
testcase_37 AC 236 ms
29,800 KB
testcase_38 AC 228 ms
29,664 KB
testcase_39 WA -
testcase_40 AC 222 ms
29,076 KB
testcase_41 AC 249 ms
29,080 KB
testcase_42 AC 249 ms
28,428 KB
testcase_43 AC 224 ms
29,376 KB
testcase_44 AC 128 ms
21,964 KB
testcase_45 AC 115 ms
33,028 KB
testcase_46 AC 112 ms
28,428 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 221 ms
29,312 KB
testcase_52 AC 192 ms
29,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

struct StronglyConnectedComponents{
    using G = vector<vector<int>>;

    G graph;
    G rev;
    int n;
    int scc_sz = 0;
    G scc_graph;

    vector<int> scc_id;
    vector<int> vs;
    vector<bool> visited;

    StronglyConnectedComponents() = default;

    StronglyConnectedComponents(int V) : n(V) {
        graph.resize(n);
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
    }

    StronglyConnectedComponents(G& g) : graph(g), n(g.size()) {
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
        
        for(int u = 0; u < n; ++u){
            for(int v : graph[u])   rev[v].push_back(u);
        }
    }

    void resize(int V){
        n = V;
        graph.resize(n);
        rev.resize(n);
        scc_id.assign(n, -1);
        visited.assign(n, false);
    }

    void add_edge(int u, int v){
        graph[u].emplace_back(v);
        rev[v].emplace_back(u);
    }

    void dfs(int from){
        visited[from] = true;
        for(int to : graph[from]){
            if(!visited[to])  dfs(to);
        }
        vs.push_back(from);
    }

    void rdfs(int from, int k){
        scc_id[from] = k;
        for(int to : rev[from]){
            if(scc_id[to] == -1)    rdfs(to, k);
        }
    }

    void build(){
        for(int v = 0; v < n; ++v){
            if(!visited[v]) dfs(v);
        }
        reverse(vs.begin(), vs.end());

        for(int v : vs){
            if(scc_id[v] == -1)    rdfs(v, scc_sz++);
        }

        scc_graph.resize(scc_sz);
        for(int v = 0; v < n; ++v){
            scc_graph[scc_id[v]].push_back(v);
        }

        return;
    }

    int size() { return scc_sz; }

    vector<int> get_set(int k) { return scc_graph[k]; }

    int operator[](int v) { return scc_id[v]; }
};

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

    int N, M;
    cin >> N >> M;
    StronglyConnectedComponents g(N);
    map<pair<int, int>, int> edge_cnt;
    for(int i = 0; i < M; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        --a, --b;
        if(c == 1){
            g.add_edge(a, b);
            g.add_edge(b, a);
        }
        else{
            g.add_edge(a, b);
            if(a > b)   swap(a, b);
        }
        edge_cnt[{a, b}] += 1;
    }
    g.build();
    for(int i = 0; i < g.size(); ++i){
        auto vs = g.get_set(i);
        if(vs.size() > 2){
            cout << "Yes\n";
            return 0;
        }
        if(vs.size() == 2 && edge_cnt[minmax(vs[0], vs[1])] > 1){
            cout << "Yes\n";
            return 0;
        }
    }
    cout << "No\n";

    return 0;
}
0