結果

問題 No.1023 Cyclic Tour
ユーザー 👑 kmjpkmjp
提出日時 2020-04-10 23:27:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 2,440 bytes
コンパイル時間 2,634 ms
コンパイル使用メモリ 216,284 KB
実行使用メモリ 123,332 KB
最終ジャッジ日時 2023-10-14 06:42:10
合計ジャッジ時間 12,449 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
107,168 KB
testcase_01 AC 45 ms
107,272 KB
testcase_02 AC 45 ms
107,204 KB
testcase_03 AC 43 ms
107,200 KB
testcase_04 AC 72 ms
107,484 KB
testcase_05 AC 73 ms
107,176 KB
testcase_06 AC 75 ms
107,372 KB
testcase_07 AC 74 ms
107,272 KB
testcase_08 AC 97 ms
110,992 KB
testcase_09 AC 118 ms
116,176 KB
testcase_10 AC 124 ms
116,232 KB
testcase_11 AC 123 ms
117,016 KB
testcase_12 AC 133 ms
118,024 KB
testcase_13 AC 128 ms
117,252 KB
testcase_14 AC 128 ms
117,236 KB
testcase_15 AC 127 ms
117,500 KB
testcase_16 AC 206 ms
119,040 KB
testcase_17 AC 209 ms
119,240 KB
testcase_18 AC 208 ms
118,384 KB
testcase_19 AC 200 ms
118,380 KB
testcase_20 AC 112 ms
114,964 KB
testcase_21 AC 110 ms
115,228 KB
testcase_22 AC 142 ms
116,192 KB
testcase_23 AC 153 ms
116,652 KB
testcase_24 AC 183 ms
118,652 KB
testcase_25 AC 112 ms
114,852 KB
testcase_26 AC 113 ms
114,960 KB
testcase_27 AC 109 ms
114,600 KB
testcase_28 AC 179 ms
118,208 KB
testcase_29 AC 183 ms
118,968 KB
testcase_30 AC 176 ms
118,244 KB
testcase_31 AC 172 ms
117,652 KB
testcase_32 AC 181 ms
118,444 KB
testcase_33 AC 180 ms
118,212 KB
testcase_34 AC 64 ms
108,376 KB
testcase_35 AC 90 ms
109,172 KB
testcase_36 AC 123 ms
115,476 KB
testcase_37 AC 154 ms
117,412 KB
testcase_38 AC 181 ms
118,468 KB
testcase_39 AC 147 ms
116,728 KB
testcase_40 AC 154 ms
116,896 KB
testcase_41 AC 158 ms
116,800 KB
testcase_42 AC 101 ms
109,124 KB
testcase_43 AC 118 ms
111,364 KB
testcase_44 AC 83 ms
113,088 KB
testcase_45 AC 139 ms
123,332 KB
testcase_46 AC 154 ms
121,028 KB
testcase_47 AC 79 ms
113,428 KB
testcase_48 AC 108 ms
115,448 KB
testcase_49 AC 109 ms
115,348 KB
testcase_50 AC 109 ms
115,388 KB
testcase_51 AC 98 ms
109,440 KB
testcase_52 AC 97 ms
109,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

class SCC {
public:
	static const int MV = 2025000;
	vector<vector<int> > SC; int NV,GR[MV];
private:
	vector<int> E[MV], RE[MV], NUM; int vis[MV];
public:
	void init(int NV) { this->NV=NV; for(int i=0;i<NV;i++) { E[i].clear(); RE[i].clear();}}
	void add_edge(int x,int y) { E[x].push_back(y); RE[y].push_back(x); }
	void dfs(int cu) { vis[cu]=1; for(int i=0;i<E[cu].size();i++) if(!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
	void revdfs(int cu, int ind) { int i; vis[cu]=1; GR[cu]=ind; SC[ind].push_back(cu);
		FOR(i,RE[cu].size()) if(!vis[RE[cu][i]]) revdfs(RE[cu][i],ind);}
	void scc() {
		int c=0,i; SC.clear(); SC.resize(NV); NUM.clear();
		assert(NV);
		FOR(i,NV) vis[i]=0; FOR(i,NV) if(!vis[i]) dfs(i); FOR(i,NV) vis[i]=0;
		for(int i=NUM.size()-1;i>=0;i--) if(!vis[NUM[i]]){
			SC[c].clear(); revdfs(NUM[i],c); sort(SC[c].begin(),SC[c].end()); c++;
		}
		SC.resize(c);
	}
};

template<int um> class UF {
	public:
	vector<int> par,rank;
	UF() {rank=vector<int>(um,0); for(int i=0;i<um;i++) par.push_back(i);}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<500000> uf;

SCC scc;

int N,M;
vector<pair<int,int>> BE;
vector<int> E[101010];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	scc.init(N);
	FOR(i,M) {
		cin>>x>>y>>j;
		if(j==1) {
			if(uf[x-1]==uf[y-1]) return _P("Yes\n");
			uf(x-1,y-1);
		}
		else {
			BE.push_back({x-1,y-1});
		}
	}
	FORR(e,BE) {
		if(uf[e.first]==uf[e.second]) return _P("Yes\n");
		scc.add_edge(uf[e.first],uf[e.second]);
	}
	
	scc.scc();
	if(scc.SC.size()!=N) return _P("Yes\n");
	_P("No\n");
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0