結果

問題 No.1640 簡単な色塗り
ユーザー 👑 KazunKazun
提出日時 2021-08-06 22:36:05
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,765 bytes
コンパイル時間 872 ms
コンパイル使用メモリ 87,340 KB
実行使用メモリ 123,764 KB
最終ジャッジ日時 2023-09-12 02:35:42
合計ジャッジ時間 25,759 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 97 ms
71,612 KB
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 AC 98 ms
71,608 KB
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 AC 216 ms
82,132 KB
testcase_31 AC 452 ms
99,776 KB
testcase_32 AC 413 ms
96,984 KB
testcase_33 AC 329 ms
91,908 KB
testcase_34 AC 366 ms
95,176 KB
testcase_35 AC 329 ms
91,868 KB
testcase_36 AC 210 ms
82,100 KB
testcase_37 AC 224 ms
83,112 KB
testcase_38 AC 406 ms
98,164 KB
testcase_39 AC 271 ms
87,240 KB
testcase_40 AC 273 ms
87,180 KB
testcase_41 AC 372 ms
94,568 KB
testcase_42 AC 282 ms
87,084 KB
testcase_43 AC 288 ms
89,808 KB
testcase_44 AC 288 ms
88,208 KB
testcase_45 AC 262 ms
86,528 KB
testcase_46 AC 216 ms
82,336 KB
testcase_47 AC 192 ms
80,648 KB
testcase_48 AC 424 ms
99,104 KB
testcase_49 AC 175 ms
79,492 KB
testcase_50 AC 95 ms
71,820 KB
testcase_51 RE -
testcase_52 RE -
testcase_53 RE -
07_evil_01.txt RE -
07_evil_02.txt RE -
権限があれば一括ダウンロードができます

ソースコード

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())

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

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

Edge=defaultdict(int)
T=[0]*N
for a in range(1,N+1):
    for t in E[a]:
        b=t[0]; k=t[1]
        if T[k]==0:
            T[k]+=1
            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"))

print(1/0)

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

    #サイクル辺を見つける
    flag=False
    for a in G[g]:
        for t in E[a]:
            b=t[0]; k=t[1]
            if T[k]==0:
                T[k]=1
                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