結果

問題 No.1640 簡単な色塗り
ユーザー mkawa2mkawa2
提出日時 2021-08-06 22:53:58
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,584 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 262,848 KB
最終ジャッジ日時 2024-06-29 15:59:28
合計ジャッジ時間 30,225 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
59,776 KB
testcase_01 AC 36 ms
54,528 KB
testcase_02 AC 38 ms
54,656 KB
testcase_03 AC 37 ms
54,912 KB
testcase_04 AC 388 ms
188,832 KB
testcase_05 AC 399 ms
188,916 KB
testcase_06 AC 38 ms
54,784 KB
testcase_07 AC 38 ms
54,528 KB
testcase_08 AC 38 ms
54,656 KB
testcase_09 AC 36 ms
54,400 KB
testcase_10 AC 1,195 ms
216,832 KB
testcase_11 AC 1,032 ms
212,224 KB
testcase_12 AC 837 ms
180,492 KB
testcase_13 TLE -
testcase_14 AC 1,917 ms
262,848 KB
testcase_15 AC 514 ms
146,896 KB
testcase_16 AC 667 ms
173,168 KB
testcase_17 AC 1,640 ms
256,020 KB
testcase_18 AC 191 ms
85,736 KB
testcase_19 AC 804 ms
189,568 KB
testcase_20 AC 1,181 ms
227,180 KB
testcase_21 AC 918 ms
205,696 KB
testcase_22 AC 161 ms
83,968 KB
testcase_23 AC 1,277 ms
238,080 KB
testcase_24 AC 358 ms
115,456 KB
testcase_25 AC 790 ms
174,720 KB
testcase_26 AC 1,545 ms
256,112 KB
testcase_27 AC 559 ms
152,960 KB
testcase_28 AC 1,680 ms
261,812 KB
testcase_29 AC 1,550 ms
228,396 KB
testcase_30 AC 487 ms
113,152 KB
testcase_31 TLE -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
07_evil_01.txt -- -
07_evil_02.txt -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 10**16
# md = 998244353
md = 10**9+7

from collections import *

class MaxFlow:
    inf = 10**18

    class E:
        def __init__(self, to, cap):
            self.to = to
            self.cap = cap
            self.rev = None

    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]

    def add_edge(self, fr, to, cap):
        graph = self.graph
        edge = self.E(to, cap)
        edge2 = self.E(fr, 0)
        edge.rev = edge2
        edge2.rev = edge
        graph[fr].append(edge)
        graph[to].append(edge2)

    def bfs(self, s, t):
        level = self.level = [self.n]*self.n
        q = deque([s])
        level[s] = 0
        while q:
            now = q.popleft()
            lw = level[now]+1
            for e in self.graph[now]:
                if e.cap and level[e.to] > lw:
                    level[e.to] = lw
                    if e.to == t:
                        return True
                    q.append(e.to)
        return False

    def dfs(self, s, t, up):
        graph = self.graph
        it = self.it
        level = self.level

        st = deque([t])
        while st:
            v = st[-1]
            if v == s:
                st.pop()
                flow = up
                for w in st:
                    e = graph[w][it[w]].rev
                    flow = min(flow, e.cap)
                for w in st:
                    e = graph[w][it[w]]
                    e.cap += flow
                    e.rev.cap -= flow
                return flow
            lv = level[v]-1
            while it[v] < len(graph[v]):
                e = graph[v][it[v]]
                re = e.rev
                if re.cap == 0 or lv != level[e.to]:
                    it[v] += 1
                    continue
                st.append(e.to)
                break
            if it[v] == len(graph[v]):
                st.pop()
                level[v] = self.n

        return 0

    def flow(self, s, t, flow_limit=inf):
        flow = 0
        while flow < flow_limit and self.bfs(s, t):
            self.it = [0]*self.n
            while flow < flow_limit:
                f = self.dfs(s, t, flow_limit-flow)
                if f == 0:
                    break
                flow += f
        return flow

    def min_cut(self, s):
        visited = [0]*self.n
        q = deque([s])
        while q:
            v = q.pop()
            visited[v] = 1
            for e in self.graph[v]:
                if e.cap and not visited[e.to]:
                    q.append(e.to)
        return visited

n = II()
mf = MaxFlow(2*n+2)
for i in range(n):
    a, b = LI1()
    mf.add_edge(2*n, i, 1)
    mf.add_edge(i, a+n, 1)
    mf.add_edge(i, b+n, 1)
    mf.add_edge(i+n, 2*n+1, 1)

mx = mf.flow(n*2, n*2+1)
ans = [0]*n
if mx == n:
    print("Yes")
    for u in range(n):
        for e in mf.graph[u]:
            if e.to < 2*n and e.cap==0:
                ans[u] = e.to-n+1
                break
    print(*ans, sep="\n")
else:
    print("No")
0