結果

問題 No.1805 Approaching Many Typhoon
ユーザー tentententen
提出日時 2023-08-08 09:26:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 2,963 bytes
コンパイル時間 2,941 ms
コンパイル使用メモリ 91,880 KB
実行使用メモリ 51,972 KB
最終ジャッジ日時 2024-04-26 02:44:21
合計ジャッジ時間 6,501 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,484 KB
testcase_01 AC 53 ms
50,400 KB
testcase_02 AC 54 ms
50,044 KB
testcase_03 AC 54 ms
50,372 KB
testcase_04 AC 55 ms
50,384 KB
testcase_05 AC 55 ms
50,456 KB
testcase_06 AC 54 ms
50,488 KB
testcase_07 AC 55 ms
50,420 KB
testcase_08 AC 55 ms
50,472 KB
testcase_09 AC 54 ms
50,460 KB
testcase_10 AC 54 ms
50,540 KB
testcase_11 AC 56 ms
49,912 KB
testcase_12 AC 53 ms
50,252 KB
testcase_13 AC 53 ms
50,504 KB
testcase_14 AC 53 ms
50,040 KB
testcase_15 AC 52 ms
50,480 KB
testcase_16 AC 54 ms
50,388 KB
testcase_17 AC 54 ms
50,376 KB
testcase_18 AC 54 ms
50,420 KB
testcase_19 AC 54 ms
50,524 KB
testcase_20 AC 53 ms
50,020 KB
testcase_21 AC 54 ms
50,308 KB
testcase_22 AC 53 ms
50,392 KB
testcase_23 AC 54 ms
50,008 KB
testcase_24 AC 55 ms
50,364 KB
testcase_25 AC 55 ms
50,600 KB
testcase_26 AC 55 ms
49,924 KB
testcase_27 AC 53 ms
50,436 KB
testcase_28 AC 86 ms
51,340 KB
testcase_29 AC 84 ms
51,292 KB
testcase_30 AC 88 ms
51,464 KB
testcase_31 AC 95 ms
51,972 KB
testcase_32 AC 56 ms
50,556 KB
testcase_33 AC 57 ms
50,432 KB
testcase_34 AC 62 ms
50,484 KB
testcase_35 AC 83 ms
51,292 KB
testcase_36 AC 80 ms
51,456 KB
testcase_37 AC 53 ms
50,420 KB
testcase_38 AC 54 ms
50,592 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 s = sc.nextInt() - 1;
        int g = sc.nextInt() - 1;
        int[] lefts = new int[m];
        int[] rights = new int[m];
        for (int i = 0; i < m; i++) {
            lefts[i] = sc.nextInt() - 1;
            rights[i] = sc.nextInt() - 1;
        }
        boolean[] atacked = new boolean[n];
        int u = sc.nextInt();
        while (u-- > 0) {
            atacked[sc.nextInt() - 1] = true;
        }
        UnionFindTree uft = new UnionFindTree(n);
        for (int i = 0; i < m; i++) {
            if (!atacked[lefts[i]] && !atacked[rights[i]]) {
                uft.unite(lefts[i], rights[i]);
            }
        }
        if (uft.same(s, g)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
    
    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 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