結果

問題 No.1293 2種類の道路
ユーザー tentententen
提出日時 2022-09-01 15:05:14
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 3,171 bytes
コンパイル時間 2,650 ms
コンパイル使用メモリ 79,640 KB
実行使用メモリ 88,884 KB
最終ジャッジ日時 2024-04-26 16:55:18
合計ジャッジ時間 7,668 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
42,592 KB
testcase_01 AC 53 ms
37,232 KB
testcase_02 AC 55 ms
37,152 KB
testcase_03 AC 55 ms
37,112 KB
testcase_04 AC 57 ms
37,140 KB
testcase_05 AC 53 ms
36,912 KB
testcase_06 AC 52 ms
36,628 KB
testcase_07 AC 52 ms
36,924 KB
testcase_08 AC 53 ms
37,232 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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