結果

問題 No.1023 Cyclic Tour
ユーザー 👑 kmjpkmjp
提出日時 2020-04-10 23:24:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,435 bytes
コンパイル時間 3,149 ms
コンパイル使用メモリ 216,124 KB
実行使用メモリ 123,328 KB
最終ジャッジ日時 2023-10-14 05:50:56
合計ジャッジ時間 12,581 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
107,204 KB
testcase_01 AC 41 ms
107,204 KB
testcase_02 AC 41 ms
107,108 KB
testcase_03 AC 41 ms
107,244 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 89 ms
110,920 KB
testcase_09 AC 106 ms
116,296 KB
testcase_10 AC 105 ms
116,208 KB
testcase_11 AC 110 ms
116,732 KB
testcase_12 AC 116 ms
118,056 KB
testcase_13 AC 116 ms
117,352 KB
testcase_14 AC 110 ms
117,356 KB
testcase_15 AC 117 ms
117,580 KB
testcase_16 AC 176 ms
119,116 KB
testcase_17 AC 176 ms
119,284 KB
testcase_18 AC 165 ms
118,384 KB
testcase_19 AC 169 ms
118,400 KB
testcase_20 AC 107 ms
115,012 KB
testcase_21 AC 108 ms
115,140 KB
testcase_22 AC 130 ms
116,204 KB
testcase_23 AC 137 ms
116,648 KB
testcase_24 AC 159 ms
118,720 KB
testcase_25 AC 108 ms
114,944 KB
testcase_26 AC 106 ms
114,860 KB
testcase_27 AC 103 ms
114,616 KB
testcase_28 AC 152 ms
118,192 KB
testcase_29 AC 159 ms
118,984 KB
testcase_30 AC 155 ms
118,228 KB
testcase_31 AC 147 ms
117,724 KB
testcase_32 AC 164 ms
118,496 KB
testcase_33 AC 161 ms
118,228 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 118 ms
115,464 KB
testcase_37 AC 145 ms
117,348 KB
testcase_38 AC 159 ms
118,384 KB
testcase_39 AC 132 ms
116,644 KB
testcase_40 AC 139 ms
116,804 KB
testcase_41 AC 139 ms
116,892 KB
testcase_42 AC 95 ms
109,072 KB
testcase_43 AC 107 ms
111,392 KB
testcase_44 AC 78 ms
113,148 KB
testcase_45 AC 124 ms
123,328 KB
testcase_46 AC 139 ms
121,044 KB
testcase_47 AC 77 ms
113,404 KB
testcase_48 AC 106 ms
115,388 KB
testcase_49 AC 106 ms
115,236 KB
testcase_50 AC 105 ms
115,408 KB
testcase_51 AC 93 ms
109,320 KB
testcase_52 AC 93 ms
109,860 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]) _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