結果

問題 No.1254 補強への架け橋
ユーザー kimiyukikimiyuki
提出日時 2020-10-09 23:12:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 290 ms / 2,000 ms
コード長 2,637 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 90,316 KB
実行使用メモリ 35,000 KB
最終ジャッジ日時 2023-09-27 19:37:59
合計ジャッジ時間 14,048 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,380 KB
testcase_38 AC 1 ms
4,380 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 3 ms
4,380 KB
testcase_44 AC 1 ms
4,380 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 2 ms
4,376 KB
testcase_52 AC 2 ms
4,376 KB
testcase_53 AC 2 ms
4,376 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,376 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 2 ms
4,380 KB
testcase_59 AC 1 ms
4,376 KB
testcase_60 AC 1 ms
4,376 KB
testcase_61 AC 2 ms
4,376 KB
testcase_62 AC 2 ms
4,376 KB
testcase_63 AC 11 ms
4,640 KB
testcase_64 AC 3 ms
4,380 KB
testcase_65 AC 8 ms
4,376 KB
testcase_66 AC 6 ms
4,376 KB
testcase_67 AC 3 ms
4,380 KB
testcase_68 AC 7 ms
4,376 KB
testcase_69 AC 9 ms
4,376 KB
testcase_70 AC 4 ms
4,376 KB
testcase_71 AC 3 ms
4,376 KB
testcase_72 AC 9 ms
4,376 KB
testcase_73 AC 4 ms
4,376 KB
testcase_74 AC 8 ms
4,376 KB
testcase_75 AC 6 ms
4,384 KB
testcase_76 AC 2 ms
4,380 KB
testcase_77 AC 5 ms
4,376 KB
testcase_78 AC 10 ms
4,476 KB
testcase_79 AC 11 ms
4,536 KB
testcase_80 AC 10 ms
4,392 KB
testcase_81 AC 10 ms
4,592 KB
testcase_82 AC 10 ms
4,468 KB
testcase_83 AC 211 ms
20,360 KB
testcase_84 AC 159 ms
19,900 KB
testcase_85 AC 84 ms
13,448 KB
testcase_86 AC 177 ms
17,428 KB
testcase_87 AC 157 ms
19,080 KB
testcase_88 AC 17 ms
5,456 KB
testcase_89 AC 207 ms
20,104 KB
testcase_90 AC 94 ms
14,060 KB
testcase_91 AC 69 ms
11,648 KB
testcase_92 AC 32 ms
7,464 KB
testcase_93 AC 143 ms
16,616 KB
testcase_94 AC 108 ms
15,292 KB
testcase_95 AC 115 ms
15,320 KB
testcase_96 AC 205 ms
19,504 KB
testcase_97 AC 54 ms
10,284 KB
testcase_98 AC 178 ms
19,564 KB
testcase_99 AC 101 ms
12,940 KB
testcase_100 AC 179 ms
20,592 KB
testcase_101 AC 28 ms
6,848 KB
testcase_102 AC 15 ms
5,088 KB
testcase_103 AC 34 ms
7,408 KB
testcase_104 AC 43 ms
8,960 KB
testcase_105 AC 122 ms
16,884 KB
testcase_106 AC 62 ms
11,136 KB
testcase_107 AC 203 ms
20,048 KB
testcase_108 AC 177 ms
20,112 KB
testcase_109 AC 160 ms
16,720 KB
testcase_110 AC 113 ms
15,380 KB
testcase_111 AC 135 ms
16,168 KB
testcase_112 AC 43 ms
8,776 KB
testcase_113 AC 103 ms
14,824 KB
testcase_114 AC 60 ms
10,924 KB
testcase_115 AC 19 ms
5,652 KB
testcase_116 AC 76 ms
12,164 KB
testcase_117 AC 46 ms
8,732 KB
testcase_118 AC 159 ms
19,260 KB
testcase_119 AC 90 ms
13,004 KB
testcase_120 AC 177 ms
18,836 KB
testcase_121 AC 37 ms
7,908 KB
testcase_122 AC 69 ms
11,888 KB
testcase_123 AC 1 ms
4,380 KB
testcase_124 AC 290 ms
35,000 KB
testcase_125 AC 263 ms
34,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "main.cpp"
#include <cstdio>
#include <map>
#include <utility>
#include <vector>
#line 2 "/home/user/Library/utils/macros.hpp"
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
#line 2 "/home/user/Library/graph/functional_graph.hpp"
#include <cassert>
#include <deque>
#include <functional>
#line 6 "/home/user/Library/graph/functional_graph.hpp"

/**
 * @brief Namori cycle / なもり閉路
 * @param g a simple connected undirected graph with |E| = |V|
 */
std::deque<int> get_namori_cycle(const std::vector<std::vector<int> >& g) {
    int n = g.size();
    {  // check the namori-ty
        int m = 0;
        REP (i, n) {
            m += g[i].size();
        }
        assert (m == 2 * n);
    }

    std::deque<int> stk;
    std::vector<bool> used(n);
    auto go = [&](auto&& go, int i, int parent) -> int {
        if (used[i]) return i;
        stk.push_back(i);
        used[i] = true;
        for (int j : g[i]) if (j != parent) {
            int k = go(go, j, i);
            if (k != -1) return k;
        }
        assert (stk.back() == i);
        stk.pop_back();
        used[i] = false;
        return -1;
    };
    int i = go(go, 0, -1);
    assert (i != -1);  // fails if the graph is not simple
    while (stk.front() != i) {
        stk.pop_front();
    }
    return stk;
}
#line 7 "main.cpp"
using namespace std;

std::vector<int> solve(int n, const std::vector<std::pair<int, int> >& edges) {
    std::vector<std::vector<int> > g(n);
    std::map<std::pair<int, int>, int> lookup;
    REP (i, edges.size()) {
        auto [a, b] = edges[i];
        g[a].push_back(b);
        g[b].push_back(a);
        lookup[std::make_pair(a, b)] = i;
        lookup[std::make_pair(b, a)] = i;
    }

    auto cycle = get_namori_cycle(g);
    std::vector<int> ans;
    REP (i, cycle.size()) {
        int a = cycle[i];
        int b = cycle[(i + 1) % cycle.size()];
        ans.push_back(lookup[std::make_pair(a, b)]);
    }
    return ans;
}

int main() {
    int n; scanf("%d", &n);
    std::vector<std::pair<int, int> > edges(n);
    REP (i, n) {
        int a, b; scanf("%d%d", &a, &b);
        -- a;
        -- b;
        edges[i] = std::make_pair(a, b);
    }
    std::vector<int> ans = solve(n, edges);
    printf("%d\n", (int)ans.size());
    REP (i, ans.size()) {
        printf("%d%c", ans[i] + 1, i + 1 < ans.size() ? ' ' : '\n');
    }
    return 0;
}
0