結果

問題 No.1002 Twotone
ユーザー 37zigen37zigen
提出日時 2020-05-15 14:26:37
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 4,055 bytes
コンパイル時間 4,991 ms
コンパイル使用メモリ 83,456 KB
実行使用メモリ 113,440 KB
最終ジャッジ日時 2023-10-19 03:41:43
合計ジャッジ時間 24,035 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
53,872 KB
testcase_01 AC 67 ms
53,824 KB
testcase_02 AC 67 ms
53,824 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 AC 70 ms
53,896 KB
testcase_07 AC 438 ms
84,760 KB
testcase_08 AC 617 ms
95,808 KB
testcase_09 AC 633 ms
95,808 KB
testcase_10 AC 70 ms
53,816 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 70 ms
53,824 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 70 ms
54,332 KB
testcase_27 AC 476 ms
88,844 KB
testcase_28 AC 638 ms
112,048 KB
testcase_29 AC 597 ms
111,656 KB
testcase_30 AC 69 ms
53,812 KB
testcase_31 AC 618 ms
99,364 KB
testcase_32 AC 739 ms
113,440 KB
testcase_33 AC 615 ms
101,256 KB
testcase_34 AC 486 ms
97,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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());}
}
0