結果

問題 No.416 旅行会社
ユーザー firiexpfiriexp
提出日時 2019-12-26 12:16:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,771 ms / 4,000 ms
コード長 2,466 bytes
コンパイル時間 1,124 ms
コンパイル使用メモリ 110,916 KB
実行使用メモリ 23,264 KB
最終ジャッジ日時 2023-08-21 10:27:45
合計ジャッジ時間 25,519 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 742 ms
15,784 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 15 ms
4,696 KB
testcase_10 AC 772 ms
15,768 KB
testcase_11 AC 754 ms
15,676 KB
testcase_12 AC 434 ms
15,804 KB
testcase_13 AC 756 ms
15,780 KB
testcase_14 AC 2,752 ms
23,128 KB
testcase_15 AC 2,746 ms
23,156 KB
testcase_16 AC 2,765 ms
23,200 KB
testcase_17 AC 2,771 ms
23,188 KB
testcase_18 AC 2,751 ms
23,264 KB
testcase_19 AC 2,315 ms
16,900 KB
testcase_20 AC 2,319 ms
16,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

struct QuickFind {
    int n;
    vector<int> roots;
    vector<vector<int>> v;
    explicit QuickFind(int n) : n(n) {
        v.resize(n);
        for (int i = 0; i < n; ++i) v[i].emplace_back(i);
        roots.resize(n);
        iota(roots.begin(),roots.end(), 0);
    }

    int root(int a){ return roots[a]; }
    size_t size(int a){ return v[a].size(); }
    bool unite(int a, int b){
        if(same(a, b)) return false;
        a = roots[a], b = roots[b];
        if(size(a) < size(b)) swap(a, b);
        v[a].reserve(v[a].size()+v[b].size());
        for (auto &&i : v[b]) {
            v[a].emplace_back(i);
            roots[i] = a;
        }
        v[b].clear();
        v[b].shrink_to_fit();
        return true;
    }
    bool same(int a, int b){ return roots[a] == roots[b]; }
    vector<int>& components(int x){ return v[roots[x]];}
};


int main() {
    int n, m, q;
    cin >> n >> m >> q;
    vector<pair<int, int>> A(m), B(q);
    for (int i = 0; i < m; ++i) scanf("%d %d", &A[i].first, &A[i].second);
    for (int i = 0; i < q; ++i) scanf("%d %d", &B[i].first, &B[i].second);
    set<pair<int, int>> S(B.begin(),B.end());
    vector<int> ans(n);
    QuickFind G(n);
    for (int i = 0; i < m; ++i) {
        if(S.count(A[i])) continue;
        int a, b; tie(a, b) = A[i];
        a--; b--;
        if(G.roots[a] == G.roots[b]) continue;
        if(G.roots[a] == G.roots[0]){
            for (auto &&j : G.components(b)) ans[j] = -1;
        }else if(G.roots[b] == G.roots[0]){
            for (auto &&j : G.components(a)) ans[j] = -1;
        }
        G.unite(a, b);
    }
    for (int i = q-1; i >= 0; --i) {
        int a, b; tie(a, b) = B[i];
        a--; b--;
        if(G.roots[a] == G.roots[b]) continue;
        if(G.roots[a] == G.roots[0]){
            for (auto &&j : G.components(b)) ans[j] = i+1;
        } else if(G.roots[b] == G.roots[0]){
            for (auto &&j : G.components(a)) ans[j] = i+1;
        }
        G.unite(a, b);
    }
    for (int i = 1; i < n; ++i) {
        printf("%d\n", ans[i]);
    }
    return 0;
}
0