結果

問題 No.1023 Cyclic Tour
ユーザー catuppercatupper
提出日時 2020-04-10 23:05:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 2,027 bytes
コンパイル時間 2,368 ms
コンパイル使用メモリ 79,176 KB
実行使用メモリ 26,688 KB
最終ジャッジ日時 2023-10-14 04:17:25
合計ジャッジ時間 16,009 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
11,136 KB
testcase_01 AC 6 ms
11,064 KB
testcase_02 AC 5 ms
11,120 KB
testcase_03 AC 5 ms
11,208 KB
testcase_04 AC 180 ms
21,268 KB
testcase_05 AC 178 ms
21,348 KB
testcase_06 AC 203 ms
23,228 KB
testcase_07 AC 187 ms
22,260 KB
testcase_08 AC 152 ms
19,680 KB
testcase_09 AC 152 ms
19,492 KB
testcase_10 AC 144 ms
19,440 KB
testcase_11 AC 165 ms
20,380 KB
testcase_12 AC 144 ms
20,048 KB
testcase_13 AC 139 ms
19,324 KB
testcase_14 AC 135 ms
19,524 KB
testcase_15 AC 143 ms
19,664 KB
testcase_16 AC 256 ms
22,460 KB
testcase_17 AC 259 ms
22,596 KB
testcase_18 AC 261 ms
22,436 KB
testcase_19 AC 254 ms
22,204 KB
testcase_20 AC 295 ms
22,988 KB
testcase_21 AC 302 ms
23,636 KB
testcase_22 AC 296 ms
22,460 KB
testcase_23 AC 303 ms
22,980 KB
testcase_24 AC 285 ms
23,092 KB
testcase_25 AC 296 ms
23,028 KB
testcase_26 AC 288 ms
23,716 KB
testcase_27 AC 257 ms
23,180 KB
testcase_28 AC 268 ms
22,548 KB
testcase_29 AC 258 ms
22,196 KB
testcase_30 AC 287 ms
23,028 KB
testcase_31 AC 270 ms
22,888 KB
testcase_32 AC 265 ms
24,376 KB
testcase_33 AC 289 ms
23,640 KB
testcase_34 AC 281 ms
22,344 KB
testcase_35 AC 299 ms
23,236 KB
testcase_36 AC 299 ms
22,988 KB
testcase_37 AC 274 ms
23,740 KB
testcase_38 AC 255 ms
22,692 KB
testcase_39 AC 294 ms
22,472 KB
testcase_40 AC 275 ms
22,404 KB
testcase_41 AC 288 ms
22,516 KB
testcase_42 AC 310 ms
23,300 KB
testcase_43 AC 253 ms
22,144 KB
testcase_44 AC 174 ms
21,280 KB
testcase_45 AC 150 ms
26,024 KB
testcase_46 AC 135 ms
25,072 KB
testcase_47 AC 156 ms
26,688 KB
testcase_48 AC 226 ms
23,332 KB
testcase_49 AC 234 ms
23,000 KB
testcase_50 AC 224 ms
23,556 KB
testcase_51 AC 223 ms
23,304 KB
testcase_52 AC 226 ms
23,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;


int n, m;
vector<P> edge[110000];
vector<int> rev[110000];
vector<int> group[110000];
int a, b, c;
bool done[110000];
Int comp[110000];
vector<int> nodes;

void dfs(int x, int last = -1){
    if(done[x])return;
    done[x] = true;
    for(auto p:edge[x]){
        int to = p.first;
        if(to == last)continue;
        dfs(to, x);
    }
    nodes.push_back(x);
}

void dfs2(int x, int g, int last = -1){
    if(done[x])return;
    done[x] = true;
    group[g].push_back(x);
    comp[x] = g;
    for(auto to:rev[x]){
        if(to == last)continue;
        dfs2(to, g, x);        
    }
}

void scc(){
    for(int i = 1;i <= n;i++){
        dfs(i);
    }
    reverse(nodes.begin(), nodes.end());
    fill(done, done + n + 1, false);
    for(auto node:nodes){
        if(!done[node])
            dfs2(node, node);
    }
}

void ok(){
    cout << "Yes" << endl;
    exit(0);
}

void ng(){
    cout <<  "No" << endl;
    exit(0);
}

bool check(int g){
    int V = group[g].size();
    if(V == 0)return false;
    int E = 0;
    for(auto v:group[g]){
        for(auto p:edge[v]){
            if(p.second == -1)continue;
            int to = p.first;
            if(comp[to] == g)E++;
        }
    }
//    cout << g << " " << V << " " << E << endl;
    if(E+1 == V)return false;
    return true;
}

int main(){
    cin >> n >> m;
    for(int i = 0;i < m;i++){
        cin >> a >> b >> c;
        edge[a].push_back(P(b,c));
        rev[b].push_back(a);
        if(c==1){
            edge[b].push_back(P(a,-1));
            rev[a].push_back(b);
        }
    }

    scc();

    for(int i = 1;i <= n;i++){
        if(check(i))ok();
    }
    ng();
    return 0;
}
0