結果

問題 No.1023 Cyclic Tour
ユーザー momoharamomohara
提出日時 2020-04-10 22:47:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 199 ms / 2,000 ms
コード長 3,760 bytes
コンパイル時間 2,132 ms
コンパイル使用メモリ 183,316 KB
実行使用メモリ 36,876 KB
最終ジャッジ日時 2023-10-14 03:28:48
合計ジャッジ時間 11,920 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 54 ms
9,600 KB
testcase_05 AC 53 ms
9,700 KB
testcase_06 AC 60 ms
10,372 KB
testcase_07 AC 54 ms
9,880 KB
testcase_08 AC 45 ms
10,228 KB
testcase_09 AC 94 ms
25,152 KB
testcase_10 AC 93 ms
24,960 KB
testcase_11 AC 101 ms
26,272 KB
testcase_12 AC 90 ms
24,868 KB
testcase_13 AC 83 ms
23,744 KB
testcase_14 AC 84 ms
23,664 KB
testcase_15 AC 87 ms
24,140 KB
testcase_16 AC 157 ms
33,144 KB
testcase_17 AC 170 ms
33,032 KB
testcase_18 AC 164 ms
32,296 KB
testcase_19 AC 157 ms
31,672 KB
testcase_20 AC 196 ms
36,016 KB
testcase_21 AC 195 ms
36,876 KB
testcase_22 AC 176 ms
34,996 KB
testcase_23 AC 182 ms
35,500 KB
testcase_24 AC 162 ms
34,696 KB
testcase_25 AC 189 ms
36,104 KB
testcase_26 AC 176 ms
36,208 KB
testcase_27 AC 176 ms
35,196 KB
testcase_28 AC 167 ms
33,504 KB
testcase_29 AC 155 ms
32,220 KB
testcase_30 AC 172 ms
34,724 KB
testcase_31 AC 168 ms
34,320 KB
testcase_32 AC 177 ms
34,324 KB
testcase_33 AC 190 ms
35,592 KB
testcase_34 AC 91 ms
12,552 KB
testcase_35 AC 99 ms
13,288 KB
testcase_36 AC 182 ms
35,736 KB
testcase_37 AC 168 ms
36,212 KB
testcase_38 AC 153 ms
32,648 KB
testcase_39 AC 178 ms
35,268 KB
testcase_40 AC 169 ms
34,988 KB
testcase_41 AC 168 ms
34,448 KB
testcase_42 AC 199 ms
36,716 KB
testcase_43 AC 86 ms
13,208 KB
testcase_44 AC 116 ms
29,456 KB
testcase_45 AC 103 ms
31,436 KB
testcase_46 AC 112 ms
31,364 KB
testcase_47 AC 132 ms
29,624 KB
testcase_48 AC 141 ms
35,072 KB
testcase_49 AC 153 ms
35,976 KB
testcase_50 AC 145 ms
35,888 KB
testcase_51 AC 143 ms
34,712 KB
testcase_52 AC 143 ms
34,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

#define rep(i,m,n) for(int (i)=(int)(m);(i)<(int)(n);(i)++)
#define rep2(i,m,n) for(int (i)=(int)(n)-1;(i)>=(int)(m);(i)--)
#define REP(i,n) rep(i,0,n)
#define REP2(i,n) rep2(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;

typedef vector<ll> Array;
typedef vector<Array> Matrix;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

//constexpr long long MOD = (ll) 1e9+7;
constexpr long long MOD = 998244353;

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;
}

struct Edge {
	int to, cap, rev;
	Edge(int _to, int _cap, int _rev) {
		to = _to; cap = _cap; rev = _rev;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph& G, int from, int to, int cap, bool revFlag, int revCap) {
	G[from].push_back(Edge(to, cap, G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, G[from].size() - 1));
}


//強連結成分分解
class SCC{
	Graph g,rg;
	vector<int> cmp,used,vs;
	int size;
public:
	SCC(Graph _g):g(_g),rg(g.size()),cmp(g.size(),-1),used(g.size(),false){
		REP(i,g.size()){
			for(auto e:g[i]){
				add_edge(rg,e.to,i,1,false,0);//逆辺のグラフ
			}
		}

		REP(i,g.size()){
			if(!used[i]) dfs(i);
		}
		int k=0;
		
		REP(i,vs.size()){
			ll v=vs[vs.size()-1-i];
			if(used[v]){
				rdfs(v,k);
				k++;
			}
		}
		size=k;
	}

	void dfs(int v){
		//帰りがけ順に番号を振る
		used[v]=true;
		for(auto e:g[v]){
			if(!used[e.to]) dfs(e.to);
		}
		vs.push_back(v);
	}

	void rdfs(int v, int k){
		//通ることができた頂点に番号を振る
		used[v]=false;
		cmp[v]=k;
		for(auto e:rg[v]){
			if(used[e.to]) rdfs(e.to,k);
		}
	}

	int getSize(){
		return size;
	}

	int group(int v){
		return cmp[v];
	}

  Graph rebuild(){
		//縮約したグラフを返す
		Graph ret(size);
		REP(i,g.size()){
			int u=cmp[i];
			for(auto e:g[i]){
				int v=cmp[e.to];
				if(u==v) continue;
				cout<<u<<" "<<v<<e.cap<<en;
				add_edge(ret,u,v,e.cap,false,0);
			}
		}
		return ret;
	}
};


class UnionFind {
 vector<int> data;
 int num;
public:
 UnionFind(int size) : data(size, -1), num(size) { }
 bool unionSet(int x, int y) {
  x = root(x); y = root(y);
  if (x != y) {
   if (data[y] < data[x]) swap(x, y);
   data[x] += data[y]; data[y] = x;
   num--;
  }
  return x != y;
 }
 bool findSet(int x, int y) {
  return root(x) == root(y);
 }
 int root(int x) {
  return data[x] < 0 ? x : data[x] = root(data[x]);
 }
 int size(int x) {
  return -data[root(x)];
 }
 int numSet() {
  return num;
 }
};

void solve() {
    ll n,m;
    cin>>n>>m;
    Graph g(n);
    vec<P> ab;
    UnionFind uni(n);
    bool flag=false;
    REP(i,m){
        ll a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        if(uni.findSet(a,b)) flag=true;
        if(c==1){ 
            add_edge(g,a,b,1,true,1);
            uni.unionSet(a,b);
        }else{ 
            add_edge(g,a,b,1,false,1);
            ab.push_back({a,b});
        }
        
    }
    if(flag){
        cout<<"Yes"<<en;
        return;
    }
    SCC scc(g);
    
    REP(i,ab.size()){
        if(scc.group(ab[i].first)==scc.group(ab[i].second)){
            cout<<"Yes"<<en;
            return;
        }
    }
    cout<<"No"<<en;

}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	solve();
	//int t; cin >> t; REP(i, t) solve();

	return 0;
}
0