結果

問題 No.416 旅行会社
ユーザー TatsunoTatsuno
提出日時 2016-08-28 02:45:23
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,457 bytes
コンパイル時間 1,860 ms
コンパイル使用メモリ 177,668 KB
実行使用メモリ 14,976 KB
最終ジャッジ日時 2024-04-26 07:22:12
合計ジャッジ時間 9,019 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 295 ms
10,752 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 8 ms
6,940 KB
testcase_09 AC 34 ms
6,940 KB
testcase_10 AC 322 ms
11,264 KB
testcase_11 AC 321 ms
11,588 KB
testcase_12 AC 318 ms
11,580 KB
testcase_13 AC 291 ms
10,816 KB
testcase_14 AC 619 ms
14,592 KB
testcase_15 AC 620 ms
14,208 KB
testcase_16 AC 614 ms
14,464 KB
testcase_17 AC 628 ms
14,336 KB
testcase_18 AC 634 ms
14,336 KB
testcase_19 AC 434 ms
14,976 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t; using i64 = int64_t; using f64 = double_t; using str = string;
template <typename T> using vec = vector<T>;
template <typename T> using heap = priority_queue<T, vec<T>, greater<T>>;
#define times(n, i) for (i64 i = 0; i < (n); ++i)
#define range(n, m, i) for (i64 i = (n); i < (m); ++i)
#define upto(n, m, i) for (i64 i = (n); i <= (m); ++i)
#define downto(n, m, i) for (i64 i = (n); i >= (m); --i)
#define foreach(xs, x) for (auto &x : (xs))
#define all(xs) (xs).begin(), (xs).end()
#define sortall(xs) sort(all(xs))
#define reverseall(xs) reverse(all(xs))
#define uniqueall(xs) (xs).erase(unique(all(xs)), (xs).end())
#define maximum(xs) (*max_element(all(xs)))
#define minimum(xs) (*min_element(all(xs)))
#define even(x) (((x) & 1) == 0)
#define odd(x) (((x) & 1) == 1)
#define append emplace_back
const i64 MOD = 1000000007;

i32 n, m, q;
i32 ans[200001];

set<pair<i32, i32>> ab;

struct unionfind {
    vec<i32> data;
    vec<vec<i32>> group;
    unionfind(i32 size) : data(size, -1), group(size) { times(size, i) group[i].append(i); }
    void unite(i32 x, i32 y) {
        x = find(x); y = find(y); if (x == y) return;
        if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x;
        foreach(group[y], z) group[x].append(z);
    }
    i32 find(i32 x) { return data[x] < 0 ? x : data[x] = find(data[x]); }
    i32 size(i32 x) { return -data[x]; }
    i32 same(i32 x, i32 y) { return find(x) == find(y); }
};

i32 main()
{
    cin >> n >> m >> q;
    vec<pair<i32, i32>> cd(q);

    times(m, i) {
        i32 a, b; cin >> a >> b;
        ab.insert(make_pair(a, b));
    }
    times(q, i) {
        cin >> cd[i].first >> cd[i].second;
        ab.erase(cd[i]);
    }
    reverseall(cd);

    unionfind uf(n+1);
    for (auto it = ab.begin(); it != ab.end(); it++) {
        uf.unite(it->first, it->second);
    }

    upto(1, n, i) {
        if (uf.find(i) == 1)
            ans[i] = -1;
    }

    downto(q, 1, j) {
        i32 i = q-j, a = cd[i].first, b = cd[i].second;
        if (uf.same(a, b))
            continue;
        if (ans[a] != 0 || ans[b] != 0) {
            i32 t = uf.find(ans[a] == 0 ? a : b);
            foreach(uf.group[t], x) {
                ans[x] = j;
            }
        }
        uf.unite(a, b);
    }

    upto(2, n, i) {
        cout << ans[i] << endl;
    }
    return 0;
}
0