結果

問題 No.416 旅行会社
ユーザー finefine
提出日時 2016-08-27 01:05:43
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 4,000 ms
コード長 2,637 bytes
コンパイル時間 2,967 ms
コンパイル使用メモリ 183,348 KB
実行使用メモリ 13,960 KB
最終ジャッジ日時 2023-08-21 09:48:48
合計ジャッジ時間 6,262 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
11,736 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 10 ms
4,384 KB
testcase_10 AC 76 ms
11,744 KB
testcase_11 AC 75 ms
11,736 KB
testcase_12 AC 77 ms
11,668 KB
testcase_13 AC 68 ms
11,688 KB
testcase_14 AC 177 ms
13,828 KB
testcase_15 AC 176 ms
13,960 KB
testcase_16 AC 175 ms
13,960 KB
testcase_17 AC 175 ms
13,524 KB
testcase_18 AC 176 ms
13,848 KB
testcase_19 AC 115 ms
12,512 KB
testcase_20 AC 113 ms
12,520 KB
権限があれば一括ダウンロードができます

ソースコード

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;
    vector<bool> memo(m, true);
    sort(e.begin(), e.end());
    for (int i = 0; i < q; i++) {
        int c, d;
        scanf("%d %d", &c, &d);
        c--;
        d--;
        query.emplace_back(c, d);
        memo[(int)(lower_bound(e.begin(), e.end(), P(c, d)) - e.begin())] = false;
    }
    Union_Find uf(n);
    for (int i = 0; i < m; i++) {
        if (memo[i]) uf.Union(e[i].first, e[i].second);
    }
    
    vector<int> ans(n, -1);
    for (int i = 1; i < n; i++) {
        if (!uf.same(0, i)) ans[i] = 0;
    }
    
    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