結果
| 問題 | 
                            No.1023 Cyclic Tour
                             | 
                    
| コンテスト | |
| ユーザー | 
                             chocorusk
                         | 
                    
| 提出日時 | 2020-03-07 17:06:15 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,411 bytes | 
| コンパイル時間 | 745 ms | 
| コンパイル使用メモリ | 75,716 KB | 
| 実行使用メモリ | 15,488 KB | 
| 最終ジャッジ日時 | 2024-10-15 19:54:12 | 
| 合計ジャッジ時間 | 9,507 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 WA * 1 | 
| other | AC * 45 WA * 4 | 
ソースコード
#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;
}
            
            
            
        
            
chocorusk