結果

問題 No.1023 Cyclic Tour
ユーザー kotatsugamekotatsugame
提出日時 2020-04-15 12:42:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 182 ms / 2,000 ms
コード長 1,281 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 11,744 KB
最終ジャッジ日時 2024-04-09 18:38:13
合計ジャッジ時間 11,182 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,812 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 3 ms
6,940 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 76 ms
7,056 KB
testcase_05 AC 77 ms
7,036 KB
testcase_06 AC 81 ms
7,068 KB
testcase_07 AC 79 ms
6,992 KB
testcase_08 AC 92 ms
9,396 KB
testcase_09 AC 92 ms
9,500 KB
testcase_10 AC 88 ms
9,504 KB
testcase_11 AC 97 ms
9,664 KB
testcase_12 AC 94 ms
10,448 KB
testcase_13 AC 88 ms
10,392 KB
testcase_14 AC 87 ms
10,140 KB
testcase_15 AC 92 ms
10,368 KB
testcase_16 AC 164 ms
11,744 KB
testcase_17 AC 166 ms
11,668 KB
testcase_18 AC 172 ms
11,416 KB
testcase_19 AC 165 ms
11,512 KB
testcase_20 AC 155 ms
8,900 KB
testcase_21 AC 157 ms
8,920 KB
testcase_22 AC 161 ms
9,656 KB
testcase_23 AC 169 ms
9,804 KB
testcase_24 AC 182 ms
10,976 KB
testcase_25 AC 154 ms
8,776 KB
testcase_26 AC 159 ms
9,008 KB
testcase_27 AC 151 ms
8,772 KB
testcase_28 AC 174 ms
10,828 KB
testcase_29 AC 173 ms
11,072 KB
testcase_30 AC 179 ms
10,720 KB
testcase_31 AC 165 ms
10,332 KB
testcase_32 AC 168 ms
10,788 KB
testcase_33 AC 176 ms
10,592 KB
testcase_34 AC 61 ms
7,628 KB
testcase_35 AC 136 ms
8,396 KB
testcase_36 AC 161 ms
9,160 KB
testcase_37 AC 161 ms
10,056 KB
testcase_38 AC 165 ms
10,784 KB
testcase_39 AC 170 ms
9,912 KB
testcase_40 AC 167 ms
10,036 KB
testcase_41 AC 168 ms
10,168 KB
testcase_42 AC 158 ms
8,776 KB
testcase_43 AC 164 ms
10,936 KB
testcase_44 AC 80 ms
7,424 KB
testcase_45 AC 96 ms
11,432 KB
testcase_46 AC 93 ms
11,484 KB
testcase_47 AC 80 ms
7,240 KB
testcase_48 AC 162 ms
8,748 KB
testcase_49 AC 162 ms
9,184 KB
testcase_50 AC 157 ms
8,800 KB
testcase_51 AC 156 ms
8,932 KB
testcase_52 AC 161 ms
8,924 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   37 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#include<vector>
struct UF{
	int n;
	vector<int>parent,rank;
	UF(int n_=0):n(n_),parent(n_),rank(n_,1)
	{
		for(int i=0;i<n_;i++)parent[i]=i;
	}
	int find(int a){return parent[a]!=a?parent[a]=find(parent[a]):a;}
	bool same(int a,int b){return find(a)==find(b);}
	bool unite(int a,int b)
	{
		a=find(a),b=find(b);
		if(a==b)return false;
		if(rank[a]<rank[b])
		{
			parent[a]=b;
			rank[b]+=rank[a];
		}
		else
		{
			parent[b]=a;
			rank[a]+=rank[b];
		}
		return true;
	}
	int size(int a){return rank[find(a)];}
};
int N,M;
vector<pair<int,int> >edge;
vector<int>G[1<<17];
int cnt[1<<17];
main()
{
	cin>>N>>M;
	UF uf(N);
	for(int i=0;i<M;i++)
	{
		int a,b,c;cin>>a>>b>>c;
		a--,b--;
		if(c==1)
		{
		    if(!uf.unite(a,b))
		    {
		        cout<<"Yes"<<endl;
		        return 0;
		    }
		}
		else edge.push_back(make_pair(a,b));
	}
	for(pair<int,int>p:edge)
	{
		int u=uf.find(p.first);
		int v=uf.find(p.second);
		G[u].push_back(v);
		cnt[v]++;
	}
	queue<int>P;
	int vc=0;
	for(int i=0;i<N;i++)if(uf.find(i)==i)
	{
		vc++;
		if(cnt[i]==0)P.push(i);
	}
	int pc=0;
	while(!P.empty())
	{
		pc++;
		int u=P.front();P.pop();
		for(int v:G[u])if(--cnt[v]==0)P.push(v);
	}
	cout<<(vc==pc?"No":"Yes")<<endl;
}
0