結果

問題 No.556 仁義なきサルたち
ユーザー mamekinmamekin
提出日時 2017-08-20 17:45:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,892 bytes
コンパイル時間 1,249 ms
コンパイル使用メモリ 108,608 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 18:21:46
合計ジャッジ時間 2,277 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 4 ms
5,376 KB
testcase_13 AC 8 ms
5,376 KB
testcase_14 AC 9 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 15 ms
5,376 KB
testcase_17 AC 16 ms
5,376 KB
testcase_18 AC 18 ms
5,376 KB
testcase_19 AC 17 ms
5,376 KB
testcase_20 AC 20 ms
5,376 KB
testcase_21 AC 19 ms
5,376 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:
    typedef pair<int, int> T1;
    typedef int T2;

    static const T1 INIT_DATA;
    T1 updateData(T1 prev, T2 x){
        return make_pair(prev.first, x);
    }
    T1 uniteData(T1 v1, T1 v2){
        if(v1 < v2)
            return make_pair(v1.first + v2.first, v1.second);
        else
            return make_pair(v1.first + v2.first, v2.second);
    }

    int n;
    vector<int> parent; // 親ノード
    vector<int> rank;   // 木の高さの上限
    vector<int> num;    // グループの要素数
    vector<T1> data;    // グループが持つ値

    int find(int x){
        if(parent[x] == x)
            return x;
        else
            return parent[x] = find(parent[x]);
    }
public:
    UnionFindTree(int n){ // コンストラクタ
        this->n = n;
        parent.resize(n);
        for(int i=0; i<n; ++i)
            parent[i] = i;
        rank.assign(n, 0);
        data.assign(n, INIT_DATA);
        num.assign(n, 1);
    }
    void unite(int a, int b){ // aとbのグループを併合
        if((a = find(a)) != (b = find(b))){
            if(rank[a] < rank[b]){
                parent[a] = b;
                data[b] = uniteData(data[b], data[a]);
                num[b] += num[a];
            }else{
                parent[b] = a;
                if(rank[a] == rank[b])
                    ++ rank[a];
                data[a] = uniteData(data[a], data[b]);
                num[a] += num[b];
            }
            -- n;
        }
    }
    bool same(int a, int b){ // aとbのグループが同じかを調べる
        return find(a) == find(b);
    }
    void update(int a, T2 x){ // ノードの値を更新する
        a = find(a);
        data[a] = updateData(data[a], x);
    }
    T1 getData(int a){ // グループが持つ値を返す
        a = find(a);
        return data[a];
    }
    int getNum(){ // グループの数を返す
        return n;
    }
    int getNum(int a){ // aのグループの要素数を返す
        return num[find(a)];
    }
};
const UnionFindTree::T1 UnionFindTree::INIT_DATA = make_pair(-1, -1);

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

    UnionFindTree uft(n+1);
    for(int i=1; i<=n; ++i)
        uft.update(i, i);

    for(int i=0; i<m; ++i){
        int a, b;
        cin >> a >> b;
        uft.unite(a, b);
    }

    for(int i=1; i<=n; ++i)
        cout << uft.getData(i).second << endl;

    return 0;
}
0