結果

問題 No.1640 簡単な色塗り
ユーザー kept1994kept1994
提出日時 2021-09-08 03:24:25
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 6,168 bytes
コンパイル時間 937 ms
コンパイル使用メモリ 87,252 KB
実行使用メモリ 125,664 KB
最終ジャッジ日時 2023-09-12 04:08:11
合計ジャッジ時間 26,049 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,624 KB
testcase_01 AC 94 ms
71,640 KB
testcase_02 WA -
testcase_03 AC 95 ms
71,560 KB
testcase_04 AC 293 ms
125,664 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 94 ms
71,560 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 256 ms
82,084 KB
testcase_31 AC 487 ms
101,428 KB
testcase_32 AC 432 ms
96,624 KB
testcase_33 AC 367 ms
92,568 KB
testcase_34 AC 401 ms
96,640 KB
testcase_35 AC 374 ms
92,700 KB
testcase_36 AC 243 ms
82,280 KB
testcase_37 AC 255 ms
82,820 KB
testcase_38 AC 451 ms
98,400 KB
testcase_39 AC 311 ms
86,484 KB
testcase_40 AC 318 ms
86,164 KB
testcase_41 AC 420 ms
94,448 KB
testcase_42 AC 340 ms
87,872 KB
testcase_43 AC 348 ms
89,732 KB
testcase_44 AC 334 ms
88,660 KB
testcase_45 AC 311 ms
87,288 KB
testcase_46 AC 241 ms
82,312 KB
testcase_47 AC 221 ms
80,680 KB
testcase_48 AC 458 ms
100,728 KB
testcase_49 AC 186 ms
80,200 KB
testcase_50 AC 94 ms
71,652 KB
testcase_51 WA -
testcase_52 WA -
testcase_53 WA -
07_evil_01.txt WA -
07_evil_02.txt WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
from collections import defaultdict
from collections import deque

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    """ 要素xの値を取得。"""
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    """ 2つの要素の併合。"""
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

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

    """ 要素xの属する集合の要素数を取得。"""
    def size(self, x):
        return -self.parents[self.find(x)]

    """ 2つの要素が同一の集合に属するか。"""
    def same(self, x, y):
        return self.find(x) == self.find(y)

    """ 要素xと同一の集合の要素を全取得。
    計算量 : O(N)
    """
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    """ 各集合の根を全取得。
    計算量 : O(N)
    """
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    """ 集合の個数を取得。
    計算量 : O(N)
    """
    def group_count(self):
        return len(self.roots())

    """ 全集合の要素一覧を取得。
    計算量 : O(N)
    """
    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

def main():
    N = int(input())
    uf = UnionFind(N)
    G = [[] for _ in range(N)]
    for nn in range(N):
        a, b = map(lambda i: int(i) - 1, input().split())
        G[a].append((b, nn)) # 答えは順番に出力するので保持しておく必要がある。
        if a != b:
            G[b].append((a, nn))
        uf.union(a, b)

    groups = uf.all_group_members()
    for _, linked in groups.items():
        link_num = 0
        for i in linked:
            link_num = len(G[i])
        if link_num != len(linked):
            print("No")
            return
        

    uu = UnionFind(N)
    ans = [-1] * N
    L=[0]*(N+1)
    T=[0]*N

    # print(G)
    # print(groups)
    # 各部分連結Cの根をみる
    for root in uf.roots():
        flag = False
        # その根に属する全ての頂点linkedについて、
        for linked in groups[root]:
            # 頂点linkedから移動可能なnodeと入力のindexととり、
            for node, index in G[linked]:
                # print(node, index, T)
                # 
                if T[index]==0:
                    # 
                    T[index]=1
                    # 移動元lonkedと移動先nodeが既に連結しているならそれはサイクルなのでフラグを立てる。
                    if uu.same(linked,node):
                        alpha=linked
                        flag=True
                        K = index
                        break
                    else:
                        uu.union(linked, node)
            # print(flag)
            if flag:
                break


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

    print("Yes")
    for i in ans:
        print(i + 1)
    # print(ans)
    return
    if True:
        is_cycle=True
        root=-1
        cycle_cnt=0
        print(linked)
        
        for point in linked:
            for link_node, num in G[point]:
                if link_node == point: # 始点と終点が同じなので自己ループ
                    cycle_cnt += 1
                    is_cycle = False
                    ans[num] = point + 1
                    root = point
            
        if cycle_cnt > 1:
            print("No")
            return
        
        if is_cycle:
            next_point=-1
            que=deque()
            que.append([linked[0],-1])
        
        print(is_cycle)
        print(ans)
        print(root)
        
        sys.setrecursionlimit(10 ** 9)
        # seen = [False] * N
        firstOrder = []
        lastOrder = []
        def dfs(nodes: "list[list[int]]", now: int):
            firstOrder.append(now)
            seen[now] = 1
            for next, _ in nodes[now]:
                if seen[next]:
                    continue
                dfs(nodes, next)
            lastOrder.append(now)
        # while que:
        #     q=que.pop()
        #     for j in G[q[0]]:
        #         if j[1]==q[1]:
        #             continue
        #         if seen[j[0]]:
        #             root=j[0]
        #             next_point=q[0]
        #             ans[j[1]]=root+1
        #             que.clear()
        #             break
        #         que.append([j[0],j[1]])
        #     seen[q[0]]=1
        
        dfs(G, linked[0])
        print(firstOrder)
        print(lastOrder)
        print(seen)
        if next_point==-1:
            print("No")
            return

        dq=deque()
        dq.append(root)

        while dq:
            q=dq.pop()

            for j in G[q]:
                if ans[j[1]]==-1:
                    ans[j[1]]=j[0]+1
                    dq.append(j[0])
    
    for i in ans:
        if i==-1:
            print("No")
            return
    
    print("Yes")
    for i in ans:
        print(i)
    return
    #     E = 0
    #     for i in nodes:
    #         E = len(G[i])
    #     if E != len(nodes):
    #         print("No")
    #         return
        
    # ans = 0
    # print(ans)
    

    return

if __name__ == '__main__':
    main()
0