結果

問題 No.416 旅行会社
ユーザー kyunakyuna
提出日時 2019-08-16 23:12:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 617 ms / 4,000 ms
コード長 1,702 bytes
コンパイル時間 1,317 ms
コンパイル使用メモリ 90,688 KB
実行使用メモリ 14,600 KB
最終ジャッジ日時 2023-08-21 10:25:32
合計ジャッジ時間 8,559 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 275 ms
10,488 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,388 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 8 ms
4,384 KB
testcase_09 AC 33 ms
4,380 KB
testcase_10 AC 306 ms
10,776 KB
testcase_11 AC 295 ms
11,036 KB
testcase_12 AC 295 ms
11,396 KB
testcase_13 AC 272 ms
10,980 KB
testcase_14 AC 606 ms
14,568 KB
testcase_15 AC 606 ms
14,464 KB
testcase_16 AC 612 ms
14,600 KB
testcase_17 AC 617 ms
14,448 KB
testcase_18 AC 616 ms
14,416 KB
testcase_19 AC 438 ms
14,452 KB
testcase_20 AC 432 ms
14,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct UnionFind {
    vector<int> data;
    int sz;
    vector<vector<int>> mem;
    UnionFind(int sz) : data(sz, -1), sz(sz), mem(sz) {
        for (int i = 0; i < sz; i++) init(i);
    }
    bool unionSet(int x, int y) {
        if ((x = root(x)) == (y = root(y))) return false;
        if (data[x] > data[y]) swap(x, y);
        data[x] += data[y]; data[y] = x; sz--;
        mem[x].insert(mem[x].end(), mem[y].begin(), mem[y].end());
        mem[y].clear();
        return true;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    vector<int>& operator[](int x) { return mem[root(x)]; }
    void init(int x) { int r = root(x); mem[r] = {r}; }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return sz; }
};

int main() {
    int n, m, q; cin >> n >> m >> q;
    set<pair<int, int>> edges;
    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b; a--, b--;
        edges.emplace(a, b);
    }
    vector<pair<int, int>> cd;
    for (int i = 0; i < q; i++) {
        int c, d; cin >> c >> d; c--, d--;
        cd.emplace_back(c, d);
        edges.erase({c, d});
    }
    UnionFind uf(n);
    for (auto &p: edges) uf.unionSet(p.first, p.second);
    vector<int> res(n, 0);
    for (int x: uf[0]) res[x] = -1;
    uf[0].clear();
    for (int i = q - 1; i >= 0; i--) {
        int c = cd[i].first, d = cd[i].second;
        uf.unionSet(c, d);
        for (int x: uf[0]) res[x] = i + 1;
        uf[0].clear();
    }
    for (int i = 1; i < n; i++) cout << res[i] << endl;
    return 0;
}
0