結果

問題 No.556 仁義なきサルたち
ユーザー 1119_29161119_2916
提出日時 2017-08-11 23:12:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 2,000 ms
コード長 1,791 bytes
コンパイル時間 1,236 ms
コンパイル使用メモリ 148,252 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-02 23:35:37
合計ジャッジ時間 2,423 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 8 ms
4,376 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 9 ms
4,376 KB
testcase_16 AC 16 ms
4,376 KB
testcase_17 AC 16 ms
4,376 KB
testcase_18 AC 16 ms
4,376 KB
testcase_19 AC 17 ms
4,380 KB
testcase_20 AC 17 ms
4,376 KB
testcase_21 AC 17 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define int3(x, y, z) Int(x); int2(y, z);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second

//int dxy[5] = {0, 1, 0, -1, 0};
// assign avl ncm dijkstra geo2 kruskal graph uf lca BIT
// matrix dinic next_combination topcoder lcm

struct UnionFind {
    vector<int> parent;
    int size;
    UnionFind(int n) :parent(n, -1), size(n) {}
    bool unite(int x, int y) {
        x = root(x); y = root(y);
        if (x == y)return false;
        if (sizeOf(x) < sizeOf(y))swap(x, y);
        if (sizeOf(x) == sizeOf(y) && x > y) swap(x, y);
        parent[x] += parent[y]; parent[y] = x; size--;
        return true;
    }
    bool same(int x, int y) { return root(x) == root(y); }
    int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
    int sizeOf(int x) { return -parent[root(x)]; }
};

signed main()
{
    int2(n, m);
    UnionFind funami_yui(n);
    rep(i, m) {
        int2(a, b);
        a--; b--;
        funami_yui.unite(a, b);
    }
    rep(i, n) {
        std::cout << funami_yui.root(i)+1 << std::endl;
    }




    return 0;
}
0