結果

問題 No.1744 Selfish Spies 1 (à la Princess' Perfectionism)
ユーザー ああいいああいい
提出日時 2022-03-29 23:40:19
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,349 bytes
コンパイル時間 490 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 116,108 KB
最終ジャッジ日時 2024-11-08 22:53:59
合計ジャッジ時間 12,199 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
54,144 KB
testcase_01 AC 43 ms
54,528 KB
testcase_02 AC 44 ms
54,784 KB
testcase_03 AC 42 ms
54,528 KB
testcase_04 AC 42 ms
54,528 KB
testcase_05 AC 44 ms
54,528 KB
testcase_06 AC 41 ms
54,400 KB
testcase_07 AC 43 ms
54,400 KB
testcase_08 AC 42 ms
54,528 KB
testcase_09 AC 61 ms
67,968 KB
testcase_10 AC 65 ms
69,248 KB
testcase_11 AC 81 ms
76,288 KB
testcase_12 AC 132 ms
76,932 KB
testcase_13 AC 85 ms
76,504 KB
testcase_14 AC 152 ms
77,440 KB
testcase_15 AC 132 ms
77,500 KB
testcase_16 AC 347 ms
79,296 KB
testcase_17 AC 561 ms
80,416 KB
testcase_18 AC 345 ms
80,016 KB
testcase_19 AC 65 ms
69,376 KB
testcase_20 AC 67 ms
70,272 KB
testcase_21 AC 84 ms
76,800 KB
testcase_22 AC 135 ms
77,352 KB
testcase_23 AC 90 ms
76,544 KB
testcase_24 AC 543 ms
83,968 KB
testcase_25 AC 170 ms
78,464 KB
testcase_26 AC 367 ms
79,100 KB
testcase_27 AC 481 ms
80,464 KB
testcase_28 TLE -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #


#Dinic法で最大流を求める
#deque のimport が必要
#逆辺追加しなきゃいけないから、
#グラフの構成はadd_edgeで行う
#最大流は flow    メソッドで

from collections import deque
class Dinic:
    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.level = None
        self.progress = None
        self.edge = []
    def add_edge(self,fr,to,cap):
        forward = [to,cap,None]
        forward[2] = backward = [fr,0,forward]
        self.G[fr].append(forward)
        self.G[to].append(backward)
        self.edge.append(forward)
    def add_multi_edge(self,v1,v2,cap1,cap2):
        edge1 = [v2,cap1,None]
        edge1[2] = edge2 = [v1,cap2,edge1]
        self.G[v1].append(edge1)
        self.G[v2].append(edge2)
        self.edge.append(edge1)
    def get_edge(self,i):
        return self.edge[i]
        # i 回目に追加した辺のポインタを返す
        # 0-index, 順辺のみ

    def bfs(self,s,t):
        self.level = level = [None] * self.N
        q = deque([s])
        level[s] = 0
        G = self.G
        while q:
            v = q.popleft()
            lv = level[v] + 1
            for w,cap,_ in G[v]:
                if cap and level[w] is None:
                    level[w] = lv
                    q.append(w)
        return level[t] is not None
    def dfs(self, v, t, f):
        if v == t:
            return f
        level = self.level
        for e in self.it[v]:
            w, cap, rev = e
            if cap and level[v] < level[w]:
                d = self.dfs(w, t, min(f, cap))
                if d:
                    e[1] -= d
                    rev[1] += d
                    return d
                else:
                    pass
            else:
                pass
        return 0

    def flow(self, s, t):
        flow = 0
        INF = 10**9 + 7
        G = self.G
        while self.bfs(s, t):
            *self.it, = map(iter, self.G)
            f = INF
            while f:
                f = self.dfs(s, t, INF)
                flow += f
        return flow
    def min_cut(self,s):
        #最小カットを実現する頂点の分割を与える
        #True  なら source側
        #False なら sink側
        visited = [False for i in range(self.N)]
        q = deque([s])
        while q:
            now = q.popleft()
            visited[now] = True
            for to,cap,_ in self.G[now]:
                if cap and not visited[to]:
                    visited[to] = True
                    q.append(to)
        return visited
    def reset(self):
        for e in self.edge:
            r = e[2]
            e[1] = 1
            r[1] = 0
    def calc(self,i):
        e = self.edge[i]
        r = e[2]
        r[1] = 0
        e[1] = 0

import sys
rr = sys.stdin
N,M,L = map(int,rr.readline().split())
edge = []
for _ in range(L):
    s,t = map(int,rr.readline().split())
    edge.append((s,t))

dinic = Dinic(N + M + 2)
start = 0
end = N + M + 1
for s,t in edge:
    dinic.add_edge(s,t + N,1)
for i in range(1,N+1):
    dinic.add_edge(0,i,1)
for j in range(1,M+1):
    dinic.add_edge(j + N,end,1)
ans = [0] * L
F = dinic.flow(start,end)
l = []
for i in range(L):
    if dinic.get_edge(i)[1] == 1:
        ans[i] = "Yes"
    else:
        l.append(i)
"""
for i in range(L):
    if ans[i] == 1:continue
    dinic = Dinic(N + M + 2)
    for j in range(L):
        if j == i:continue
        s,t = edge[j]
        dinic.add_edge(s,t+N,1)
    for j in range(1,N+1):
        dinic.add_edge(start,j,1)
    for j in range(1,M+1):
        dinic.add_edge(j + N,end,1)
    if dinic.flow(start,end) != F:
        pass
    else:
        ans[i] = 1
"""
for i in l:
    s,t = edge[i]
    #if ans[i] == 1:continue
    #dinic.reset()
    if dinic.get_edge(i)[1] == 1:
        ans[i] = 'Yes'
        continue
    
    dinic.calc(i)
    """
    dinic.calc(L-1+s)
    dinic.calc(L-1+N-1+t)
    """
    e = dinic.get_edge(L-1+s)
    r = e[2]
    e[1] = 1
    r[1] = 0
    e = dinic.get_edge(L-1+N+t)
    r = e[2]
    e[1] = 1
    r[1] = 0
    if dinic.flow(start,end) == 1:
        ans[i] = 'Yes'
    else:
        ans[i] = 'No'
    dinic.get_edge(i)[1] = 1
    dinic.flow(start,end)
print(*ans,sep="\n")
"""
for a in ans:
    if a == 0:
        print('No')
    else:
        print('Yes')
"""
0