結果

問題 No.330 Eigenvalue Decomposition
ユーザー koba-e964koba-e964
提出日時 2016-12-17 16:06:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 181 ms / 5,000 ms
コード長 1,164 bytes
コンパイル時間 1,490 ms
コンパイル使用メモリ 61,520 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 21:50:25
合計ジャッジ時間 4,310 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,384 KB
testcase_03 AC 79 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 90 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 59 ms
4,380 KB
testcase_09 AC 151 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 48 ms
4,380 KB
testcase_12 AC 175 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 46 ms
4,380 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 5 ms
4,380 KB
testcase_18 AC 29 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 7 ms
4,376 KB
testcase_21 AC 181 ms
4,380 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 12 ms
4,376 KB
testcase_24 AC 151 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 7 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;

/*
 * Union-Find tree
 * header requirement: vector
 */
class UnionFind {
private:
  std::vector<int> disj;
  std::vector<int> rank;
public:
  UnionFind(int n) : disj(n), rank(n) {
    for (int i = 0; i < n; ++i) {
      disj[i] = i;
      rank[i] = 0;
    }
  }
  int root(int x) {
    if (disj[x] == x) {
      return x;
    }
    return disj[x] = root(disj[x]);
  }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return;
    }
    if (rank[x] < rank[y]) {
      disj[x] = y;
    } else {
      disj[y] = x;
      if (rank[x] == rank[y]) {
	++rank[x];
      }
    }
  }
  bool is_same_set(int x, int y) {
    return root(x) == root(y);
  }
};

// Solution in the editoral.
int main(void){
  int n, m;
  cin >> n >> m;
  int conn = n;
  UnionFind uf(n);
  REP(i, 0, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    if (not uf.is_same_set(a, b)) {
      uf.unite(a, b);
      conn--;
    }
  }
  cout << conn << endl;
}
0