結果

問題 No.416 旅行会社
ユーザー tentententen
提出日時 2023-04-05 15:57:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,148 ms / 4,000 ms
コード長 4,937 bytes
コンパイル時間 2,897 ms
コンパイル使用メモリ 92,340 KB
実行使用メモリ 80,280 KB
最終ジャッジ日時 2024-10-02 01:58:51
合計ジャッジ時間 17,456 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 586 ms
67,232 KB
testcase_01 AC 54 ms
36,972 KB
testcase_02 AC 53 ms
37,108 KB
testcase_03 AC 53 ms
37,076 KB
testcase_04 AC 54 ms
37,112 KB
testcase_05 AC 56 ms
37,296 KB
testcase_06 AC 59 ms
37,416 KB
testcase_07 AC 87 ms
38,600 KB
testcase_08 AC 150 ms
40,992 KB
testcase_09 AC 222 ms
48,192 KB
testcase_10 AC 636 ms
66,440 KB
testcase_11 AC 597 ms
66,608 KB
testcase_12 AC 602 ms
66,352 KB
testcase_13 AC 561 ms
66,468 KB
testcase_14 AC 1,128 ms
80,280 KB
testcase_15 AC 1,112 ms
77,432 KB
testcase_16 AC 1,127 ms
78,336 KB
testcase_17 AC 1,097 ms
79,804 KB
testcase_18 AC 1,148 ms
79,956 KB
testcase_19 AC 957 ms
78,800 KB
testcase_20 AC 917 ms
77,452 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