結果
問題 | No.1023 Cyclic Tour |
ユーザー | chocopuu |
提出日時 | 2020-04-11 16:40:42 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 524 ms / 2,000 ms |
コード長 | 3,304 bytes |
コンパイル時間 | 3,092 ms |
コンパイル使用メモリ | 208,600 KB |
最終ジャッジ日時 | 2025-01-09 17:24:46 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 49 |
ソースコード
#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"); }