結果

問題 No.1023 Cyclic Tour
ユーザー 👑 jupirojupiro
提出日時 2020-04-11 23:12:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 2,344 bytes
コンパイル時間 1,604 ms
コンパイル使用メモリ 126,064 KB
実行使用メモリ 11,656 KB
最終ジャッジ日時 2023-10-19 20:00:03
合計ジャッジ時間 10,144 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 28 ms
4,928 KB
testcase_05 AC 29 ms
4,928 KB
testcase_06 AC 32 ms
4,928 KB
testcase_07 AC 30 ms
4,928 KB
testcase_08 AC 43 ms
9,620 KB
testcase_09 AC 41 ms
9,448 KB
testcase_10 AC 41 ms
9,460 KB
testcase_11 AC 44 ms
9,784 KB
testcase_12 AC 44 ms
10,472 KB
testcase_13 AC 41 ms
9,944 KB
testcase_14 AC 41 ms
9,944 KB
testcase_15 AC 43 ms
10,056 KB
testcase_16 AC 72 ms
11,580 KB
testcase_17 AC 72 ms
11,656 KB
testcase_18 AC 73 ms
11,264 KB
testcase_19 AC 68 ms
11,284 KB
testcase_20 AC 61 ms
9,960 KB
testcase_21 AC 62 ms
10,084 KB
testcase_22 AC 67 ms
10,488 KB
testcase_23 AC 71 ms
10,704 KB
testcase_24 AC 75 ms
11,540 KB
testcase_25 AC 61 ms
9,964 KB
testcase_26 AC 60 ms
9,948 KB
testcase_27 AC 58 ms
9,620 KB
testcase_28 AC 74 ms
11,288 KB
testcase_29 AC 73 ms
11,492 KB
testcase_30 AC 77 ms
11,392 KB
testcase_31 AC 70 ms
10,988 KB
testcase_32 AC 71 ms
11,012 KB
testcase_33 AC 74 ms
11,312 KB
testcase_34 AC 49 ms
5,984 KB
testcase_35 AC 53 ms
5,984 KB
testcase_36 AC 64 ms
10,208 KB
testcase_37 AC 68 ms
10,824 KB
testcase_38 AC 71 ms
11,184 KB
testcase_39 AC 71 ms
10,640 KB
testcase_40 AC 72 ms
10,736 KB
testcase_41 AC 71 ms
10,716 KB
testcase_42 AC 62 ms
10,072 KB
testcase_43 AC 70 ms
11,172 KB
testcase_44 AC 32 ms
8,240 KB
testcase_45 AC 48 ms
11,440 KB
testcase_46 AC 44 ms
10,736 KB
testcase_47 AC 32 ms
8,592 KB
testcase_48 AC 60 ms
10,144 KB
testcase_49 AC 60 ms
10,148 KB
testcase_50 AC 60 ms
10,144 KB
testcase_51 AC 60 ms
10,148 KB
testcase_52 AC 61 ms
10,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class UnionFind {
public:
	vector<int> Parent;
	UnionFind(int N) {
		Parent = vector<int>(N, -1);
	}
	int root(int A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	long long size(int A) {
		return -(long long)Parent[root(A)];
	}

	bool connect(int A, int B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};
int main()
{
	
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int n, m; scanf("%d %d", &n, &m);
	vector<int> a(m), b(m), c(m); for (int i = 0; i < m; i++) scanf("%d %d %d", &a[i], &b[i], &c[i]), a[i]--, b[i]--, c[i]--;
	UnionFind uni(n);
	for (int i = 0; i < m; i++) {
		if (c[i]) continue;
		if (uni.root(a[i]) == uni.root(b[i])) {
			puts("Yes");
			return 0;
		}
		uni.connect(a[i], b[i]);
	}
	vector<vector<int>> g(n);
	vector<int> deg(n);
	for (int i = 0; i < m; i++) {
		if (c[i]) {
			g[uni.root(a[i])].push_back(uni.root(b[i]));
			deg[uni.root(b[i])]++;
		}
	}
	queue<int> q;
	vector<int> topo;
	for (int i = 0; i < n; i++) {
		if (deg[i] == 0) {
			q.push(i);
			topo.push_back(i);
		}
	}
	while (!q.empty()) {
		int cur = q.front();
		q.pop();
		for (int next : g[cur]) {
			deg[next]--;
			if (deg[next] == 0) {
				q.push(next);
				topo.push_back(next);
			}
		}
	}
	if (topo.size() == n) puts("No");
	else puts("Yes");
	return 0;
}
0