結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using graph = vector<vector<int>>;

vector<int> dfs(int v, int p, vector<bool> &visited, const graph &to, vector<int> &route) {
    visited.at(v) = true;
    for (int next : to.at(v)) {
        if (next == p) continue;
        route.push_back(v);
        if (visited.at(next)) {
            route.push_back(next);
            return route;
        }
        auto res = dfs(next, v, visited, to, route);
        if (not res.empty()) return res;
        route.pop_back();
    }
    return {};
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) {
        cin >> a.at(i) >> b.at(i);
        a.at(i)--;
        b.at(i)--;
    }
    graph to(n);
    for (int i = 0; i < n; i++) {
        to.at(a.at(i)).push_back(b.at(i));
        to.at(b.at(i)).push_back(a.at(i));
    }
    vector<int> route;
    vector<bool> visited(n);
    auto res = dfs(0, -1, visited, to, route);
    int s = res.back();
    res.pop_back();
    reverse(res.begin(), res.end());
    vector<bool> inloop(n);
    for (int v : res) {
        inloop.at(v) = true;
        if (v == s) break;
    }
    cout << count(inloop.begin(), inloop.end(), true) << endl;
    for (int i = 0; i < n; i++) {
        if (inloop.at(a.at(i)) and inloop.at(b.at(i))) {
            cout << i + 1 << " ";
        }
    }
    cout << endl;
}
0