結果

問題 No.1023 Cyclic Tour
ユーザー chocopuuchocopuu
提出日時 2020-04-11 16:40:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 3,304 bytes
コンパイル時間 2,552 ms
コンパイル使用メモリ 218,528 KB
実行使用メモリ 35,224 KB
最終ジャッジ日時 2023-10-19 08:24:02
合計ジャッジ時間 14,539 ms
ジャッジサーバーID
(参考情報)
judge12 / 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 85 ms
7,060 KB
testcase_05 AC 85 ms
7,060 KB
testcase_06 AC 90 ms
7,588 KB
testcase_07 AC 86 ms
7,324 KB
testcase_08 AC 95 ms
11,020 KB
testcase_09 AC 138 ms
23,964 KB
testcase_10 AC 136 ms
23,988 KB
testcase_11 AC 151 ms
25,244 KB
testcase_12 AC 154 ms
27,400 KB
testcase_13 AC 145 ms
26,016 KB
testcase_14 AC 146 ms
25,860 KB
testcase_15 AC 153 ms
26,344 KB
testcase_16 AC 270 ms
31,200 KB
testcase_17 AC 272 ms
31,492 KB
testcase_18 AC 274 ms
29,792 KB
testcase_19 AC 264 ms
29,504 KB
testcase_20 AC 186 ms
25,036 KB
testcase_21 AC 188 ms
25,716 KB
testcase_22 AC 219 ms
27,388 KB
testcase_23 AC 232 ms
28,044 KB
testcase_24 AC 271 ms
31,996 KB
testcase_25 AC 185 ms
25,036 KB
testcase_26 AC 186 ms
25,140 KB
testcase_27 AC 178 ms
23,620 KB
testcase_28 AC 270 ms
30,968 KB
testcase_29 AC 268 ms
31,968 KB
testcase_30 AC 264 ms
31,200 KB
testcase_31 AC 244 ms
29,612 KB
testcase_32 AC 255 ms
30,440 KB
testcase_33 AC 262 ms
30,816 KB
testcase_34 AC 66 ms
9,172 KB
testcase_35 AC 147 ms
9,436 KB
testcase_36 AC 205 ms
25,896 KB
testcase_37 AC 228 ms
28,808 KB
testcase_38 AC 257 ms
30,612 KB
testcase_39 AC 237 ms
28,344 KB
testcase_40 AC 237 ms
28,676 KB
testcase_41 AC 236 ms
28,600 KB
testcase_42 AC 173 ms
12,604 KB
testcase_43 AC 158 ms
12,868 KB
testcase_44 AC 100 ms
18,744 KB
testcase_45 AC 193 ms
35,224 KB
testcase_46 AC 182 ms
29,952 KB
testcase_47 AC 99 ms
19,036 KB
testcase_48 AC 193 ms
25,832 KB
testcase_49 AC 192 ms
25,796 KB
testcase_50 AC 190 ms
25,460 KB
testcase_51 AC 169 ms
12,312 KB
testcase_52 AC 177 ms
13,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template<class T>inline void out(T t){cout << t << "\n";}
template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);}
template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;}
template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;}
constexpr int INF = 1e18;

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
	to = x;
	return *this;
  }

  operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename G >
struct StronglyConnectedComponents {
  const G &g;
  UnWeightedGraph gg, rg;
  vector< int > comp, order, used;

  StronglyConnectedComponents(G &g) : g(g), gg(g.size()), rg(g.size()), comp(g.size(), -1), used(g.size()) {
	for(int i = 0; i < g.size(); i++) {
	  for(auto e : g[i]) {
		gg[i].emplace_back((int) e);
		rg[(int) e].emplace_back(i);
	  }
	}
  }

  int operator[](int k) {
	return comp[k];
  }

  void dfs(int idx) {
	if(used[idx]) return;
	used[idx] = true;
	for(int to : gg[idx]) dfs(to);
	order.push_back(idx);
  }

  void rdfs(int idx, int cnt) {
	if(comp[idx] != -1) return;
	comp[idx] = cnt;
	for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(UnWeightedGraph &t) {
	for(int i = 0; i < gg.size(); i++) dfs(i);
	reverse(begin(order), end(order));
	int ptr = 0;
	for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

	t.resize(ptr);
	for(int i = 0; i < g.size(); i++) {
	  for(auto &to : g[i]) {
		int x = comp[i], y = comp[to];
		if(x == y) continue;
		t[x].push_back(y);
	  }
	}
  }
};

struct UF{
	vector<int>par,sz;
	void init(int n){
		par.resize(n);
		sz.resize(n);
		for(int i=0;i<n;i++){
			par[i]=i;
			sz[i]=1;
		}
	}
	int find(int x){
		return x==par[x]?x:par[x]=find(par[x]);
	}
	void unite(int x,int y){
		x=find(x);y=find(y);
		if(x==y)return;
		sz[x]+=sz[y];
		par[y]=x;
	}
	bool same(int x,int y){
		return find(x)==find(y);
	}
	int size(int x){
		return sz[find(x)];
	}
};


signed main(){
	int N,M;
	cin >> N >> M;
	vector<int>a(M),b(M),c(M);
	UF uf;
	uf.init(N);
	REP(i,M){
		cin >> a[i] >> b[i] >> c[i];
		--a[i];--b[i];
		if(c[i] == 2)continue;
		if(uf.same(a[i],b[i])){
			out("Yes");
			return 0;
		}else{
			uf.unite(a[i],b[i]);
		}
	}
	vector<vector<int>>g(N);
	map<pair<int,int>,int>mp;
	REP(i,M){
		if(c[i] == 1)continue;
		if(uf.find(a[i]) == uf.find(b[i])){
			out("Yes");
			return 0;
		}
		g[uf.find(a[i])].emplace_back(uf.find(b[i]));
	}
	StronglyConnectedComponents<vector<vector<int>>>scc(g);
	vector<vector<int>>buf;
	scc.build(buf);
	if(buf.size() < N)out("Yes");
	else out("No");
}
0