結果

問題 No.1023 Cyclic Tour
ユーザー chocoruskchocorusk
提出日時 2020-02-28 14:47:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,371 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 76,088 KB
実行使用メモリ 15,488 KB
最終ジャッジ日時 2024-04-23 19:08:19
合計ジャッジ時間 8,846 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 4 ms
6,016 KB
testcase_02 AC 3 ms
5,888 KB
testcase_03 AC 3 ms
6,016 KB
testcase_04 AC 69 ms
7,680 KB
testcase_05 AC 70 ms
7,552 KB
testcase_06 AC 79 ms
7,936 KB
testcase_07 AC 72 ms
7,552 KB
testcase_08 AC 77 ms
9,216 KB
testcase_09 AC 79 ms
9,088 KB
testcase_10 AC 83 ms
9,088 KB
testcase_11 AC 89 ms
9,344 KB
testcase_12 AC 86 ms
9,856 KB
testcase_13 AC 82 ms
9,472 KB
testcase_14 AC 78 ms
9,472 KB
testcase_15 AC 85 ms
9,856 KB
testcase_16 AC 152 ms
12,672 KB
testcase_17 AC 152 ms
12,544 KB
testcase_18 AC 162 ms
12,544 KB
testcase_19 AC 146 ms
12,288 KB
testcase_20 AC 136 ms
9,600 KB
testcase_21 AC 150 ms
9,472 KB
testcase_22 AC 147 ms
10,112 KB
testcase_23 AC 155 ms
10,112 KB
testcase_24 AC 163 ms
10,880 KB
testcase_25 AC 137 ms
9,600 KB
testcase_26 AC 140 ms
9,472 KB
testcase_27 AC 148 ms
9,216 KB
testcase_28 AC 156 ms
10,752 KB
testcase_29 AC 141 ms
11,136 KB
testcase_30 AC 156 ms
11,008 KB
testcase_31 AC 139 ms
10,752 KB
testcase_32 AC 145 ms
12,160 KB
testcase_33 AC 157 ms
11,264 KB
testcase_34 AC 125 ms
8,704 KB
testcase_35 AC 134 ms
8,576 KB
testcase_36 AC 136 ms
9,600 KB
testcase_37 AC 142 ms
11,136 KB
testcase_38 AC 142 ms
11,264 KB
testcase_39 AC 150 ms
10,240 KB
testcase_40 AC 151 ms
10,240 KB
testcase_41 AC 145 ms
10,240 KB
testcase_42 AC 139 ms
9,600 KB
testcase_43 AC 141 ms
10,752 KB
testcase_44 AC 72 ms
7,680 KB
testcase_45 AC 89 ms
15,488 KB
testcase_46 AC 94 ms
15,488 KB
testcase_47 AC 76 ms
7,680 KB
testcase_48 AC 148 ms
9,392 KB
testcase_49 AC 143 ms
9,392 KB
testcase_50 AC 134 ms
9,516 KB
testcase_51 AC 138 ms
9,388 KB
testcase_52 AC 141 ms
9,516 KB
権限があれば一括ダウンロードができます

ソースコード

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) g[uf.find(a[i])].push_back(uf.find(b[i]));
	}
	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