結果

問題 No.2536 同値性と充足可能性
ユーザー titiatitia
提出日時 2023-11-12 00:13:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 957 ms / 2,000 ms
コード長 2,203 bytes
コンパイル時間 348 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 55,892 KB
最終ジャッジ日時 2023-11-12 00:14:02
合計ジャッジ時間 9,220 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,240 KB
testcase_01 AC 30 ms
10,240 KB
testcase_02 AC 30 ms
10,240 KB
testcase_03 AC 31 ms
10,240 KB
testcase_04 AC 31 ms
10,240 KB
testcase_05 AC 30 ms
10,240 KB
testcase_06 AC 32 ms
10,240 KB
testcase_07 AC 30 ms
10,240 KB
testcase_08 AC 30 ms
10,240 KB
testcase_09 AC 29 ms
10,240 KB
testcase_10 AC 30 ms
10,240 KB
testcase_11 AC 30 ms
10,240 KB
testcase_12 AC 31 ms
10,240 KB
testcase_13 AC 29 ms
10,240 KB
testcase_14 AC 35 ms
10,240 KB
testcase_15 AC 39 ms
10,240 KB
testcase_16 AC 30 ms
10,240 KB
testcase_17 AC 34 ms
10,240 KB
testcase_18 AC 32 ms
10,240 KB
testcase_19 AC 41 ms
10,240 KB
testcase_20 AC 47 ms
10,496 KB
testcase_21 AC 50 ms
11,136 KB
testcase_22 AC 51 ms
11,136 KB
testcase_23 AC 277 ms
20,864 KB
testcase_24 AC 267 ms
21,248 KB
testcase_25 AC 889 ms
55,072 KB
testcase_26 AC 908 ms
55,456 KB
testcase_27 AC 781 ms
53,316 KB
testcase_28 AC 957 ms
55,892 KB
testcase_29 AC 854 ms
54,888 KB
testcase_30 AC 739 ms
54,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,M=map(int,input().split())

E=[[] for i in range(N+1)]
E2=[[] for i in range(N+1)]

for i in range(M):
    S=input().split()
    x=int(S[0])
    y=int(S[-1])

    if len(S[1])==4:
        E[x].append(y)
        E[y].append(x)
    else:
        E2[x].append(y)
        E2[y].append(x)

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(N+1):
    for to in E[i]:
        Union(i,to)

E3=[[] for i in range(N+1)]

for i in range(N+1):
    for to in E2[i]:
        if find(i)==find(to):
            print("No")
            exit()
        else:
            E3[find(i)].append(find(to))
            E3[find(to)].append(find(i))

ANS=[]
USE=[-1]*(N+1)

for i in range(N+1):
    if find(i)!=i:
        continue
    if USE[i]!=-1:
        continue

    Q=[i]
    USE[i]=0
    zero=Nodes[i]
    one=0
    Z=[i]
    O=[]

    while Q:
        #print(Q)
        x=Q.pop()
        for to in E3[x]:
            if USE[to]==-1:
                USE[to]=USE[x]^1

                if USE[to]==0:
                    zero+=Nodes[to]
                    Z.append(to)
                else:
                    one+=Nodes[to]
                    O.append(to)
                Q.append(to)
            else:
                if USE[to]==USE[x]^1:
                    pass
                else:
                    print("No")
                    exit()
    #print(Z,O)

    if zero>=one:
        for z in Z:
            ANS.append(z)
    else:
        for o in O:
            ANS.append(o)

SET=set(ANS)

LANS=[]
for i in range(1,N+1):
    if find(i) in SET:
        LANS.append(i)

print("Yes")
print(len(LANS))
print(*LANS)
                

    
    
        




        
0