結果

問題 No.1640 簡単な色塗り
ユーザー 👑 KazunKazun
提出日時 2021-08-07 00:04:15
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 4,044 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 131,556 KB
最終ジャッジ日時 2023-09-12 03:29:55
合計ジャッジ時間 26,342 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 102 ms
71,484 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 103 ms
71,700 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 238 ms
83,436 KB
testcase_31 AC 436 ms
107,164 KB
testcase_32 AC 408 ms
103,792 KB
testcase_33 AC 334 ms
96,200 KB
testcase_34 AC 364 ms
99,760 KB
testcase_35 AC 337 ms
95,944 KB
testcase_36 AC 237 ms
82,852 KB
testcase_37 AC 243 ms
84,376 KB
testcase_38 AC 413 ms
104,528 KB
testcase_39 AC 282 ms
89,164 KB
testcase_40 AC 294 ms
88,608 KB
testcase_41 AC 376 ms
100,200 KB
testcase_42 AC 300 ms
89,776 KB
testcase_43 AC 304 ms
92,976 KB
testcase_44 AC 299 ms
92,092 KB
testcase_45 AC 277 ms
88,908 KB
testcase_46 AC 231 ms
83,044 KB
testcase_47 AC 231 ms
82,972 KB
testcase_48 AC 423 ms
105,100 KB
testcase_49 AC 190 ms
81,004 KB
testcase_50 AC 98 ms
71,492 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Coloring_Union_Find():
    def __init__(self,N,f,e=0):
        """0,1,...,N-1を要素として初期化する.

        N:要素数
        f:2変数関数の合成
        e:最初の値
        """
        self.n=N
        self.parents=[-1]*N
        self.data=[e]*N
        self.rank=[0]*N
        self.edge_count=[0]*N
        self.f=f

    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:
            self.edge_count[x]+=1
            return

        self.data[x]=self.data[y]=self.f(self.data[x],self.data[y])

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

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

        self.edge_count[x]+=self.edge_count[y]+1
        self.edge_count[y]=0

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

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

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

    def edges(self, x):
        return self.edge_count[self.find(x)]

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

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

    def set(self,x,c):
        """要素xの属する成分の色をcに変更する.

        x:要素
        c:色
        """
        self.data[self.find(x)]=c
        return

    def look(self,x):
        """要素xの属する成分の色

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

    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 color_list(self):
        return [self.look(x) for x in range(self.n)]

    def color_map(self):
        return {x:self.look(x) for x in self.roots()}

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

    def __repr__(self):
        return self.__str__()

    def __getitem__(self,index):
        return self.data[self.find(index)]

    def __setitem__(self,index,value):
        self.data[self.find(index)]=value
#==================================================
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)]
M=[]
U=Coloring_Union_Find(N+1,max,-1)
for k in range(N):
    A,B=map(int,input().split())

    if A==B:
        E[A].append((B,k))
    else:
        E[A].append((B,k))
        E[B].append((A,k))

    M.append((A,B))
    if U.same(A,B):
        U[A]=k
    U.union(A,B)

G=U.all_group_members()
for g in G:
    if g==0:
        continue
    if U.size(g)!=U.edges(g):
        exit(print("No"))

Ans=[0]*N
L=[-1]*(N+1)

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

    K=U[g]
    A=M[K][0]
    Ans[K]=A
    L[A]=K

    Q=deque([A])
    while Q:
        x=Q.popleft()
        for y,k in E[x]:
            if L[y]==-1 and k!=K:
                Q.append(y)
                Ans[k]=y
                L[y]=k

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