結果

問題 No.330 Eigenvalue Decomposition
ユーザー 37zigen
提出日時 2016-05-23 22:32:37
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,123 ms / 5,000 ms
コード長 1,039 bytes
コンパイル時間 2,224 ms
コンパイル使用メモリ 77,156 KB
実行使用メモリ 58,876 KB
最終ジャッジ日時 2024-10-07 03:26:20
合計ジャッジ時間 18,539 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.Arrays;
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		DJSet ds=new DJSet(n);
		for(int i=0;i<m;i++){
			int a=sc.nextInt()-1;
			int b=sc.nextInt()-1;
			int c=sc.nextInt();
			ds.setUnion(a, b);
		}
		System.out.println(ds.count());
	}
	class DJSet{
		int n;//the number of vertices
		int[] d;
		DJSet(int n){
			this.n=n;
			d=new int[n];
			Arrays.fill(d, -1);
		}
		int root(int x){
			return d[x]<0?x:root(d[x]);
		}
		boolean setUnion(int x,int y){
			x=root(x);
			y=root(y);
			if(x!=y){
				if(x<y){
					int d=x;
					x=y;
					y=d;
				}
				//x>y
				d[y]+=d[x];
				d[x]=y;
			}
			return x!=y;
		}
		boolean equiv(int x,int y){
			return root(x)==root(y);
		}
		int size(int x){
			return d[root(x)]*(-1);
		}
		//連結グラフの数
		int count(){
			int ct=0;
			for(int u:d){
				if(u<0)ct++;
			}
			return ct;
		}
	}
}
0