結果

問題 No.2674 k-Walk on Bipartite
ユーザー ks2mks2m
提出日時 2024-03-15 23:31:01
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,140 bytes
コンパイル時間 2,414 ms
コンパイル使用メモリ 78,704 KB
実行使用メモリ 100,108 KB
最終ジャッジ日時 2024-03-15 23:31:23
合計ジャッジ時間 19,385 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
53,996 KB
testcase_01 AC 58 ms
53,972 KB
testcase_02 AC 56 ms
53,992 KB
testcase_03 AC 57 ms
53,984 KB
testcase_04 AC 58 ms
53,972 KB
testcase_05 AC 59 ms
53,984 KB
testcase_06 AC 58 ms
53,992 KB
testcase_07 AC 799 ms
84,448 KB
testcase_08 AC 989 ms
86,196 KB
testcase_09 AC 790 ms
87,756 KB
testcase_10 AC 1,009 ms
90,924 KB
testcase_11 AC 760 ms
81,736 KB
testcase_12 AC 1,015 ms
84,336 KB
testcase_13 AC 769 ms
82,296 KB
testcase_14 AC 337 ms
75,712 KB
testcase_15 AC 1,105 ms
96,992 KB
testcase_16 AC 1,135 ms
97,216 KB
testcase_17 AC 831 ms
81,400 KB
testcase_18 AC 495 ms
75,592 KB
testcase_19 AC 812 ms
89,584 KB
testcase_20 AC 882 ms
86,888 KB
testcase_21 AC 855 ms
86,160 KB
testcase_22 AC 1,104 ms
100,108 KB
testcase_23 AC 59 ms
53,976 KB
testcase_24 AC 58 ms
53,972 KB
testcase_25 AC 57 ms
53,948 KB
testcase_26 AC 58 ms
54,000 KB
testcase_27 AC 58 ms
52,008 KB
testcase_28 AC 59 ms
53,988 KB
testcase_29 AC 61 ms
53,984 KB
testcase_30 AC 61 ms
53,976 KB
testcase_31 AC 59 ms
53,968 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 58 ms
53,980 KB
testcase_35 AC 60 ms
53,996 KB
testcase_36 AC 61 ms
53,972 KB
testcase_37 AC 59 ms
53,984 KB
testcase_38 AC 61 ms
53,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		sa = br.readLine().split(" ");
		int s = Integer.parseInt(sa[0]) - 1;
		int t = Integer.parseInt(sa[1]) - 1;
		int k = Integer.parseInt(sa[2]);

		Node2[] arr = new Node2[n];
		for (int i = 0; i < n; i++) {
			Node2 o = new Node2();
			arr[i] = o;
		}
		UnionFind uf = new UnionFind(n);
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			int a = Integer.parseInt(sa[0]) - 1;
			int b = Integer.parseInt(sa[1]) - 1;
			arr[a].nexts.add(b);
			arr[b].nexts.add(a);
			uf.union(a, b);
		}
		br.close();

		Queue<Integer> que = new ArrayDeque<>();
		for (int i = 0; i < n; i++) {
			if (arr[i].grp == 0) {
				que.add(i);
				arr[i].grp = 1;
				while (!que.isEmpty()) {
					int cur = que.poll();
					for (int next : arr[cur].nexts) {
						if (arr[next].grp == 0) {
							que.add(next);
							arr[next].grp = 3 - arr[cur].grp;
						} else if (arr[next].grp == arr[cur].grp) {
							// 二部グラフではない
							throw new RuntimeException();
						}
					}
				}
			}
		}

		if (uf.same(s, t)) {
			if ((arr[s].grp == arr[t].grp) ^ (k % 2 == 1)) {
				int[] d = new int[n];
				Arrays.fill(d, -1);
				d[s] = 0;
				que = new ArrayDeque<>();
				que.add(s);
				while (!que.isEmpty()) {
					int cur = que.poll();
					for (int next : arr[cur].nexts) {
						if (d[next] == -1) {
							que.add(next);
							d[next] = d[cur] + 1;
						}
					}
				}
				if (d[t] <= k) {
					if (n == 1) {
						System.out.println("No");
					} else {
						if (d[t] == 0 && k > 0) {
							System.out.println("Unknown");
						} else {
							System.out.println("Yes");
						}
					}
				} else {
					System.out.println("Unknown");
				}
			} else {
				System.out.println("No");
			}
		} else {
			if (n == 2 && k % 2 == 0) {
				System.out.println("No");
			} else {
				System.out.println("Unknown");
			}
		}
	}

	static class Node2 {
		int grp;
		Integer target;
		List<Integer> nexts = new ArrayList<>();
	}

	static class UnionFind {
		int[] parent, size;
		int num = 0; // 連結成分の数

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			num = n;
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
				num--;
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}
}
0