結果

問題 No.416 旅行会社
ユーザー tentententen
提出日時 2023-04-05 15:57:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,058 ms / 4,000 ms
コード長 4,937 bytes
コンパイル時間 2,465 ms
コンパイル使用メモリ 85,232 KB
実行使用メモリ 90,292 KB
最終ジャッジ日時 2024-04-10 00:52:51
合計ジャッジ時間 15,795 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 539 ms
78,300 KB
testcase_01 AC 51 ms
50,260 KB
testcase_02 AC 50 ms
50,388 KB
testcase_03 AC 51 ms
50,228 KB
testcase_04 AC 51 ms
50,388 KB
testcase_05 AC 53 ms
50,388 KB
testcase_06 AC 55 ms
50,448 KB
testcase_07 AC 89 ms
51,436 KB
testcase_08 AC 143 ms
52,988 KB
testcase_09 AC 206 ms
58,536 KB
testcase_10 AC 586 ms
76,580 KB
testcase_11 AC 575 ms
76,184 KB
testcase_12 AC 577 ms
76,660 KB
testcase_13 AC 510 ms
78,528 KB
testcase_14 AC 990 ms
88,068 KB
testcase_15 AC 1,038 ms
88,800 KB
testcase_16 AC 989 ms
88,184 KB
testcase_17 AC 1,058 ms
88,572 KB
testcase_18 AC 1,034 ms
88,664 KB
testcase_19 AC 892 ms
90,292 KB
testcase_20 AC 894 ms
88,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        int q = sc.nextInt();
        HashSet<Bridge> remains = new HashSet<>();
        for (int i = 0; i < m; i++) {
            remains.add(new Bridge(sc.nextInt() - 1, sc.nextInt() - 1));
        }
        Bridge[] bridges = new Bridge[q];
        for (int i = 0; i < q; i++) {
            bridges[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1);
            remains.remove(bridges[i]);
        }
        UnionFindTree uft = new UnionFindTree(n);
        for (Bridge x : remains) {
            uft.unite(x);
        }
        int[] ans = new int[n];
        for (int i = 0; i < n; i++) {
            if (uft.same(0, i)) {
                ans[i] = -1;
            }
        }
        for (int i = q - 1; i >= 0; i--) {
            for (int x : uft.unite(bridges[i])) {
                ans[x] = i + 1;
            }
        }
        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;
        ArrayList<ArrayList<Integer>> groups = new ArrayList<>();
        
        public UnionFindTree(int size) {
            parents = new int[size];
            for (int i = 0; i < size; i++) {
                parents[i] = i;
                groups.add(new ArrayList<>());
                groups.get(i).add(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 ArrayList<Integer> unite(int x, int y) {
            int xx = find(x);
            int yy = find(y);
            if (xx == yy) {
                return new ArrayList<>();
            }
            int type = 0;
            if (same(0, xx)) {
                type = 1;
            } else if (same(0, yy)) {
                type = 2;
            }
            ArrayList<Integer> ans = new ArrayList<>();
            if (groups.get(xx).size() < groups.get(yy).size()) {
                if (type == 1) {
                    ans.addAll(groups.get(yy));
                } else if (type == 2) {
                    ans = groups.get(xx);
                }
                groups.get(yy).addAll(groups.get(xx));
                parents[xx] = yy;
            } else {
                if (type == 1) {
                    ans = groups.get(yy);
                } else if (type == 2) {
                    ans.addAll(groups.get(xx));
                }
                groups.get(xx).addAll(groups.get(yy));
                parents[yy] = xx;
            }
            return ans;
        }
        
        public ArrayList<Integer> unite(Bridge x) {
            return unite(x.left, x.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 + right;
        }
        
        public boolean equals(Object o) {
            Bridge b = (Bridge)o;
            return left == b.left && right == b.right;
        }
        
        public String toString() {
            return left + ":" + right;
        }
    }
    
    
}
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
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 int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0