結果
| 問題 |
No.1002 Twotone
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-15 14:26:37 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 16 WA * 17 |
ソースコード
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());}
}