結果

問題 No.416 旅行会社
ユーザー kyunakyuna
提出日時 2019-08-16 23:05:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,703 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 90,340 KB
実行使用メモリ 14,880 KB
最終ジャッジ日時 2023-10-24 04:08:29
合計ジャッジ時間 10,095 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 283 ms
10,772 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 317 ms
11,220 KB
testcase_11 AC 314 ms
11,484 KB
testcase_12 AC 323 ms
11,484 KB
testcase_13 AC 283 ms
11,336 KB
testcase_14 AC 670 ms
14,796 KB
testcase_15 AC 649 ms
14,796 KB
testcase_16 AC 671 ms
14,796 KB
testcase_17 AC 688 ms
14,796 KB
testcase_18 AC 656 ms
14,796 KB
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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, -1);
    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