結果

問題 No.2536 同値性と充足可能性
ユーザー miya145592miya145592
提出日時 2023-11-10 23:25:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 571 ms / 2,000 ms
コード長 3,412 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 151,312 KB
最終ジャッジ日時 2023-11-10 23:25:19
合計ジャッジ時間 6,437 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,588 KB
testcase_01 AC 39 ms
53,588 KB
testcase_02 AC 37 ms
53,588 KB
testcase_03 AC 38 ms
53,588 KB
testcase_04 AC 38 ms
53,588 KB
testcase_05 AC 37 ms
53,588 KB
testcase_06 AC 37 ms
53,588 KB
testcase_07 AC 38 ms
53,588 KB
testcase_08 AC 38 ms
53,588 KB
testcase_09 AC 38 ms
53,588 KB
testcase_10 AC 37 ms
53,588 KB
testcase_11 AC 38 ms
53,588 KB
testcase_12 AC 38 ms
53,588 KB
testcase_13 AC 37 ms
53,588 KB
testcase_14 AC 37 ms
53,588 KB
testcase_15 AC 37 ms
53,588 KB
testcase_16 AC 37 ms
53,588 KB
testcase_17 AC 57 ms
68,608 KB
testcase_18 AC 54 ms
66,508 KB
testcase_19 AC 37 ms
53,588 KB
testcase_20 AC 56 ms
70,616 KB
testcase_21 AC 75 ms
77,784 KB
testcase_22 AC 78 ms
77,824 KB
testcase_23 AC 194 ms
97,552 KB
testcase_24 AC 210 ms
97,040 KB
testcase_25 AC 554 ms
139,536 KB
testcase_26 AC 571 ms
149,264 KB
testcase_27 AC 414 ms
139,024 KB
testcase_28 AC 515 ms
151,312 KB
testcase_29 AC 498 ms
145,936 KB
testcase_30 AC 353 ms
147,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n, w=None):
        self.par = [-1]*n
        self.rank = [0]*n
        self.siz = [1]*n
        self.cnt = n
        self.min_node = [i for i in range(n)]
        self.weight = w if w else [1]*n

    def root(self, x):
        if self.par[x] == -1:
            return x
        self.par[x] = self.root(self.par[x])
        return self.par[x]

    def issame(self, x, y):
        return self.root(x) == self.root(y)
            
    def unite(self, x, y):
        px = self.root(x)
        py = self.root(y)
        if px == py:
            return False
        if self.rank[px] < self.rank[py]:
            px, py = py, px
        self.par[py] = px
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        self.siz[px] += self.siz[py]
        self.cnt -= 1
        self.min_node[px] = min(self.min_node[px], self.min_node[py])
        self.weight[px] += self.weight[py]
        return False

    def count(self):
        return self.cnt

    def min(self, x):
        return self.min_node[self.root(x)]

    def getweight(self, x):
        return self.weight[self.root(x)]
    
    def size(self, x):
        return self.siz[self.root(x)]

import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
iEj = [input().split() for _ in range(M)]
UF = UnionFind(N)
for i, E, j in iEj:
    i = int(i)
    j = int(j)
    i-=1
    j-=1
    if E=="<==>":
        UF.unite(i, j)
D = dict()
Z = []
for i in range(N):
    r = UF.root(i)
    if r not in D:
        D[r] = len(D)
        Z.append(r)
G = [[] for _ in range(len(D))]
UF2 = UnionFind(len(D))
for i, E, j in iEj:
    i = int(i)
    j = int(j)
    i-=1
    j-=1
    if E=="<=/=>":
        ri = UF.root(i)
        rj = UF.root(j)
        if ri==rj:
            print("No")
            exit()
        G[D[ri]].append(D[rj])
        G[D[rj]].append(D[ri])
        UF2.unite(D[ri], D[rj])

color = [0] * len(D)

# 頂点を1と-1で塗っていく
def dfs(v, c):
    color[v] = c    # 頂点vをcで塗る
    for i in range(len(G[v])):
        # 隣接している頂点が同じ色ならFalse
        if color[G[v][i]] == c:
            return False
        # 隣接している頂点がまだ塗られていないなら-cで塗る
        if color[G[v][i]] == 0 and not dfs(G[v][i], -c):
            return False
    # すべての頂点を塗れたらTrue
    return True

for i in range(len(D)):
    if color[i] == 0:
        # まだ頂点iが塗られていなければ1で塗る
        if not dfs(i, 1):
            print("No")
            exit()

color_cnt = dict()
color_group = dict()
for i in range(len(D)):
    ri = UF2.root(i)
    if ri not in color_cnt:
        color_cnt[ri] = [0, 0]
        color_group[ri] = [[], []]
    c = color[i]
    if c==1:
        color_cnt[ri][1] += UF.size(Z[i])
        color_group[ri][1].append(Z[i])
    else:
        color_cnt[ri][0] += UF.size(Z[i])
        color_group[ri][0].append(Z[i])

cnt = 0
group = []
for key in color_cnt:
    cnt0, cnt1 = color_cnt[key]
    if cnt0<cnt1:
        cnt+=cnt1
        group.extend(color_group[key][1])
    else:
        cnt+=cnt0
        group.extend(color_group[key][0])

if (N+1)//2 > cnt:
    print("No")
    exit()

group = set(group)
ans = []
for i in range(N):
    ri = UF.root(i)
    if ri in group:
        ans.append(i+1)
print("Yes")
print(len(ans))
print(*ans)
0