結果
| 問題 | 
                            No.1805 Approaching Many Typhoon
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2024-06-12 16:25:02 | 
| 言語 | Java  (openjdk 23)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 206 ms / 2,000 ms | 
| コード長 | 4,163 bytes | 
| コンパイル時間 | 4,190 ms | 
| コンパイル使用メモリ | 77,320 KB | 
| 実行使用メモリ | 58,324 KB | 
| 最終ジャッジ日時 | 2024-06-12 16:25:19 | 
| 合計ジャッジ時間 | 8,434 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 35 | 
ソースコード
import java.util.*;
import java.lang.*;
import java.io.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int s = sc.nextInt()-1;
        int g = sc.nextInt()-1;
        
        int[] from = new int[m];
        int[] to = new int[m];
        for (int i=0; i<m; i++) {
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            from[i] = f;
            to[i] = t;
        }
        int u = sc.nextInt();
        boolean[] isConnect = new boolean[n];
        Arrays.fill(isConnect , true);
        for (int k=0; k<u; k++) {
            int i = sc.nextInt()-1;
            isConnect[i] = false;
        }
          
        UnionFindTree uft = new UnionFindTree(n);
        for (int i=0; i<m; i++) {
            if(isConnect[from[i]] && isConnect[to[i]]){
                uft.union(from[i] , to[i]);
            }
        }
        System.out.println(uft.same(s,g) ? "Yes" : "No");
    }	
}
//Union find クラス
class UnionFindTree {
	int[] parent; // インデックスとノードを対応させ、そのルートノードのインデックスを格納
	int[] rank; // parentと同様に、木の高さを格納
	int[] size; // 木を構成する、要素の数を格納
	int treeCount; // 木の数
	/**
	 * コンストラクタ
	 * 
	 * @param n : 頂点の数
	 */
	public UnionFindTree(int n) {
		this.parent = new int[n];
		this.rank = new int[n];
		this.size = new int[n];
		this.treeCount = n;
		// 根を作る。
		for (int i = 0; i < n; i++) {
			makeSet(i);
		}
	}
	/**
	 * 根をつくる。最初は、すべて異なる木に属しているものとする。
	 * 
	 * @param i : 頂点の数
	 */
	private void makeSet(int i) {
		parent[i] = i;
		rank[i] = 0; // 集合の高さ
		size[i] = 1; // 木を構成する要素の数
	}
	/**
	 * 要素xが属する木と要素yが属する木を連結する。 木の高さ(ランク)を気にして、低い方に高い方をつなげる。(高い方の木を全体の木とする。)
	 *
	 * @param x : 1つ目の要素(yとは異なる木に属している必要がある。)
	 * @param y : 2つ目の要素(xとは異なる木に属している必要がある。)
	 */
	public void union(int x, int y) {
		int xRoot = find(x);
		int yRoot = find(y);
		int xSize = getSize(xRoot);
		int ySize = getSize(yRoot);
		// xが属する木の方が大きい場合
		if (rank[xRoot] > rank[yRoot]) {
			parent[yRoot] = xRoot; // yの親をxに更新
			size[xRoot] = xSize + ySize; // 木を構成する要素の数を更新
			treeCount--;
		} else if (rank[xRoot] < rank[yRoot]) {
			parent[xRoot] = yRoot;
			size[yRoot] = ySize + xSize;
			treeCount--;
		} else if (xRoot != yRoot) {
			parent[yRoot] = xRoot;
			rank[xRoot]++; // 同じ高さの木がルートの子として着くから大きさ++;
			size[xRoot] = xSize + ySize;
			treeCount--;
		}
	}
	/**
	 * 要素の根を返す。 経路圧縮も同時に行う。(1→3→2となっていて2をfindした際、1→3,2と木の深さを浅くする。)
	 *
	 * @param i
	 * @return 要素iの根 (1→3→2となっていて2をfindした際、1となる。)
	 */
	public int find(int i) {
		if (i != parent[i]) {
			parent[i] = find(parent[i]);
		}
		return parent[i];
	}
	/**
	 * 2つの要素が同じ木に属するかどうかを返す。
	 *
	 * @param x : 1つ目の要素
	 * @param y : 2つ目の要素
	 * @return 同じ木に属していればtrue
	 */
	public boolean same(int x, int y) {
		return find(x) == find(y);
	}
	/**
	 * 木の高さを返す。
	 * 
	 * @param i : 高さを調べたい木の根の番号
	 * @return 木の高さ
	 */
	public int getRank(int i) {
		return rank[i];
	}
	/**
	 * 木を構成する要素の数を返す。
	 * 
	 * @param i : 要素数を調べたい木の根の番号
	 * @return 木を構成する要素の数
	 */
	public int getSize(int i) {
		return size[find(i)];
	}
	/**
	 * 木の数を返す。
	 * 
	 * @return 木の数。
	 */
	public int getTreeCount() {
		return treeCount;
	}
}