結果

問題 No.1640 簡単な色塗り
ユーザー 👑 KazunKazun
提出日時 2021-08-06 22:24:40
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,607 bytes
コンパイル時間 564 ms
コンパイル使用メモリ 87,200 KB
実行使用メモリ 116,336 KB
最終ジャッジ日時 2023-09-12 02:24:34
合計ジャッジ時間 27,622 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 97 ms
71,684 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 94 ms
71,936 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 231 ms
82,316 KB
testcase_31 AC 456 ms
98,296 KB
testcase_32 AC 417 ms
97,044 KB
testcase_33 AC 334 ms
91,548 KB
testcase_34 AC 386 ms
94,728 KB
testcase_35 AC 343 ms
91,252 KB
testcase_36 AC 227 ms
82,064 KB
testcase_37 AC 242 ms
83,360 KB
testcase_38 AC 434 ms
98,576 KB
testcase_39 AC 288 ms
87,044 KB
testcase_40 AC 290 ms
86,304 KB
testcase_41 AC 393 ms
95,296 KB
testcase_42 AC 303 ms
86,992 KB
testcase_43 AC 309 ms
89,604 KB
testcase_44 AC 305 ms
88,824 KB
testcase_45 AC 275 ms
86,660 KB
testcase_46 AC 226 ms
82,484 KB
testcase_47 AC 205 ms
80,792 KB
testcase_48 AC 434 ms
98,904 KB
testcase_49 AC 183 ms
80,020 KB
testcase_50 AC 96 ms
71,748 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union_Find():
    __slots__=["n","parents","rank"]
    def __init__(self,N):
        """0,1,...,N-1を要素として初期化する.

        N:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    def find(self, x):
        """要素xの属している族を調べる.

        x:要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """要素x,yを同一視する.

        x,y:要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        if self.rank[x]<self.rank[y]:
            x,y=y,x

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

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        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):
        """全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def refresh(self):
        for i in range(self.n):
            _=self.find(i)

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()
#==================================================
from collections import defaultdict
from collections import deque
import sys

input=sys.stdin.readline
write=sys.stdout.write

N=int(input())
E=[[] for _ in range(N+1)]
for k in range(N):
    A,B=map(int,input().split())
    E[A].append((B,k))
    E[B].append((A,k))

    X=min(A,B); Y=max(A,B)

#判定パート
U=Union_Find(N+1)
for a in range(1,N+1):
    for b,_ in E[a]:
        U.union(a,b)

Edge=defaultdict(int)
for a in range(1,N+1):
    for b,_ in E[a]:
        if a<b:
            Edge[U.find(a)]+=1

G=U.all_group_members()
for g in G:
    if g==0:
        continue

    if U.size(g)-1==Edge[g]:
        exit(print("No"))

#構築パート
U=Union_Find(N+1)
Ans=[-1]*N
L=[0]*(N+1)
for g in G:
    if g==0: continue

    #サイクル辺を見つける
    flag=False
    for a in G[g]:
        for b,k in E[a]:
            if a<b:
                if U.same(a,b):
                    alpha=a
                    beta =b
                    flag=True
                    K=k
                    break
                else:
                    U.union(a,b)
        if flag:
            break

    Ans[K]=alpha
    Q=deque([alpha]); L[alpha]=1
    while Q:
        x=Q.popleft()
        for y,k in E[x]:
            if L[y]==0:
                if k==K:
                    continue
                L[y]=1
                Q.append(y)
                Ans[k]=y

write("\n".join(map(str,Ans)))


0