結果

問題 No.1023 Cyclic Tour
ユーザー rodearodea
提出日時 2020-04-10 22:08:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,738 bytes
コンパイル時間 2,852 ms
コンパイル使用メモリ 127,996 KB
実行使用メモリ 31,836 KB
最終ジャッジ日時 2023-10-14 00:45:52
合計ジャッジ時間 21,277 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,020 KB
testcase_01 AC 6 ms
7,916 KB
testcase_02 AC 5 ms
8,184 KB
testcase_03 AC 5 ms
7,960 KB
testcase_04 AC 213 ms
21,324 KB
testcase_05 AC 213 ms
21,464 KB
testcase_06 AC 239 ms
23,652 KB
testcase_07 AC 226 ms
22,432 KB
testcase_08 AC 203 ms
22,800 KB
testcase_09 AC 192 ms
22,124 KB
testcase_10 AC 189 ms
22,244 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 176 ms
22,460 KB
testcase_15 WA -
testcase_16 AC 347 ms
29,016 KB
testcase_17 AC 349 ms
29,420 KB
testcase_18 AC 351 ms
29,128 KB
testcase_19 AC 333 ms
28,272 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 396 ms
27,456 KB
testcase_26 AC 383 ms
28,348 KB
testcase_27 AC 357 ms
27,760 KB
testcase_28 AC 396 ms
29,608 KB
testcase_29 AC 374 ms
29,524 KB
testcase_30 AC 403 ms
30,184 KB
testcase_31 AC 368 ms
29,208 KB
testcase_32 AC 375 ms
30,132 KB
testcase_33 AC 396 ms
30,576 KB
testcase_34 AC 379 ms
27,132 KB
testcase_35 AC 392 ms
27,736 KB
testcase_36 WA -
testcase_37 AC 363 ms
29,228 KB
testcase_38 AC 348 ms
29,420 KB
testcase_39 WA -
testcase_40 AC 396 ms
28,592 KB
testcase_41 AC 375 ms
28,724 KB
testcase_42 AC 400 ms
28,040 KB
testcase_43 AC 342 ms
29,096 KB
testcase_44 AC 217 ms
21,240 KB
testcase_45 AC 216 ms
31,836 KB
testcase_46 AC 173 ms
27,556 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 WA -
testcase_51 AC 325 ms
28,692 KB
testcase_52 AC 343 ms
28,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;

#define all(a) (a).begin(), (a).end()

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

const int MAX_V = 100010;

int V;
vector<vector<int>> G(MAX_V);
vector<vector<int>> rG(MAX_V);
vector<int> vs;
vector<bool> used(MAX_V);
vector<int> cmp(MAX_V);

void add_edge(int from, int to){
    G[from].push_back(to);
    rG[to].push_back(from);
}

void dfs(int v){
    used[v] = true;
    
    for(int i=0; i<G[v].size(); i++){
        if(!used[G[v][i]]) dfs(G[v][i]);
    }

    vs.push_back(v);
}

void rdfs(int v, int k){
    used[v] = true;
    cmp[v] = k;

    for(int i=0; i<rG[v].size(); i++){
        if(!used[rG[v][i]]) rdfs(rG[v][i], k);
    }
}

int scc(){
    for(int i=0; i<MAX_V; i++) used[i] = false;
    vs.clear();
    
    for(int v=0; v<V; v++){
        if(!used[v]) dfs(v);
    }

    for(int i=0; i<MAX_V; i++) used[i] = false;
    int k = 0;

    for(int i=vs.size()-1; i>=0; i--){
        if(!used[vs[i]]) rdfs(vs[i], k++);
    }

    return k;
}

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

    int m; cin>>V>>m;
    map<P, int> edge;
    for(int i=0; i<m; i++){
        int a, b, c; cin>>a>>b>>c;
        a--, b--;
        add_edge(a, b);
        if(c == 1) add_edge(b, a);
        edge[minmax(a, b)]++;
    }

    scc();

    map<int, int> cnt;
    for(int i=0; i<V; i++){
        cnt[cmp[i]]++;
    }

    int ma = 0;
    for(auto i:cnt) chmax(ma, i.second);

    if(2 < ma){
        cout << "Yes" << endl;
        return 0;
    }

    bool valid = false;
    if(ma == 2){
        vector<vector<int>> G(V);
        for(int i=0; i<V; i++){
            G[cmp[i]].emplace_back(i);
        }

        for(int i=0; i<V; i++){
            if(G[i].size() == 2){
                int val1 = G[i][0], val2 = G[i][1];
                if(edge[minmax(val1, val2)] >= 2) valid = true;
            }
        }
    }

    cout << (valid ? "Yes" : "No") << endl;

    return 0;
}
0