結果
問題 | No.1254 補強への架け橋 |
ユーザー | ok |
提出日時 | 2020-10-09 23:03:07 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 302 ms / 2,000 ms |
コード長 | 1,963 bytes |
コンパイル時間 | 1,170 ms |
コンパイル使用メモリ | 113,152 KB |
実行使用メモリ | 34,560 KB |
最終ジャッジ日時 | 2024-07-20 13:53:11 |
合計ジャッジ時間 | 15,187 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 123 |
ソースコード
#include<iostream> #include<string> #include<iomanip> #include<cmath> #include<vector> #include<algorithm> #include<utility> #include<deque> #include<map> using namespace std; #define int long long #define endl "\n" constexpr long long INF = (long long)1e18; constexpr long long MOD = 1'000'000'007; struct fast_io { fast_io(){ std::cin.tie(nullptr); std::ios::sync_with_stdio(false); }; } fio; bool cycle_detection(const vector<vector<int>> &G, vector<bool> &used, vector<bool> &used2, deque<int> &C, int cur, int per){ used[cur] = true; used2[cur] = true; C.push_back(cur); for(int nex : G[cur]){ if(used[nex]){ if(used2[nex]) { if(per == nex) continue; while(C.front() != nex){ C.pop_front(); } return true; } else { continue; } } if(cycle_detection(G, used, used2, C, nex, cur)) return true; } C.pop_back(); used2[cur] = false; return false; } deque<int> cycle_detection(const vector<vector<int>> &G){ int N = G.size(); vector<bool> used(N), used2(N); deque<int> C; for(int i = 0; i < N; i++){ if(used[i] == true) continue; C.clear(); if(cycle_detection(G, used, used2, C, i, -1)) return C; } return {}; } signed main(){ cout<<fixed<<setprecision(10); int N; vector<vector<int>> G; deque<int> c; map<pair<int,int>, int> mp; cin>>N; G.resize(N); for(int i = 0; i < N; i++){ int a, b; cin>>a>>b; a--, b--; G[a].push_back(b); G[b].push_back(a); mp[{a,b}] = i + 1; mp[{b,a}] = i + 1; } c = cycle_detection(G); cout<<(int)c.size()<<endl; for(int i = 0; i < c.size(); i++){ if(i) cout<<" "; cout<<mp[{c[i], c[(i+1)%c.size()]}]; } cout<<endl; // for(int i = 0; i< c.size(); i++){ // cout<<" "<<c[i]; // } // cout<<endl; return 0; }