結果
| 問題 |
No.1254 補強への架け橋
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-12-23 18:13:36 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 123 ms / 2,000 ms |
| コード長 | 3,360 bytes |
| コンパイル時間 | 1,540 ms |
| コンパイル使用メモリ | 141,712 KB |
| 最終ジャッジ日時 | 2025-01-17 06:28:13 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 123 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
template<typename G>
struct LowLink{
const G& g;
vector<int> visited, order, low;
vector<int> articulation; // 関節点
vector<pair<int, int>> bridge; // 橋 (order[from] < low[to])
int LinkSize = 0; // 連結成分の個数
LowLink(const G &g) : g(g) {}
int dfs(int from, int k, int par = -1){
visited[from] = true;
order[from] = k++;
low[from] = order[from];
bool is_articulation = false;
int cnt = 0;
for(auto &to : g[from]){
if(!visited[to]){
++cnt;
k = dfs(to, k, from);
low[from] = min(low[from], low[to]);
// 頂点 from がDFS木の根以外であるときの関節点となる条件
is_articulation |= ~par && low[to] >= order[from];
// (from, to) が条件を満たせば、橋に追加
if(order[from] < low[to]) bridge.emplace_back(minmax(from, (int)to));
}
else if(to != par){
low[from] = min(low[from], order[to]);
}
}
// 頂点 from がDFS木の根であるときの関節点となる条件
is_articulation |= par == -1 && cnt > 1;
if(is_articulation) articulation.push_back(from);
return k;
}
virtual void build(){
visited.assign(g.size(), 0);
order.assign(g.size(), 0);
low.assign(g.size(), 0);
int k = 0;
for(int i = 0; i < g.size(); ++i){
if(!visited[i]){
k = dfs(i, k);
++LinkSize;
}
}
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int> a(N), b(N);
vector<vector<int>> graph(N);
for(int i = 0; i < N; ++i){
cin >> a[i] >> b[i];
--a[i], --b[i];
graph[a[i]].emplace_back(b[i]);
graph[b[i]].emplace_back(a[i]);
}
LowLink<vector<vector<int>>> g(graph);
g.build();
set<P> bridge;
for(auto [u, v] : g.bridge){
bridge.emplace(minmax(u, v));
}
vector<int> ans;
for(int i = 0; i < N; ++i){
if(bridge.find(minmax(a[i], b[i])) != bridge.end()) continue;
ans.emplace_back(i + 1);
}
int n = ans.size();
cout << n << '\n';
for(int i = 0; i < n; ++i){
cout << ans[i];
cout << ((i == n - 1) ? '\n' : ' ');
}
return 0;
}