結果
問題 | No.1023 Cyclic Tour |
ユーザー | Y17 |
提出日時 | 2020-04-11 00:00:53 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 325 ms / 2,000 ms |
コード長 | 4,327 bytes |
コンパイル時間 | 2,197 ms |
コンパイル使用メモリ | 186,008 KB |
実行使用メモリ | 50,520 KB |
最終ジャッジ日時 | 2024-09-16 03:52:49 |
合計ジャッジ時間 | 16,773 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 143 ms
22,092 KB |
testcase_05 | AC | 146 ms
22,148 KB |
testcase_06 | AC | 164 ms
22,836 KB |
testcase_07 | AC | 155 ms
22,256 KB |
testcase_08 | AC | 141 ms
29,064 KB |
testcase_09 | AC | 184 ms
39,764 KB |
testcase_10 | AC | 179 ms
39,940 KB |
testcase_11 | AC | 192 ms
42,108 KB |
testcase_12 | AC | 161 ms
42,188 KB |
testcase_13 | AC | 152 ms
39,752 KB |
testcase_14 | AC | 148 ms
39,520 KB |
testcase_15 | AC | 159 ms
40,352 KB |
testcase_16 | AC | 276 ms
46,452 KB |
testcase_17 | AC | 282 ms
46,580 KB |
testcase_18 | AC | 284 ms
43,780 KB |
testcase_19 | AC | 267 ms
43,664 KB |
testcase_20 | AC | 306 ms
43,852 KB |
testcase_21 | AC | 325 ms
45,176 KB |
testcase_22 | AC | 303 ms
47,268 KB |
testcase_23 | AC | 309 ms
47,748 KB |
testcase_24 | AC | 311 ms
50,328 KB |
testcase_25 | AC | 309 ms
43,896 KB |
testcase_26 | AC | 299 ms
43,676 KB |
testcase_27 | AC | 300 ms
41,608 KB |
testcase_28 | AC | 311 ms
48,808 KB |
testcase_29 | AC | 271 ms
47,704 KB |
testcase_30 | AC | 304 ms
50,456 KB |
testcase_31 | AC | 293 ms
49,284 KB |
testcase_32 | AC | 284 ms
47,980 KB |
testcase_33 | AC | 304 ms
50,520 KB |
testcase_34 | AC | 203 ms
23,088 KB |
testcase_35 | AC | 227 ms
25,260 KB |
testcase_36 | AC | 307 ms
45,132 KB |
testcase_37 | AC | 289 ms
47,548 KB |
testcase_38 | AC | 262 ms
46,592 KB |
testcase_39 | AC | 301 ms
46,716 KB |
testcase_40 | AC | 299 ms
47,200 KB |
testcase_41 | AC | 294 ms
47,172 KB |
testcase_42 | AC | 306 ms
40,152 KB |
testcase_43 | AC | 191 ms
30,468 KB |
testcase_44 | AC | 245 ms
37,944 KB |
testcase_45 | AC | 186 ms
48,516 KB |
testcase_46 | AC | 185 ms
44,820 KB |
testcase_47 | AC | 260 ms
48,192 KB |
testcase_48 | AC | 246 ms
45,588 KB |
testcase_49 | AC | 252 ms
45,716 KB |
testcase_50 | AC | 245 ms
45,944 KB |
testcase_51 | AC | 245 ms
41,620 KB |
testcase_52 | AC | 246 ms
42,216 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } using UnWeightedGraph = vector< vector< int > >; 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); } } } }; template< typename G > struct LowLink { const G &g; vector< int > used, ord, low; vector< int > articulation; vector< pair< int, int > > bridge; LowLink(const G &g) : g(g) {} int dfs(int idx, int k, int par) { used[idx] = true; ord[idx] = k++; low[idx] = ord[idx]; bool is_articulation = false; int cnt = 0; for(auto &to : g[idx]) { if(!used[to]) { ++cnt; k = dfs(to, k, idx); low[idx] = min(low[idx], low[to]); is_articulation |= ~par && low[to] >= ord[idx]; if(ord[idx] < low[to]) bridge.emplace_back(minmax(idx, (int) to)); } else if(to != par) { low[idx] = min(low[idx], ord[to]); } } is_articulation |= par == -1 && cnt > 1; if(is_articulation) articulation.push_back(idx); return k; } virtual void build() { used.assign(g.size(), 0); ord.assign(g.size(), 0); low.assign(g.size(), 0); int k = 0; for(int i = 0; i < g.size(); i++) { if(!used[i]) k = dfs(i, k, -1); } } }; template< typename G > struct TwoEdgeConnectedComponents : LowLink< G > { using LL = LowLink< G >; vector< int > comp; TwoEdgeConnectedComponents(const G &g) : LL(g) {} int operator[](const int &k) { return comp[k]; } void dfs(int idx, int par, int &k) { if(~par && this->ord[par] >= this->low[idx]) comp[idx] = comp[par]; else comp[idx] = k++; for(auto &to : this->g[idx]) { if(comp[to] == -1) dfs(to, idx, k); } } void build(UnWeightedGraph &t) { LL::build(); comp.assign(this->g.size(), -1); int k = 0; for(int i = 0; i < comp.size(); i++) { if(comp[i] == -1) dfs(i, -1, k); } t.resize(k); for(auto &e : this->bridge) { int x = comp[e.first], y = comp[e.second]; t[x].push_back(y); t[y].push_back(x); } } }; int a[200010]; int b[200010]; int c[200010]; signed main(){ int n, m; cin >> n >> m; UnWeightedGraph graph(n); for(int i = 0;i < m;i++){ cin >> a[i] >> b[i] >> c[i]; a[i]--; b[i]--; if(c[i] == 1){ graph[a[i]].push_back(b[i]); graph[b[i]].push_back(a[i]); } } TwoEdgeConnectedComponents<UnWeightedGraph> x(graph); UnWeightedGraph tmp, tmp2, tmp3; x.build(tmp); vector<int> cnt(n, 0); for(int i = 0;i < n;i++){ cnt[x[i]]++; if(cnt[x[i]] > 1){ cout << "Yes" << endl; return 0; } } StronglyConnectedComponents<UnWeightedGraph> scc(graph); scc.build(tmp2); for(int i = 0;i < m;i++){ if(c[i] == 2){ if(scc[a[i]] == scc[b[i]]){ cout << "Yes" << endl; return 0; }else{ tmp2[scc[a[i]]].push_back(scc[b[i]]); } } } StronglyConnectedComponents<UnWeightedGraph> scc2(tmp2); scc2.build(tmp3); vector<int> cnt2(n, 0); for(int i = 0;i < tmp2.size();i++){ cnt2[scc2[i]]++; if(cnt2[scc2[i]] > 1){ cout << "Yes" << endl; return 0; } } cout << "No" << endl; return 0; }