結果

問題 No.19 ステージの選択
ユーザー 37zigen37zigen
提出日時 2020-03-31 03:06:19
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,353 bytes
コンパイル時間 3,044 ms
コンパイル使用メモリ 76,160 KB
実行使用メモリ 61,300 KB
最終ジャッジ日時 2023-09-05 17:58:08
合計ジャッジ時間 11,311 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,788 KB
testcase_01 AC 128 ms
55,976 KB
testcase_02 AC 124 ms
56,008 KB
testcase_03 AC 126 ms
55,780 KB
testcase_04 AC 143 ms
55,764 KB
testcase_05 WA -
testcase_06 AC 139 ms
55,832 KB
testcase_07 AC 140 ms
55,936 KB
testcase_08 AC 142 ms
55,628 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

class DJSet{
	int n;
	int[] upper;
	
	public DJSet(int n) {
		this.n=n;
		upper=new int[n];
		Arrays.fill(upper, -1);
	}
	
	int root(int x) {
		return upper[x]<0?x:(upper[x]=root(upper[x]));
	}
	
	boolean equiv(int x,int y) {
		return root(x)==root(y);
	}
	
	void setUnion(int x,int y) {
		x=root(x);
		y=root(y);
		if(x==y)return;
		if(upper[x]<upper[y]) {
			x^=y;y^=x;x^=y;
		}
		upper[y]+=upper[x];
		upper[x]=y;
	}
}

class Main {
	public static void main(String[] args) {
		new Main().run();
	}
	
	void run() {
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		DJSet ds=new DJSet(N);
		int[] to=new int[N];
		int[] level=new int[N];
		for(int i=0;i<N;++i)to[i]=i;
		for(int i=0;i<N;++i) {
			level[i]=sc.nextInt();
			int need=sc.nextInt()-1;
			to[i]=need;
			ds.setUnion(i, need);
		}
		long ans=Arrays.stream(level).sum();
		boolean[] vis=new boolean[N];
		for(int i=0;i<N;++i) {
			if(vis[ds.root(i)])continue;
			int cur=i;
			while(!vis[cur]) {
				vis[cur]=true;
				cur=to[cur];
			}
			int p=cur,min=Integer.MAX_VALUE/3;
			do {
				min=Math.min(min, level[p]);
				p=to[p];
			}while(p!=cur);
			ans+=min;
			
		}
		System.out.println(ans*0.5);
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}

0