結果

問題 No.330 Eigenvalue Decomposition
ユーザー PachicobuePachicobue
提出日時 2017-11-09 22:27:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 1,664 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 170,308 KB
実行使用メモリ 4,876 KB
最終ジャッジ日時 2023-08-15 22:27:29
合計ジャッジ時間 4,614 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
4,632 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 27 ms
4,572 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 27 ms
4,584 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 18 ms
4,384 KB
testcase_09 AC 46 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 15 ms
4,376 KB
testcase_12 AC 54 ms
4,876 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 4 ms
4,636 KB
testcase_16 AC 2 ms
4,384 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 12 ms
4,572 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 56 ms
4,696 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 45 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,584 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

class DisjointSets
{
public:
    DisjointSets(const int v)
    {
        m_parent.resize(v);
        m_rank.resize(v);
        m_size.resize(v);
        for (int i = 0; i < v; i++) {
            m_parent[i] = i;
            m_rank[i] = 0;
            m_size[i] = 1;
        }
    }

    bool same(const int a, const int b)
    {
        return find(a) == find(b);
    }


    int find(const int a)
    {
        if (m_parent[a] == a) {
            return a;
        } else {
            return m_parent[a] = find(m_parent[a]);
        }
    }

    void unite(const int a_, const int b_)
    {
        const int a = find(a_);
        const int b = find(b_);
        if (a == b) {
            return;
        }
        if (m_rank[a] > m_rank[b]) {
            m_parent[b] = a;
            m_size[a] += m_size[b];
        } else {
            m_parent[a] = b;
            m_size[b] += m_size[a];
        }
        if (m_rank[a] == m_rank[b]) {
            m_rank[b]++;
        }
    }

    int getSize(const int a)
    {
        return m_size[m_parent[a]];
    }

private:
    vector<int> m_parent;
    vector<int> m_rank;
    vector<int> m_size;
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    DisjointSets uf(N);
    for (int i = 0; i < M; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        uf.unite(a, b);
    }
    vector<int> num(N, 0);
    for (int i = 0; i < N; i++) {
        num[uf.find(i)] = 1;
    }
    const int cnt = accumulate(num.begin(), num.end(), 0);
    cout << cnt << endl;

    return 0;
}
0