結果

問題 No.1254 補強への架け橋
ユーザー chocopuuchocopuu
提出日時 2020-10-09 22:44:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 3,258 bytes
コンパイル時間 2,501 ms
コンパイル使用メモリ 217,308 KB
最終ジャッジ日時 2025-01-15 05:23:56
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 123
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
//#include "atcoder/all"
//using namespace atcoder;
#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>istream&operator >>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
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;

struct Edge {
    int to;
};
using Graph = vector<vector<Edge>>;
using P = pair<long, long>;
/* Lowlink: グラフの関節点・橋を列挙する構造体
    作成: O(E+V)
    関節点の集合: vector<int> aps
    橋の集合: vector<P> bridges
*/
struct LowLink {
    const Graph &G;
    vector<int> used, ord, low;
    vector<int> aps;  // articulation points
    vector<P> bridges;
    LowLink(const Graph &G_) : G(G_) {
        used.assign(G.size(), 0);
        ord.assign(G.size(), 0);
        low.assign(G.size(), 0);
        int k = 0;
        for (int i = 0; i < (int)G.size(); i++) {
            if (!used[i]) k = dfs(i, k, -1);
        }
        sort(aps.begin(), aps.end()); // 必要ならソートする
        sort(bridges.begin(), bridges.end()); // 必要ならソートする
    }
    int dfs(int id, int k, int par) { // id:探索中の頂点, k:dfsで何番目に探索するか, par:idの親
        used[id] = true;
        ord[id] = k++;
        low[id] = ord[id];
        bool is_aps = false;
        int count = 0; // 子の数
        for (auto &e : G[id]) {
            if (!used[e.to]) {
                count++;
                k = dfs(e.to, k, id);
                low[id] = min(low[id], low[e.to]);
                if (par != -1 && ord[id] <= low[e.to]) is_aps = true; 
                if (ord[id] < low[e.to]) bridges.emplace_back(min(id, e.to), max(id, e.to)); // 条件を満たすので橋  
            } else if (e.to != par) { // eが後退辺の時
                low[id] = min(low[id], ord[e.to]);
            }
        }
        if (par == -1 && count >= 2) is_aps = true; 
        if (is_aps) aps.push_back(id);
        return k;
    }
};

signed main(){
	int N;
	cin >> N;
	Graph g(N);
	vector<int>a(N), b(N);
	REP(i, N) {
		cin >> a[i] >> b[i];
		--a[i]; --b[i];
		Edge A, B;
		A.to = a[i];
		B.to = b[i];
		g[a[i]].emplace_back(B);
		g[b[i]].emplace_back(A);
	}
	auto ll = LowLink(g);
	vector<int>ans;
	REP(i, N) {
		if(ll.ord[b[i]] < ll.low[a[i]]) ans.emplace_back(i);
		if(ll.ord[a[i]] < ll.low[b[i]]) ans.emplace_back(i);
	}
	sort(ans.begin(), ans.end());
	ans.erase(unique(ans.begin(), ans.end()), ans.end());
	//out(ans.size());
	set<int>st;
	REP(i, N) st.insert(i);
	REP(i, ans.size()) st.erase(ans[i]);
	ans.clear();
	for(auto e: st) ans.emplace_back(e + 1);
	out(ans.size());
	REP(i,ans.size())cout << ans[i] << " \n"[i+1 == ans.size()];
}
0