結果

問題 No.1023 Cyclic Tour
ユーザー cn_449cn_449
提出日時 2020-04-11 23:19:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,650 bytes
コンパイル時間 1,857 ms
コンパイル使用メモリ 114,720 KB
実行使用メモリ 14,332 KB
最終ジャッジ日時 2023-10-19 20:18:53
合計ジャッジ時間 7,989 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,424 KB
testcase_01 AC 4 ms
5,424 KB
testcase_02 AC 4 ms
5,424 KB
testcase_03 AC 4 ms
5,424 KB
testcase_04 AC 30 ms
7,176 KB
testcase_05 AC 31 ms
7,176 KB
testcase_06 AC 33 ms
7,176 KB
testcase_07 AC 31 ms
7,176 KB
testcase_08 AC 44 ms
10,464 KB
testcase_09 AC 47 ms
10,672 KB
testcase_10 AC 47 ms
10,688 KB
testcase_11 AC 51 ms
11,080 KB
testcase_12 AC 53 ms
11,848 KB
testcase_13 AC 49 ms
11,488 KB
testcase_14 AC 49 ms
11,472 KB
testcase_15 AC 51 ms
11,496 KB
testcase_16 AC 83 ms
14,052 KB
testcase_17 AC 85 ms
14,332 KB
testcase_18 AC 85 ms
14,308 KB
testcase_19 AC 81 ms
14,060 KB
testcase_20 AC 65 ms
10,752 KB
testcase_21 AC 66 ms
10,808 KB
testcase_22 AC 77 ms
11,584 KB
testcase_23 AC 80 ms
12,372 KB
testcase_24 AC 89 ms
13,540 KB
testcase_25 WA -
testcase_26 AC 64 ms
10,728 KB
testcase_27 WA -
testcase_28 AC 88 ms
13,412 KB
testcase_29 AC 86 ms
13,940 KB
testcase_30 AC 91 ms
13,276 KB
testcase_31 AC 83 ms
12,732 KB
testcase_32 AC 83 ms
13,036 KB
testcase_33 AC 88 ms
13,012 KB
testcase_34 AC 25 ms
8,056 KB
testcase_35 AC 52 ms
9,208 KB
testcase_36 AC 68 ms
11,032 KB
testcase_37 AC 76 ms
12,372 KB
testcase_38 AC 82 ms
13,284 KB
testcase_39 AC 80 ms
12,324 KB
testcase_40 AC 83 ms
12,324 KB
testcase_41 AC 81 ms
12,324 KB
testcase_42 AC 62 ms
10,248 KB
testcase_43 AC 64 ms
11,780 KB
testcase_44 AC 34 ms
7,968 KB
testcase_45 AC 53 ms
12,672 KB
testcase_46 AC 47 ms
12,672 KB
testcase_47 AC 32 ms
7,968 KB
testcase_48 AC 65 ms
11,352 KB
testcase_49 AC 66 ms
11,352 KB
testcase_50 AC 62 ms
11,352 KB
testcase_51 AC 65 ms
9,768 KB
testcase_52 AC 60 ms
10,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#define int long long
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

class unionfind {
	vector<int> par;
	vector<int> sz;
public:
	unionfind(int n) {
		par = vector<int>(n);
		for (int i = 0; i < n; i++) par[i] = i;
		sz = vector<int>(n, 1);
	}
	int find(int x) {
		if (par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	int si(int x) { return sz[find(x)]; }
	bool same(int x, int y) { return find(x) == find(y); }
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return;
		if (sz[x] < sz[y]) {
			par[x] = y;
			sz[y] += sz[x];
		}
		else {
			par[y] = x;
			sz[x] += sz[y];
		}
	}
};

vector<vector<int>> graph(100010, vector<int>());
vector<bool> vis(100010);
bool ans = false;

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n, m;
	cin >> n >> m;
	unionfind uni(n);
	vector<P> edge;
	rep(i, m) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		int c;
		cin >> c;
		if (c == 1){
		    if(uni.same(u, v)){
		        cout << "Yes" << endl;
		        return 0;
		    }
		    uni.unite(u, v);
		}
		else edge.pb(P(u, v));
	}
	vector<int> deg(n);
	for (P p : edge) {
		if (uni.find(p.first) == uni.find(p.second)) {
			cout << "Yes" << endl;
			return 0;
		}
		graph[uni.find(p.first)].pb(uni.find(p.second));
		deg[uni.find(p.second)]++;
	}
	queue<int> que;
	rep(i, n) {
		int r = uni.find(i);
		if (!vis[r] && deg[r] == 0) {
			vis[r] = true;
			que.push(r);
		}
	}
	while (!que.empty()) {
		int v = que.front(); que.pop(); vis[v] = true;
		for (int i : graph[v]) {
			vis[i] = true;
			/*if (deg[i] == 0) {
				ans = true;
				break;
			}*/
			//else {
				deg[i]--;
				if (deg[i] == 0) que.push(i);
			//}
		}
	}
	rep(i, n) {
		if (!vis[uni.find(i)]) ans = true;
	}
	cout << (ans ? "Yes" : "No") << endl;
}
0