結果

問題 No.416 旅行会社
ユーザー pekempeypekempey
提出日時 2016-10-29 05:40:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 280 ms / 4,000 ms
コード長 1,837 bytes
コンパイル時間 1,781 ms
コンパイル使用メモリ 157,132 KB
実行使用メモリ 22,488 KB
最終ジャッジ日時 2023-08-21 10:07:19
合計ジャッジ時間 5,465 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
16,208 KB
testcase_01 AC 3 ms
7,580 KB
testcase_02 AC 3 ms
7,576 KB
testcase_03 AC 3 ms
7,560 KB
testcase_04 AC 3 ms
7,636 KB
testcase_05 AC 3 ms
7,888 KB
testcase_06 AC 3 ms
7,760 KB
testcase_07 AC 3 ms
7,944 KB
testcase_08 AC 4 ms
7,928 KB
testcase_09 AC 13 ms
8,444 KB
testcase_10 AC 110 ms
16,064 KB
testcase_11 AC 110 ms
16,020 KB
testcase_12 AC 112 ms
15,996 KB
testcase_13 AC 91 ms
15,896 KB
testcase_14 AC 276 ms
22,232 KB
testcase_15 AC 279 ms
22,240 KB
testcase_16 AC 280 ms
22,152 KB
testcase_17 AC 275 ms
22,320 KB
testcase_18 AC 277 ms
22,488 KB
testcase_19 AC 152 ms
15,936 KB
testcase_20 AC 152 ms
15,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;
int par[N];
list<int> items[N];

struct node { 
    int val;
    node *next;
};

node body[N];
node *head[N];
node *tail[N];
int size[N];

void join(int x, int y) {
    tail[x]->next = head[y];
    tail[x] = tail[y];
}

int find(int x) {
    if (par[x] == x) return x;
    return par[x] = find(par[x]);
}

void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if (size[x] < size[y]) swap(x, y);
    size[x] += size[y];
    par[y] = x;
    join(x, y);
}

int main() {
    int n, m, q;
    cin >> n >> m >> q;

    for (int i = 0; i < n; i++) {
        size[i] = 1;
        par[i] = i;
        head[i] = &body[i];
        tail[i] = &body[i];
        body[i].val = i;
        body[i].next = nullptr;
    }

    vector<pair<int, int>> g;
    set<pair<int, int>> removed;

    for (int i = 0; i < m; i++) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        g.push_back(minmax(a, b));
    }

    vector<int> c(q), d(q);
    for (int i = 0; i < q; i++) {
        scanf("%d %d", &c[i], &d[i]);
        c[i]--; d[i]--;
        removed.insert(minmax(c[i], d[i]));
    }

    for (auto e : g) if (!removed.count(e)) unite(e.first, e.second);

    vector<int> ans(n);
    for (int i = 0; i < n; i++) ans[i] = find(0) == find(i) ? -1 : 0;

    for (int i = q - 1; i >= 0; i--) {
        c[i] = find(c[i]);
        d[i] = find(d[i]);
        if (c[i] == d[i]) continue;
        if (d[i] == find(0)) swap(c[i], d[i]);
        if (c[i] == find(0)) {
            node *it = head[d[i]];
            for (node *it = head[d[i]]; it != nullptr; it = it->next) {
                ans[it->val] = i + 1;
            }
        }
        unite(c[i], d[i]);
    }

    for (int i = 1; i < n; i++) {
        printf("%d\n", ans[i]);
    }
}
0