結果

問題 No.1254 補強への架け橋
ユーザー ぷらぷら
提出日時 2020-10-09 21:49:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 2,528 bytes
コンパイル時間 2,080 ms
コンパイル使用メモリ 186,540 KB
実行使用メモリ 25,156 KB
最終ジャッジ日時 2023-09-27 16:21:07
合計ジャッジ時間 14,382 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 1 ms
4,384 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,384 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 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,376 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 2 ms
4,380 KB
testcase_56 AC 2 ms
4,380 KB
testcase_57 AC 2 ms
4,376 KB
testcase_58 AC 3 ms
4,376 KB
testcase_59 AC 2 ms
4,376 KB
testcase_60 AC 2 ms
4,376 KB
testcase_61 AC 2 ms
4,380 KB
testcase_62 AC 2 ms
4,376 KB
testcase_63 AC 11 ms
4,764 KB
testcase_64 AC 3 ms
4,376 KB
testcase_65 AC 8 ms
4,436 KB
testcase_66 AC 7 ms
4,376 KB
testcase_67 AC 3 ms
4,380 KB
testcase_68 AC 7 ms
4,380 KB
testcase_69 AC 8 ms
4,380 KB
testcase_70 AC 4 ms
4,380 KB
testcase_71 AC 3 ms
4,380 KB
testcase_72 AC 8 ms
4,380 KB
testcase_73 AC 4 ms
4,376 KB
testcase_74 AC 9 ms
4,376 KB
testcase_75 AC 7 ms
4,380 KB
testcase_76 AC 3 ms
4,380 KB
testcase_77 AC 6 ms
4,376 KB
testcase_78 AC 11 ms
4,464 KB
testcase_79 AC 10 ms
4,460 KB
testcase_80 AC 9 ms
4,376 KB
testcase_81 AC 8 ms
4,612 KB
testcase_82 AC 10 ms
4,404 KB
testcase_83 AC 164 ms
16,740 KB
testcase_84 AC 130 ms
15,808 KB
testcase_85 AC 75 ms
10,976 KB
testcase_86 AC 145 ms
14,684 KB
testcase_87 AC 164 ms
15,728 KB
testcase_88 AC 17 ms
5,120 KB
testcase_89 AC 149 ms
15,536 KB
testcase_90 AC 96 ms
11,776 KB
testcase_91 AC 75 ms
10,252 KB
testcase_92 AC 35 ms
6,764 KB
testcase_93 AC 124 ms
14,224 KB
testcase_94 AC 102 ms
12,956 KB
testcase_95 AC 103 ms
12,824 KB
testcase_96 AC 142 ms
15,996 KB
testcase_97 AC 50 ms
8,868 KB
testcase_98 AC 132 ms
15,736 KB
testcase_99 AC 65 ms
10,520 KB
testcase_100 AC 143 ms
16,524 KB
testcase_101 AC 25 ms
6,532 KB
testcase_102 AC 14 ms
4,900 KB
testcase_103 AC 31 ms
6,712 KB
testcase_104 AC 41 ms
8,076 KB
testcase_105 AC 119 ms
14,100 KB
testcase_106 AC 56 ms
9,520 KB
testcase_107 AC 155 ms
16,800 KB
testcase_108 AC 158 ms
16,520 KB
testcase_109 AC 113 ms
13,412 KB
testcase_110 AC 107 ms
12,772 KB
testcase_111 AC 124 ms
13,572 KB
testcase_112 AC 42 ms
7,620 KB
testcase_113 AC 89 ms
12,160 KB
testcase_114 AC 56 ms
9,256 KB
testcase_115 AC 16 ms
5,128 KB
testcase_116 AC 67 ms
10,584 KB
testcase_117 AC 41 ms
7,944 KB
testcase_118 AC 131 ms
15,680 KB
testcase_119 AC 68 ms
10,660 KB
testcase_120 AC 137 ms
15,204 KB
testcase_121 AC 36 ms
7,276 KB
testcase_122 AC 71 ms
10,180 KB
testcase_123 AC 2 ms
4,380 KB
testcase_124 AC 292 ms
25,156 KB
testcase_125 AC 290 ms
25,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 3e18+7;
int mod = 1e9+7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct UnionFind {
    vector<int> par, size;
    UnionFind(int x) {
        par.resize(x);
        size.resize(x, 1);
        for(int i = 0; i < x; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if (par[x] == x)
            return x;
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;
        if (size[x] < size[y]) {
            par[x] = y;
            size[y] += size[x];
        }
        else {
            par[y] = x;
            size[x] += size[y];
        }
    }
};
signed main() {
    int N;
    cin >> N;
    vector<vector<int>>ki(N);
    UnionFind uf(N);
    map<P,int>Mp;
    vector<int>ans;
    vector<int>cnt(N,INF);
    for(int i = 0; i < N; i++) {
        int a,b;
        cin >> a >> b;
        a--;b--;
        Mp[{a,b}] = i+1;
        if(uf.same(a,b)) {
            ans.push_back(i+1);
            queue<int>que;
            que.push(a);
            cnt[a] = 0;
            while (!que.empty()) {
                int x = que.front();
                que.pop();
                for(int i = 0; i < ki[x].size(); i++) {
                    if(cnt[ki[x][i]] == INF) {
                        cnt[ki[x][i]] = cnt[x]+1;
                        que.push(ki[x][i]);
                    }
                }
            }
            vector<int>road;
            road.push_back(b);
            que.push(b);
            while (!que.empty()) {
                int x = que.front();
                que.pop();
                for(int i = 0; i < ki[x].size(); i++) {
                    if(cnt[ki[x][i]] == cnt[x]-1) {
                        road.push_back(ki[x][i]);
                        que.push(ki[x][i]);
                    }
                }
            }
            for(int i = 0; i < road.size()-1; i++) {
                ans.push_back(max(Mp[{road[i],road[i+1]}],Mp[{road[i+1],road[i]}]));
            }
            break;
        }
        uf.unite(a,b);
        ki[a].push_back(b);
        ki[b].push_back(a);
    }
    cout << ans.size() << endl;
    for(int i = 0; i < ans.size(); i++) {
        cout << ans[i] << ((i+1 == ans.size())?"\n":" ");
    }
}
0