結果

問題 No.416 旅行会社
ユーザー tentententen
提出日時 2020-10-16 17:16:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,072 ms / 4,000 ms
コード長 3,741 bytes
コンパイル時間 2,361 ms
コンパイル使用メモリ 80,464 KB
実行使用メモリ 98,588 KB
最終ジャッジ日時 2024-05-08 15:57:52
合計ジャッジ時間 26,343 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,269 ms
77,940 KB
testcase_01 AC 115 ms
41,156 KB
testcase_02 AC 118 ms
40,008 KB
testcase_03 AC 118 ms
40,272 KB
testcase_04 AC 137 ms
41,632 KB
testcase_05 AC 142 ms
41,832 KB
testcase_06 AC 160 ms
42,208 KB
testcase_07 AC 206 ms
44,144 KB
testcase_08 AC 260 ms
47,180 KB
testcase_09 AC 479 ms
51,888 KB
testcase_10 AC 1,322 ms
82,592 KB
testcase_11 AC 1,260 ms
77,268 KB
testcase_12 AC 1,323 ms
84,288 KB
testcase_13 AC 1,260 ms
89,296 KB
testcase_14 AC 2,072 ms
98,588 KB
testcase_15 AC 1,981 ms
97,448 KB
testcase_16 AC 2,028 ms
98,464 KB
testcase_17 AC 2,029 ms
98,312 KB
testcase_18 AC 1,906 ms
97,604 KB
testcase_19 AC 1,589 ms
81,312 KB
testcase_20 AC 1,539 ms
79,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int q = sc.nextInt();
        Bridge[] org = new Bridge[m];
        for (int i = 0; i < m; i++) {
            org[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1);
        }
        HashSet<Bridge> broken = new HashSet<>();
        Bridge[] breaking = new Bridge[q];
        for (int i = 0; i < q; i++) {
            breaking[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1);
            broken.add(breaking[i]);
        }
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        UnionFindTree uft = new UnionFindTree(n);
        for (Bridge b : org) {
            if (!broken.contains(b)) {
                uft.unite(b);
                graph.get(b.left).add(b.right);
                graph.get(b.right).add(b.left);
            }
        }
        boolean[] visited = new boolean[n];
        int[] ans = new int[n];
        ArrayDeque<Integer> deq = new ArrayDeque<>();
        deq.add(0);
        while (deq.size() > 0) {
            int x = deq.poll();
            if (visited[x]) {
                continue;
            }
            visited[x] = true;
            ans[x] = -1;
            deq.addAll(graph.get(x));
        }
        for (int i = q - 1; i >= 0; i--) {
            if (uft.same(breaking[i])) {
                continue;
            }
            if (uft.same(0, breaking[i].left)) {
                deq.add(breaking[i].right);
            } else if (uft.same(0, breaking[i].right)) {
                deq.add(breaking[i].left);
            }
            while (deq.size() > 0) {
                int x = deq.poll();
                if (visited[x]) {
                    continue;
                }
                visited[x] = true;
                ans[x] = i + 1;
                deq.addAll(graph.get(x));
            }
            uft.unite(breaking[i]);
            graph.get(breaking[i].left).add(breaking[i].right);
            graph.get(breaking[i].right).add(breaking[i].left);
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i < n; i++) {
            sb.append(ans[i]).append("\n");
        }
        System.out.print(sb);
    }
    
    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 (parents[x] == x) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public boolean same(Bridge b) {
            return same(b.left, b.right);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
        
        public void unite(Bridge b) {
            unite(b.left, b.right);
        }
    }
    
    static class Bridge {
        int left;
        int right;
        
        public Bridge(int left, int right) {
            this.left = left;
            this.right = right;
        }
        
        
        public int hashCode() {
            return left;
        }
        
        public boolean equals(Object o) {
            Bridge b = (Bridge)o;
            return left == b.left && right == b.right;
        }
    }
} 
0