結果

問題 No.416 旅行会社
ユーザー mamekinmamekin
提出日時 2016-10-05 17:21:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 928 ms / 4,000 ms
コード長 3,342 bytes
コンパイル時間 1,774 ms
コンパイル使用メモリ 128,428 KB
実行使用メモリ 24,760 KB
最終ジャッジ日時 2023-08-21 10:08:28
合計ジャッジ時間 11,744 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 422 ms
19,024 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 4 ms
4,376 KB
testcase_08 AC 10 ms
4,376 KB
testcase_09 AC 44 ms
4,696 KB
testcase_10 AC 453 ms
19,188 KB
testcase_11 AC 469 ms
24,760 KB
testcase_12 AC 418 ms
24,288 KB
testcase_13 AC 449 ms
24,616 KB
testcase_14 AC 909 ms
20,036 KB
testcase_15 AC 922 ms
22,372 KB
testcase_16 AC 901 ms
23,816 KB
testcase_17 AC 924 ms
22,000 KB
testcase_18 AC 928 ms
21,120 KB
testcase_19 AC 684 ms
23,688 KB
testcase_20 AC 669 ms
21,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class UnionFindTree
{
private:
    const int n;
    vector<int> parent;       // 親ノード
    vector<set<int> > child;  // 子ノード
    vector<int> rank;         // 木の高さの上限
    int find(int i){
        if(parent[i] == i){
            return i;
        }
        else{
            child[parent[i]].erase(i);
            parent[i] = find(parent[i]);
            child[parent[i]].insert(i);
            return parent[i];
        }
    }
public:
    UnionFindTree(int n) : n(n){ // コンストラクタ
        parent.resize(n);
        child.resize(n);
        for(int i=0; i<n; ++i){
            parent[i] = i;
            child[i].insert(i);
        }
        rank.assign(n, 0);
    }
    void unite(int a, int b){ // aとbのグループを併合
        if((a = find(a)) != (b = find(b))){
            if(rank[a] < rank[b]){
                parent[a] = b;
                child[b].insert(a);
            }
            else{
                parent[b] = a;
                child[a].insert(b);
                if(rank[a] == rank[b])
                    ++ rank[a];
            }
        }
    }
    bool same(int a, int b){ // aとbのグループが同じかを調べる
        return find(a) == find(b);
    }
    void getGroup(int a, vector<int>& group){ // グループ構成を返す
        set<int> s;
        queue<int> q;
        s.insert(a);
        q.push(a);
        while(!q.empty()){
            int x = q.front();
            q.pop();
            if(s.find(parent[x]) == s.end()){
                s.insert(parent[x]);
                q.push(parent[x]);
            }
            for(int y : child[x]){
                if(s.find(y) == s.end()){
                    s.insert(y);
                    q.push(y);
                }
            }
        }
        group = vector<int>(s.begin(), s.end());
    }
};

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

    set<pair<int, int> > s;
    for(int i=0; i<m; ++i){
        int a, b;
        cin >> a >> b;
        -- a;
        -- b;
        s.insert(make_pair(a, b));
    }

    vector<int> c(q), d(q);
    for(int i=0; i<q; ++i){
        cin >> c[i] >> d[i];
        -- c[i];
        -- d[i];
        s.erase(make_pair(c[i], d[i]));
    }

    UnionFindTree uft(n);
    for(const auto& p : s)
        uft.unite(p.first, p.second);

    vector<int> ans(n, 0);
    vector<int> v;
    uft.getGroup(0, v);
    for(unsigned i=0; i<v.size(); ++i)
        ans[v[i]] = -1;

    for(int i=q-1; i>=0; --i){
        if(uft.same(c[i], d[i]))
            continue;

        vector<int> v;
        if(uft.same(0, c[i]))
            uft.getGroup(d[i], v);
        else if(uft.same(0, d[i]))
            uft.getGroup(c[i], v);
        for(unsigned j=0; j<v.size(); ++j)
            ans[v[j]] = i + 1;

        uft.unite(c[i], d[i]);
    }
    for(int i=1; i<n; ++i)
        cout << ans[i] << endl;

    return 0;
}
0