結果

問題 No.1023 Cyclic Tour
ユーザー leaf_1415leaf_1415
提出日時 2020-04-10 22:29:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 2,010 bytes
コンパイル時間 2,257 ms
コンパイル使用メモリ 79,080 KB
実行使用メモリ 34,408 KB
最終ジャッジ日時 2023-10-14 01:14:37
合計ジャッジ時間 15,838 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
17,468 KB
testcase_01 AC 6 ms
17,440 KB
testcase_02 AC 7 ms
17,444 KB
testcase_03 AC 6 ms
17,488 KB
testcase_04 AC 140 ms
29,452 KB
testcase_05 AC 171 ms
29,560 KB
testcase_06 AC 203 ms
31,880 KB
testcase_07 AC 187 ms
30,844 KB
testcase_08 AC 127 ms
27,120 KB
testcase_09 AC 118 ms
27,040 KB
testcase_10 AC 108 ms
26,904 KB
testcase_11 AC 138 ms
27,640 KB
testcase_12 AC 103 ms
26,592 KB
testcase_13 AC 93 ms
26,028 KB
testcase_14 AC 93 ms
26,160 KB
testcase_15 AC 97 ms
26,508 KB
testcase_16 AC 237 ms
33,896 KB
testcase_17 AC 242 ms
34,108 KB
testcase_18 AC 248 ms
34,408 KB
testcase_19 AC 239 ms
33,896 KB
testcase_20 AC 290 ms
33,972 KB
testcase_21 AC 304 ms
34,368 KB
testcase_22 AC 271 ms
33,160 KB
testcase_23 AC 258 ms
33,176 KB
testcase_24 AC 267 ms
32,524 KB
testcase_25 AC 274 ms
34,000 KB
testcase_26 AC 265 ms
33,676 KB
testcase_27 AC 247 ms
33,332 KB
testcase_28 AC 231 ms
32,288 KB
testcase_29 AC 203 ms
31,400 KB
testcase_30 AC 252 ms
32,752 KB
testcase_31 AC 239 ms
32,412 KB
testcase_32 AC 238 ms
33,560 KB
testcase_33 AC 267 ms
33,812 KB
testcase_34 AC 270 ms
33,028 KB
testcase_35 AC 287 ms
34,204 KB
testcase_36 AC 265 ms
33,752 KB
testcase_37 AC 242 ms
33,312 KB
testcase_38 AC 228 ms
31,616 KB
testcase_39 AC 254 ms
32,844 KB
testcase_40 AC 244 ms
32,700 KB
testcase_41 AC 236 ms
32,592 KB
testcase_42 AC 267 ms
34,328 KB
testcase_43 AC 219 ms
31,136 KB
testcase_44 AC 173 ms
29,368 KB
testcase_45 AC 119 ms
32,532 KB
testcase_46 AC 132 ms
34,252 KB
testcase_47 AC 151 ms
34,244 KB
testcase_48 AC 176 ms
32,044 KB
testcase_49 AC 185 ms
32,116 KB
testcase_50 AC 170 ms
32,136 KB
testcase_51 AC 153 ms
32,048 KB
testcase_52 AC 152 ms
32,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

using namespace std;
typedef pair<llint, llint> P;

llint n, m;
vector<llint> G[200005], revG[200005];
vector<llint> G2[200005];
vector<llint> topo;
bool used[200005];
int scc[200005];

void tpsort(int v)
{
	used[v] = true;
	for(int i = 0; i < G[v].size(); i++){
		if(!used[G[v][i]]) tpsort(G[v][i]);
	}
	topo.push_back(v);
}
void sccdfs(int v, int id)
{
	used[v] = true;
	scc[v] = id;
	for(int i = 0; i < revG[v].size(); i++){
		if(!used[revG[v][i]]) sccdfs(revG[v][i], id);
	}
}

bool dfs(int v, int p)
{
	used[v] = true;
	bool ret = false;
	for(int i = 0; i < G2[v].size(); i++){
		llint u = G2[v][i];
		if(u == p) continue;
		if(scc[v] != scc[u]) continue;
		if(used[u]){
			ret = true;
			continue;
		}
		ret |= dfs(u, v);
	}
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	llint u, v, t;
	for(int i = 1; i <= m; i++){
		cin >> u >> v >> t;
		G[u].push_back(v), revG[v].push_back(u);
		if(t == 1) G[v].push_back(u), revG[u].push_back(v);
		G2[u].push_back(v), G2[v].push_back(u);
	}
	
	for(int i = 1; i <= n; i++) if(!used[i]) tpsort(i);
	reverse(topo.begin(), topo.end());
	
	int id = 0;
	for(int i = 1; i <= n; i++) used[i] = false;
	for(int i = 0; i < topo.size(); i++) if(!used[topo[i]]) sccdfs(topo[i], id++);
	
	for(int i = 1; i <= n; i++) used[i] = false;
	for(int i = 1; i <= n; i++){
		if(used[i]) continue;
		if(dfs(i, -1)){
			cout << "Yes" << endl;
			return 0;
		}
	}
	cout << "No" << endl;
	
	return 0;
}
0