結果

問題 No.1023 Cyclic Tour
ユーザー cn_449cn_449
提出日時 2020-04-10 23:18:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,757 bytes
コンパイル時間 1,313 ms
コンパイル使用メモリ 113,044 KB
実行使用メモリ 15,176 KB
最終ジャッジ日時 2023-10-14 05:09:31
合計ジャッジ時間 8,238 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,344 KB
testcase_01 AC 4 ms
5,432 KB
testcase_02 AC 4 ms
5,352 KB
testcase_03 AC 3 ms
5,348 KB
testcase_04 AC 32 ms
7,604 KB
testcase_05 AC 32 ms
7,600 KB
testcase_06 AC 36 ms
7,740 KB
testcase_07 AC 34 ms
7,844 KB
testcase_08 AC 45 ms
10,284 KB
testcase_09 AC 51 ms
11,004 KB
testcase_10 AC 50 ms
11,020 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 53 ms
11,812 KB
testcase_15 WA -
testcase_16 AC 94 ms
14,852 KB
testcase_17 AC 92 ms
14,876 KB
testcase_18 AC 90 ms
14,804 KB
testcase_19 AC 90 ms
14,548 KB
testcase_20 AC 68 ms
11,452 KB
testcase_21 AC 70 ms
11,328 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 70 ms
11,468 KB
testcase_26 AC 71 ms
11,516 KB
testcase_27 AC 68 ms
11,096 KB
testcase_28 AC 99 ms
13,704 KB
testcase_29 AC 96 ms
14,508 KB
testcase_30 AC 98 ms
13,920 KB
testcase_31 AC 88 ms
13,348 KB
testcase_32 AC 89 ms
13,680 KB
testcase_33 AC 94 ms
13,900 KB
testcase_34 AC 74 ms
11,484 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 84 ms
13,256 KB
testcase_38 AC 88 ms
13,844 KB
testcase_39 WA -
testcase_40 AC 90 ms
13,280 KB
testcase_41 AC 86 ms
13,020 KB
testcase_42 AC 65 ms
9,992 KB
testcase_43 AC 67 ms
12,092 KB
testcase_44 AC 35 ms
8,628 KB
testcase_45 AC 59 ms
13,212 KB
testcase_46 AC 49 ms
13,360 KB
testcase_47 AC 34 ms
8,788 KB
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 66 ms
11,108 KB
testcase_51 AC 69 ms
9,512 KB
testcase_52 AC 63 ms
10,188 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) 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;
	vector<int> id(n);
	int idx = 0;
	rep(i, n) {
		int r = uni.find(i);
		if (!vis[r] && deg[r] == 0) {
			vis[r] = true;
			que.push(r);
			id[idx] = r;
			idx++;
		}
	}
	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);
			id[i] = idx;
			idx++;
			//}
		}
	}
	for (P p : edge) {
		int px = uni.find(p.first);
		int py = uni.find(p.second);
		if (id[px] > id[py]) ans = true;
	}
	rep(i, n) {
		if (!vis[uni.find(i)]) ans = true;
	}
	cout << (ans ? "Yes" : "No") << endl;
}
0