結果

問題 No.2674 k-Walk on Bipartite
ユーザー hato336hato336
提出日時 2024-03-15 22:54:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 713 ms / 2,000 ms
コード長 2,865 bytes
コンパイル時間 227 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 111,220 KB
最終ジャッジ日時 2024-03-15 22:54:15
合計ジャッジ時間 13,303 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
88,288 KB
testcase_01 AC 148 ms
88,288 KB
testcase_02 AC 149 ms
88,288 KB
testcase_03 AC 136 ms
84,704 KB
testcase_04 AC 137 ms
84,704 KB
testcase_05 AC 135 ms
84,704 KB
testcase_06 AC 134 ms
84,704 KB
testcase_07 AC 474 ms
109,792 KB
testcase_08 AC 582 ms
105,056 KB
testcase_09 AC 375 ms
106,208 KB
testcase_10 AC 639 ms
108,896 KB
testcase_11 AC 474 ms
102,624 KB
testcase_12 AC 606 ms
106,336 KB
testcase_13 AC 410 ms
103,904 KB
testcase_14 AC 208 ms
99,424 KB
testcase_15 AC 713 ms
110,816 KB
testcase_16 AC 537 ms
111,220 KB
testcase_17 AC 574 ms
104,800 KB
testcase_18 AC 290 ms
98,272 KB
testcase_19 AC 458 ms
105,056 KB
testcase_20 AC 420 ms
108,000 KB
testcase_21 AC 565 ms
106,720 KB
testcase_22 AC 588 ms
110,432 KB
testcase_23 AC 131 ms
84,704 KB
testcase_24 AC 133 ms
84,704 KB
testcase_25 AC 134 ms
84,704 KB
testcase_26 AC 130 ms
84,704 KB
testcase_27 AC 141 ms
84,704 KB
testcase_28 AC 150 ms
84,704 KB
testcase_29 AC 135 ms
84,704 KB
testcase_30 AC 132 ms
84,704 KB
testcase_31 AC 135 ms
84,704 KB
testcase_32 AC 134 ms
84,704 KB
testcase_33 AC 133 ms
84,704 KB
testcase_34 AC 136 ms
84,704 KB
testcase_35 AC 144 ms
88,288 KB
testcase_36 AC 147 ms
84,704 KB
testcase_37 AC 132 ms
84,704 KB
testcase_38 AC 146 ms
88,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items())
input = sys.stdin.readline
n,m = map(int,input().split())
s,t,k = map(int,input().split())
if n == 1:
    if k == 0:
        print('Yes')
    else:
        print('No')
    exit()
if n == 2:
    if s != t and k % 2 == 0:
        print('No')
        exit()
    if s == t and k % 2 == 1:
        print('No')
        exit()

   



uf = UnionFind(n)
edge = [[] for i in range(n)]
for i in range(m):
    u,v = map(int,input().split())
    u-=1
    v-=1
    edge[u].append(v)
    edge[v].append(u)
    uf.union(u,v)
s -= 1
t -= 1
if s == t:
    if len(edge[s]) >= 1 and k % 2 == 0:
        print('Yes')
        exit()
    elif len(edge[s]) == 0 and k % 2 == 0:
        print('Unknown')
        exit()
    elif k % 2 == 1:
        print('No')
        exit()
if uf.same(s,t) == False:
    print('Unknown')
    exit()
start = s
d = collections.deque()
dist = [10**18 for i in range(n)]
color = [0 for i in range(n)]
d.append(start)
dist[start] = 0
while d:
    now = d.popleft()
    for i in edge[now]:
        color[i] = color[now] ^ 1
        if dist[i] > dist[now] + 1:
            dist[i] = dist[now] + 1
            d.append(i)
if color[s] == color[t]:
    if k % 2 == 0 and dist[t] <= k:
        print('Yes')
        exit()
    if k % 2 != 0:
        print('No')
        exit()
if color[s] != color[t]:
    if k % 2 == 1 and dist[t] <= k:
        print('Yes')
        exit()
    if k % 2 != 1:
        print('No')
        exit()
print('Unknown')
0