結果
問題 |
No.19 ステージの選択
|
ユーザー |
|
提出日時 | 2020-03-31 03:06:19 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,353 bytes |
コンパイル時間 | 2,319 ms |
コンパイル使用メモリ | 79,064 KB |
実行使用メモリ | 54,376 KB |
最終ジャッジ日時 | 2024-06-23 13:27:17 |
合計ジャッジ時間 | 10,602 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 8 WA * 1 TLE * 1 -- * 14 |
ソースコード
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)); } }