結果
| 問題 |
No.1293 2種類の道路
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-09-01 15:05:14 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,171 bytes |
| コンパイル時間 | 2,740 ms |
| コンパイル使用メモリ | 79,136 KB |
| 実行使用メモリ | 251,300 KB |
| 最終ジャッジ日時 | 2024-11-14 06:17:38 |
| 合計ジャッジ時間 | 38,171 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 12 TLE * 10 |
ソースコード
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner();
int n = sc.nextInt();
int d = sc.nextInt();
int w = sc.nextInt();
UnionFindTree drive = new UnionFindTree(n);
UnionFindTree walk = new UnionFindTree(n);
for (int i = 0; i < d; i++) {
drive.unite(sc.nextInt() - 1, sc.nextInt() - 1);
}
for (int i = 0; i < w; i++) {
walk.unite(sc.nextInt() - 1, sc.nextInt() - 1);
}
HashMap<Integer, HashSet<Integer>> dUnion = new HashMap<>();
HashMap<Integer, HashSet<Integer>> cUnion = new HashMap<>();
HashMap<Integer, HashSet<Integer>> wUnion = new HashMap<>();
for (int i = 0; i < n; i++) {
int pd = drive.find(i);
if (!dUnion.containsKey(pd)) {
dUnion.put(pd, new HashSet<>());
cUnion.put(pd, new HashSet<>());
}
dUnion.get(pd).add(i);
int pw = walk.find(i);
if (!wUnion.containsKey(pw)) {
wUnion.put(pw, new HashSet<>());
}
cUnion.get(pd).add(pw);
wUnion.get(pw).add(i);
}
long ans = 0;
for (int x : dUnion.keySet()) {
long count = 0;
for (int y : cUnion.get(x)) {
for (int z : wUnion.get(y)) {
if (!dUnion.get(x).contains(z)) {
count++;
}
}
}
long size = dUnion.get(x).size();
ans += (size - 1 + count) * size;
}
System.out.println(ans);
}
static class UnionFindTree {
int[] parents;
public UnionFindTree(int size) {
parents = new int[size];
for (int i = 0; i < size; i++) {
parents[i] = i;
}
}
public int find(int x) {
if (x == parents[x]) {
return x;
} else {
return parents[x] = find(parents[x]);
}
}
public boolean same(int x, int y) {
return find(x) == find(y);
}
public void unite(int x, int y) {
if (!same(x, y)) {
parents[find(x)] = find(y);
}
}
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
public Scanner() throws Exception {
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten