結果
問題 | No.1023 Cyclic Tour |
ユーザー | chocopuu |
提出日時 | 2020-04-11 16:40:42 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 228 ms / 2,000 ms |
コード長 | 3,304 bytes |
コンパイル時間 | 2,605 ms |
コンパイル使用メモリ | 218,600 KB |
実行使用メモリ | 34,956 KB |
最終ジャッジ日時 | 2024-09-19 04:38:54 |
合計ジャッジ時間 | 13,193 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 1 ms
6,944 KB |
testcase_04 | AC | 76 ms
7,040 KB |
testcase_05 | AC | 78 ms
6,940 KB |
testcase_06 | AC | 77 ms
7,296 KB |
testcase_07 | AC | 82 ms
7,168 KB |
testcase_08 | AC | 82 ms
10,752 KB |
testcase_09 | AC | 116 ms
23,756 KB |
testcase_10 | AC | 117 ms
23,780 KB |
testcase_11 | AC | 122 ms
25,028 KB |
testcase_12 | AC | 120 ms
27,412 KB |
testcase_13 | AC | 120 ms
25,916 KB |
testcase_14 | AC | 120 ms
25,640 KB |
testcase_15 | AC | 115 ms
26,180 KB |
testcase_16 | AC | 219 ms
31,028 KB |
testcase_17 | AC | 227 ms
31,200 KB |
testcase_18 | AC | 220 ms
29,660 KB |
testcase_19 | AC | 215 ms
29,356 KB |
testcase_20 | AC | 168 ms
24,840 KB |
testcase_21 | AC | 170 ms
25,392 KB |
testcase_22 | AC | 192 ms
27,172 KB |
testcase_23 | AC | 199 ms
27,956 KB |
testcase_24 | AC | 226 ms
31,792 KB |
testcase_25 | AC | 176 ms
24,972 KB |
testcase_26 | AC | 170 ms
25,068 KB |
testcase_27 | AC | 162 ms
23,564 KB |
testcase_28 | AC | 223 ms
30,984 KB |
testcase_29 | AC | 219 ms
31,848 KB |
testcase_30 | AC | 228 ms
31,156 KB |
testcase_31 | AC | 208 ms
29,516 KB |
testcase_32 | AC | 209 ms
30,376 KB |
testcase_33 | AC | 220 ms
30,844 KB |
testcase_34 | AC | 59 ms
9,216 KB |
testcase_35 | AC | 132 ms
9,344 KB |
testcase_36 | AC | 180 ms
25,768 KB |
testcase_37 | AC | 200 ms
28,732 KB |
testcase_38 | AC | 214 ms
30,516 KB |
testcase_39 | AC | 206 ms
28,120 KB |
testcase_40 | AC | 203 ms
28,584 KB |
testcase_41 | AC | 205 ms
28,504 KB |
testcase_42 | AC | 151 ms
12,288 KB |
testcase_43 | AC | 143 ms
12,800 KB |
testcase_44 | AC | 88 ms
18,708 KB |
testcase_45 | AC | 161 ms
34,956 KB |
testcase_46 | AC | 147 ms
29,868 KB |
testcase_47 | AC | 89 ms
19,120 KB |
testcase_48 | AC | 175 ms
25,612 KB |
testcase_49 | AC | 169 ms
25,736 KB |
testcase_50 | AC | 172 ms
25,376 KB |
testcase_51 | AC | 151 ms
12,160 KB |
testcase_52 | AC | 163 ms
13,056 KB |
ソースコード
#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"); }