結果
問題 | No.1221 木 *= 3 |
ユーザー | anagohirame |
提出日時 | 2020-08-27 20:08:23 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,282 bytes |
コンパイル時間 | 335 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 377,660 KB |
最終ジャッジ日時 | 2024-11-15 16:19:00 |
合計ジャッジ時間 | 26,237 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 43 ms
59,776 KB |
testcase_01 | AC | 42 ms
321,540 KB |
testcase_02 | AC | 43 ms
59,904 KB |
testcase_03 | AC | 42 ms
321,828 KB |
testcase_04 | AC | 42 ms
59,648 KB |
testcase_05 | AC | 44 ms
323,088 KB |
testcase_06 | AC | 43 ms
59,264 KB |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | RE | - |
testcase_10 | RE | - |
testcase_11 | RE | - |
testcase_12 | AC | 561 ms
180,192 KB |
testcase_13 | AC | 1,276 ms
262,344 KB |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | AC | 1,223 ms
222,920 KB |
testcase_17 | TLE | - |
testcase_18 | AC | 1,701 ms
258,256 KB |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
ソースコード
import sys from collections import deque def input(): return sys.stdin.buffer.readline()[:-1] INF = 10**15 class Dinic: def __init__(self, n): self.n = n self.g = [[] for i in range(n)] 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) def add_bidirectional_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) def bfs(self, s, t): self.level = level = [None]*self.n deq = deque([s]) level[s] = 0 g = self.g while deq: v = deq.popleft() lv = level[v] + 1 for w, cap, _ in g[v]: if cap and level[w] is None: level[w] = lv deq.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 return 0 def flow(self, s, t): flow = 0 INF = 10**30 g = self.g while self.bfs(s, t): self.it = list(map(iter, self.g)) f = INF while f: f = self.dfs(s, t, INF) flow += f return flow n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) adj = [[] for _ in range(n)] for i in range(n-1): u, v = map(int, input().split()) adj[u-1].append((v-1, n+2+i)) adj[v-1].append((u-1, n+2+i)) S, T = n, n+1 dnc = Dinic(2*n+1) score = 0 def dfs(i, p, sgn): global dnc, score if a[i] > 0: score += a[i] if sgn: dnc.add_edge(S, i, a[i]) else: dnc.add_edge(i, T, a[i]) elif a[i] < 0: if sgn: dnc.add_edge(i, T, -a[i]) else: dnc.add_edge(S, i, -a[i]) for j, e in adj[i]: if j == p: continue if b[i]+b[j] >= 0: score += b[i]+b[j] if sgn: dnc.add_edge(i, e, INF) dnc.add_edge(j, e, INF) dnc.add_edge(e, T, b[i]+b[j]) else: dnc.add_edge(e, i, INF) dnc.add_edge(e, j, INF) dnc.add_edge(S, e, b[i]+b[j]) dfs(j, i, sgn) else: if sgn: dnc.add_edge(j, i, -b[i]-b[j]) else: dnc.add_edge(i, j, -b[i]-b[j]) dfs(j, i, 1^sgn) dfs(0, -1, 0) min_cut = dnc.flow(S, T) print(score - min_cut)