結果

問題 No.416 旅行会社
ユーザー finefine
提出日時 2016-08-27 00:52:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,606 bytes
コンパイル時間 1,923 ms
コンパイル使用メモリ 176,900 KB
実行使用メモリ 18,268 KB
最終ジャッジ日時 2024-04-26 05:25:36
合計ジャッジ時間 22,677 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,449 ms
18,268 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 5 ms
6,940 KB
testcase_09 AC 41 ms
6,940 KB
testcase_10 AC 2,364 ms
12,520 KB
testcase_11 AC 2,362 ms
12,464 KB
testcase_12 AC 2,361 ms
12,664 KB
testcase_13 AC 2,433 ms
12,744 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> P;

struct Union_Find {
    //各要素が属する集合の代表(根)を管理する
    //もし、要素xが根であればdata[x]は負の値を取り、-data[x]はxが属する集合の大きさに等しい
    vector<int> data;
    vector< vector<int> > array;
    Union_Find(int size) : data(size, -1), array(size) {for (int i = 0; i < size; i++) array[i].emplace_back(i);}
    bool Union(int x, int y) {
        x = Find(x);
        y = Find(y);
        bool is_union = (x != y);
        if (is_union) {
            if (data[x] > data[y]) swap(x, y);
            data[x] += data[y];
            array[x].insert(array[x].end(), array[y].begin(), array[y].end());
            data[y] = x;
            array[y].clear();
        }
        return is_union;
    }
    int Find(int x) {
        if (data[x] < 0) { //要素xが根である
            return x;
        } else {
            data[x] = Find(data[x]); //data[x]がxの属する集合の根でない場合、根になるよう更新される
            return data[x];
        }
    }
    bool same(int x, int y) {
        return Find(x) == Find(y);
    }
    int size(int x) {
        return -data[Find(x)];
    }
};

void setter(vector<int>& ans, int z, int v, Union_Find& uf) {
    z = uf.Find(z);
    for (auto i = uf.array[z].begin(), e = uf.array[z].end(); i != e; ++i) {
        ans[*i] = v;
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, q;
    scanf("%d %d %d", &n, &m, &q);
    vector<P> e;
    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--;
        b--;
        e.emplace_back(a, b);
    }
    vector<P> query;
    for (int i = 0; i < q; i++) {
        int c, d;
        scanf("%d %d", &c, &d);
        c--;
        d--;
        query.emplace_back(c, d);
        e.erase(find(e.begin(), e.end(), P(c, d)));
    }
    Union_Find uf(n);
    for (auto i = e.begin(), f = e.end(); i != f; ++i) {
        uf.Union((*i).first, (*i).second);
    }
    
    vector<int> ans(n, -1);
    vector<int> memo;
    for (int i = 1; i < n; i++) {
        if (!uf.same(0, i)) ans[i] = 0;
        memo.emplace_back(i);
    }
    
    for (int i = q - 1; i >= 0; i--) {
        int x = query[i].first, y = query[i].second;
        if (uf.same(x, y)) continue;
        if (uf.same(0, x)) setter(ans, y, i + 1, uf);
        else if (uf.same(0, y)) setter(ans, x, i + 1, uf);
        uf.Union(x, y);
    }
    
    for (int i = 1; i < n; i++) {
        printf("%d\n", ans[i]);
    }
    return 0;
}
0