結果

問題 No.1023 Cyclic Tour
ユーザー chocoruskchocorusk
提出日時 2020-03-07 17:06:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,411 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 76,188 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-04-23 19:05:30
合計ジャッジ時間 9,798 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 4 ms
6,016 KB
testcase_02 WA -
testcase_03 AC 4 ms
6,016 KB
testcase_04 AC 77 ms
7,552 KB
testcase_05 AC 77 ms
7,552 KB
testcase_06 AC 87 ms
7,808 KB
testcase_07 AC 82 ms
7,552 KB
testcase_08 AC 89 ms
9,216 KB
testcase_09 AC 88 ms
9,088 KB
testcase_10 AC 84 ms
9,088 KB
testcase_11 AC 97 ms
9,216 KB
testcase_12 AC 94 ms
9,728 KB
testcase_13 AC 88 ms
9,472 KB
testcase_14 AC 86 ms
9,472 KB
testcase_15 AC 93 ms
9,728 KB
testcase_16 AC 172 ms
12,416 KB
testcase_17 AC 175 ms
12,416 KB
testcase_18 AC 182 ms
12,544 KB
testcase_19 AC 176 ms
12,288 KB
testcase_20 AC 153 ms
9,472 KB
testcase_21 AC 154 ms
9,600 KB
testcase_22 AC 158 ms
10,112 KB
testcase_23 AC 166 ms
10,240 KB
testcase_24 AC 180 ms
10,880 KB
testcase_25 AC 153 ms
9,600 KB
testcase_26 AC 156 ms
9,472 KB
testcase_27 AC 150 ms
9,216 KB
testcase_28 AC 172 ms
10,880 KB
testcase_29 AC 166 ms
11,008 KB
testcase_30 AC 176 ms
10,752 KB
testcase_31 AC 162 ms
10,624 KB
testcase_32 AC 166 ms
12,160 KB
testcase_33 AC 172 ms
11,136 KB
testcase_34 AC 142 ms
8,448 KB
testcase_35 AC 148 ms
8,576 KB
testcase_36 AC 157 ms
9,728 KB
testcase_37 AC 160 ms
11,136 KB
testcase_38 AC 163 ms
11,392 KB
testcase_39 AC 165 ms
10,112 KB
testcase_40 AC 166 ms
10,496 KB
testcase_41 AC 165 ms
10,240 KB
testcase_42 WA -
testcase_43 WA -
testcase_44 AC 78 ms
7,680 KB
testcase_45 AC 102 ms
15,488 KB
testcase_46 AC 100 ms
15,488 KB
testcase_47 AC 79 ms
7,808 KB
testcase_48 AC 158 ms
9,524 KB
testcase_49 AC 156 ms
9,644 KB
testcase_50 AC 152 ms
9,520 KB
testcase_51 WA -
testcase_52 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
struct unionfind{
    vector<int> par, sz;
    unionfind() {}
    unionfind(int n):par(n), sz(n, 1){
        for(int i=0; i<n; i++) par[i]=i;
    }
    int find(int x){
        if(par[x]==x) return x;
        return par[x]=find(par[x]);
    }
    void unite(int x, int y){
        x=find(x); y=find(y);
        if(x==y) return;
        if(sz[x]>sz[y]) swap(x, y);
        par[x]=y;
        sz[y]+=sz[x];
    }
    bool same(int x, int y){
        return find(x)==find(y);
    }
    int size(int x){
        return sz[find(x)];
    }
};
int n, m;
int a[200020], b[200020], c[200020];
vector<int> g[100010];
bool used[100010];
bool finish[100010];
bool ok;
void dfs(int x){
	used[x]=1;
	for(auto y:g[x]){
		if(used[y] && !finish[y]) ok=1;
		if(!used[y]) dfs(y);
	}
	finish[x]=1;
}
int main()
{
    cin>>n>>m;
	for(int i=0; i<m; i++){
		cin>>a[i]>>b[i]>>c[i];
		a[i]--; b[i]--;
	}
	unionfind uf(n);
	for(int i=0; i<m; i++){
		if(c[i]==1){
			if(uf.same(a[i], b[i])){
				cout<<"Yes"<<endl;
				return 0;
			}
			uf.unite(a[i], b[i]);
		}
	}
	for(int i=0; i<m; i++){
		if(c[i]==2){
		    int x=uf.find(a[i]), y=uf.find(b[i]);
		    if(x!=y) g[x].push_back(y);
		}
	}
	for(int i=0; i<n; i++){
		if(used[i]) continue;
		dfs(i);
		if(ok){
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;
    return 0;
}
0