結果
問題 | No.1002 Twotone |
ユーザー | 37zigen |
提出日時 | 2020-05-15 14:26:37 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,055 bytes |
コンパイル時間 | 2,504 ms |
コンパイル使用メモリ | 83,524 KB |
実行使用メモリ | 98,412 KB |
最終ジャッジ日時 | 2024-09-18 23:55:02 |
合計ジャッジ時間 | 19,826 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
37,400 KB |
testcase_01 | AC | 57 ms
37,416 KB |
testcase_02 | AC | 56 ms
37,276 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 61 ms
37,660 KB |
testcase_07 | AC | 396 ms
71,396 KB |
testcase_08 | AC | 498 ms
82,244 KB |
testcase_09 | AC | 536 ms
84,876 KB |
testcase_10 | AC | 59 ms
37,508 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | AC | 62 ms
37,836 KB |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 61 ms
37,200 KB |
testcase_27 | AC | 407 ms
75,732 KB |
testcase_28 | AC | 526 ms
85,864 KB |
testcase_29 | AC | 503 ms
96,212 KB |
testcase_30 | AC | 56 ms
37,800 KB |
testcase_31 | AC | 522 ms
96,676 KB |
testcase_32 | AC | 609 ms
83,368 KB |
testcase_33 | AC | 552 ms
96,176 KB |
testcase_34 | AC | 432 ms
84,724 KB |
ソースコード
import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.NoSuchElementException; 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 unite(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; } int sz(int x) { return -upper[root(x)]; } } public class Main{ public static void main(String[] args) { new Main().run(); } void run() { FastScanner sc=new FastScanner(); int N=sc.nextInt(); int K=sc.nextInt(); DJSet ds=new DJSet(N); ArrayList<int[]>[] g=new ArrayList[N]; for (int i=0;i<g.length;++i) g[i]=new ArrayList<>(); int[] u=new int[N-1]; int[] v=new int[N-1]; int[] c=new int[N-1]; for (int i=0;i<N-1;++i) { u[i]=sc.nextInt()-1; v[i]=sc.nextInt()-1; c[i]=sc.nextInt()-1; g[u[i]].add(new int[]{v[i],c[i],i}); g[v[i]].add(new int[]{u[i],c[i],i}); } for (int i=0;i<N;++i) { Collections.sort(g[i],Comparator.comparing(a->a[1])); for (int j=0;j+1<g[i].size();++j) { if (g[i].get(j)[1]==g[i].get(j+1)[1]) { ds.unite(g[i].get(j)[2], g[i].get(j+1)[2]); } } } long ans=0; for (int i=0;i<N;++i) { long sum=0; for (int j=0;j<g[i].size();++j) { if (j+1<g[i].size() && g[i].get(j)[1]==g[i].get(j+1)[1]) continue; sum+=ds.sz(g[i].get(j)[2]); } for (int j=0;j<g[i].size();++j) { if (j+1<g[i].size() && g[i].get(j)[1]==g[i].get(j+1)[1]) continue; ans+=1L*ds.sz(g[i].get(j)[2])*(sum-ds.sz(g[i].get(j)[2])); } } System.out.println(ans/2); } void tr(Object...objects) {System.out.println(Arrays.deepToString(objects));} } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; }else{ ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;} private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;} public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();} public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while(true){ if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; }else if(b == -1 || !isPrintableChar(b)){ return minus ? -n : n; }else{ throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next());} }