s_id
string
p_id
string
u_id
string
date
string
language
string
original_language
string
filename_ext
string
status
string
cpu_time
string
memory
string
code_size
string
code
string
error
string
stdout
string
s705330673
p02368
u400336986
1487593289
Python
Python3
py
Runtime Error
280
8016
801
def dfs(u, g, stack): visited[u] = True for v in g[u]: if not visited[v]: dfs(v, g, stack) stack.append(u) return stack nv, ne = map(int, input().split(' ')) adj, tps = ([[] for _ in range(nv)] for _ in range(2)) for _ in range(ne): s, t = map(int, input().split(' ')) adj[s].append(t) tps[t].append(s) visited = [False] * nv stack = [] for i in range(nv): if not visited[i]: stack = dfs(i, adj, stack) visited = [False] * nv comps = [] while stack: i = stack.pop() if not visited[i]: comp = dfs(i, tps, []) comps.append(comp) nv = int(input()) for _ in range(nv): u, v = map(int, input().split(' ')) ans = 0 for comp in comps: if u in comp and v in comp: ans = 1 print(ans)
Traceback (most recent call last): File "/tmp/tmpgjbr82nu/tmpee9ja6d1.py", line 9, in <module> nv, ne = map(int, input().split(' ')) ^^^^^^^ EOFError: EOF when reading a line
s966539963
p02368
u855775311
1499781277
Python
Python3
py
Runtime Error
140
8264
1032
def main(): nvertices, nedges = map(int, input().split()) Adj = [[] for i in range(nvertices)] Adjrev = [[] for i in range(nvertices)] for i in range(nedges): s, t = map(int, input().split()) Adj[s].append(t) Adjrev[t].append(s) component_ids = [-1] * nvertices id = 0 for u in range(nvertices): if component_ids[u] != -1: continue s1 = set() s2 = set() visited1 = [0] * nvertices visited2 = [0] * nvertices dfs(u, Adj, visited1, s1) dfs(u, Adjrev, visited2, s2) for v in s1 & s2: component_ids[v] = id id += 1 Q = int(input()) for i in range(Q): s, t = map(int, input().split()) if component_ids[s] == component_ids[t]: print(1) else: print(0) def dfs(u, Adj, visited, s): visited[u] = 1 s.add(u) for v in Adj[u]: if visited[v]: continue dfs(v, Adj, visited, s) return s main()
Traceback (most recent call last): File "/tmp/tmpdpkb4bym/tmp2b5mdagz.py", line 44, in <module> main() File "/tmp/tmpdpkb4bym/tmp2b5mdagz.py", line 2, in main nvertices, nedges = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s169080503
p02368
u855775311
1499920890
Python
Python3
py
Runtime Error
0
0
1241
def main(): nvertices, nedges = map(int, input().split()) Adj = [[] for i in range(nvertices)] Adjrev = [[] for i in range(nvertices)] for i in range(nedges): s, t = map(int, input().split()) Adj[s].append(t) Adjrev[t].append(s) ordering = [] visited = [False] * nvertices for u in range(nvertices): if not visited[u]: dfs1(u, Adj, visited, ordering) ordering.reverse() component_ids = [-1] * nvertices id = 0 visited = [False] * nvertices for u in ordering: if visited[u]: continue dfs2(u, Adjrev, id, visited, component_ids) id += 1 Q = int(input()) for i in range(Q): s, t = map(int, input().split()) if component_ids[s] == component_ids[t]: print(1) else: print(0) def dfs1(u, Adj, visited, ordering): visited[u] = True for v in Adj[u]: if visited[v]: continue dfs1(v, Adj, visited, ordering) ordering.append(u) def dfs2(u, Adj, id, visited, component_ids): visited[u] = True component_ids[u] = id for v in range(Adj[u]): if not visited[v]: dfs2(v, Adj, id, visited) main()
Traceback (most recent call last): File "/tmp/tmpp4nxyhjo/tmp7n0xnqnj.py", line 51, in <module> main() File "/tmp/tmpp4nxyhjo/tmp7n0xnqnj.py", line 2, in main nvertices, nedges = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s325399044
p02368
u855775311
1499920982
Python
Python3
py
Runtime Error
130
8176
1249
def main(): nvertices, nedges = map(int, input().split()) Adj = [[] for i in range(nvertices)] Adjrev = [[] for i in range(nvertices)] for i in range(nedges): s, t = map(int, input().split()) Adj[s].append(t) Adjrev[t].append(s) ordering = [] visited = [False] * nvertices for u in range(nvertices): if not visited[u]: dfs1(u, Adj, visited, ordering) ordering.reverse() component_ids = [-1] * nvertices id = 0 visited = [False] * nvertices for u in ordering: if visited[u]: continue dfs2(u, Adjrev, id, visited, component_ids) id += 1 Q = int(input()) for i in range(Q): s, t = map(int, input().split()) if component_ids[s] == component_ids[t]: print(1) else: print(0) def dfs1(u, Adj, visited, ordering): visited[u] = True for v in Adj[u]: if visited[v]: continue dfs1(v, Adj, visited, ordering) ordering.append(u) def dfs2(u, Adj, id, visited, component_ids): visited[u] = True component_ids[u] = id for v in Adj[u]: if not visited[v]: dfs2(v, Adj, id, visited, component_ids) main()
Traceback (most recent call last): File "/tmp/tmp_h3y3u6w/tmp35hv6_9r.py", line 51, in <module> main() File "/tmp/tmp_h3y3u6w/tmp35hv6_9r.py", line 2, in main nvertices, nedges = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s526226443
p02368
u855775311
1499921156
Python
Python3
py
Runtime Error
120
8024
1299
import sys def main(): sys.setrecursionlimit(int(1e4)) nvertices, nedges = map(int, input().split()) Adj = [[] for i in range(nvertices)] Adjrev = [[] for i in range(nvertices)] for i in range(nedges): s, t = map(int, input().split()) Adj[s].append(t) Adjrev[t].append(s) ordering = [] visited = [False] * nvertices for u in range(nvertices): if not visited[u]: dfs1(u, Adj, visited, ordering) ordering.reverse() component_ids = [-1] * nvertices id = 0 visited = [False] * nvertices for u in ordering: if visited[u]: continue dfs2(u, Adjrev, id, visited, component_ids) id += 1 Q = int(input()) for i in range(Q): s, t = map(int, input().split()) if component_ids[s] == component_ids[t]: print(1) else: print(0) def dfs1(u, Adj, visited, ordering): visited[u] = True for v in Adj[u]: if visited[v]: continue dfs1(v, Adj, visited, ordering) ordering.append(u) def dfs2(u, Adj, id, visited, component_ids): visited[u] = True component_ids[u] = id for v in Adj[u]: if not visited[v]: dfs2(v, Adj, id, visited, component_ids) main()
Traceback (most recent call last): File "/tmp/tmpglxdnf4m/tmpxxfmwl03.py", line 56, in <module> main() File "/tmp/tmpglxdnf4m/tmpxxfmwl03.py", line 6, in main nvertices, nedges = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s106263419
p02368
u855775311
1501749583
Python
Python3
py
Runtime Error
110
7924
1114
def main(): nvertices, nedges = map(int, input().split()) Adj = [[] for i in range(nvertices)] AdjRev = [[] for i in range(nvertices)] for i in range(nedges): u, v = map(int, input().split()) Adj[u].append(v) AdjRev[v].append(u) lst = [] visited = [False] * nvertices for u in range(nvertices): if not visited[u]: dfs_first(u, Adj, visited, lst) lst.reverse() ids = [-1] * nvertices visited = [False] * nvertices id = 0 for u in lst: if not visited[u]: dfs_second(u, AdjRev, visited, id, ids) id += 1 Q = int(input()) for i in range(Q): u, v = map(int, input().split()) print(1 if ids[u] == ids[v] else 0) def dfs_first(u, Adj, visited, lst): visited[u] = True for v in Adj[u]: if not visited[v]: dfs_first(v, Adj, visited, lst) lst.append(u) def dfs_second(u, Adj, visited, id, ids): ids[u] = id visited[u] = True for v in Adj[u]: if not visited[v]: dfs_second(v, Adj, visited, id, ids) main()
Traceback (most recent call last): File "/tmp/tmp6jgbs6g9/tmpvw050i13.py", line 46, in <module> main() File "/tmp/tmp6jgbs6g9/tmpvw050i13.py", line 2, in main nvertices, nedges = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s419177994
p02368
u798803522
1508158537
Python
Python3
py
Runtime Error
150
8544
1430
from collections import defaultdict def counter(): cnt = 0 while True: yield cnt cnt += 1 def dfs(here, visited, track, count, connect): visited |= {here} for c in connect[here]: if c not in visited: dfs(c, visited, track, count, connect) track[here] = next(count) def kosaraju(here, visited, track, count, connect): visited |= {here} for c in connect[here]: if c not in visited: kosaraju(c, visited, track, count, connect) track[here] = count vertices, edges = (int(n) for n in input().split(" ")) connect = defaultdict(list) reversed_connect = defaultdict(list) for _ in range(edges): v1, v2 = (int(n) for n in input().split(" ")) connect[v1].append(v2) reversed_connect[v2].append(v1) visited = set() track_gen = counter() track = [-1 for n in range(vertices)] for v in range(vertices): if v not in visited: dfs(v, visited, track, track_gen, connect) visited = set() track_gen = counter() strongly_con = [-1 for n in range(vertices)] for v in range(vertices - 1 ,- 1, -1): here = track.index(v) if here not in visited: num = next(track_gen) kosaraju(here, visited, strongly_con, num, reversed_connect) q_num = int(input()) for _ in range(q_num): v1, v2 = (int(n) for n in input().split(" ")) if strongly_con[v1] == strongly_con[v2]: print(1) else: print(0)
Traceback (most recent call last): File "/tmp/tmp1c8jl3gw/tmpv0ax1kkd.py", line 23, in <module> vertices, edges = (int(n) for n in input().split(" ")) ^^^^^^^ EOFError: EOF when reading a line
s159978713
p02368
u798803522
1508158584
Python
Python3
py
Runtime Error
0
0
1460
from collections import defaultdict sys.setrecursionlimit(100000) def counter(): cnt = 0 while True: yield cnt cnt += 1 def dfs(here, visited, track, count, connect): visited |= {here} for c in connect[here]: if c not in visited: dfs(c, visited, track, count, connect) track[here] = next(count) def kosaraju(here, visited, track, count, connect): visited |= {here} for c in connect[here]: if c not in visited: kosaraju(c, visited, track, count, connect) track[here] = count vertices, edges = (int(n) for n in input().split(" ")) connect = defaultdict(list) reversed_connect = defaultdict(list) for _ in range(edges): v1, v2 = (int(n) for n in input().split(" ")) connect[v1].append(v2) reversed_connect[v2].append(v1) visited = set() track_gen = counter() track = [-1 for n in range(vertices)] for v in range(vertices): if v not in visited: dfs(v, visited, track, track_gen, connect) visited = set() track_gen = counter() strongly_con = [-1 for n in range(vertices)] for v in range(vertices - 1 ,- 1, -1): here = track.index(v) if here not in visited: num = next(track_gen) kosaraju(here, visited, strongly_con, num, reversed_connect) q_num = int(input()) for _ in range(q_num): v1, v2 = (int(n) for n in input().split(" ")) if strongly_con[v1] == strongly_con[v2]: print(1) else: print(0)
Traceback (most recent call last): File "/tmp/tmpap9ajtjr/tmpr13lcvda.py", line 2, in <module> sys.setrecursionlimit(100000) ^^^ NameError: name 'sys' is not defined
s447351435
p02368
u352394527
1528130011
Python
Python3
py
Runtime Error
200
6056
1222
""" 強連結成分分解 """ def dfs(v, visited, edges, order): visited[v] = True for to in edges[v]: if not visited[to]: dfs(to, visited, edges, order) order.append(v) def search_strongly_connection(v, visited, reverse_edges, parent): visited[v] = True for to in reverse_edges[v]: if not visited[to]: parent[to] = v search_strongly_connection(to, visited, reverse_edges, parent) def find(parent, x): if parent[x] == x: return x tmp = find(parent, parent[x]) parent[x] = tmp return tmp v_num , e_num = map(int, input().split()) edges = [[] for _ in range(v_num)] reverse_edges = [[] for _ in range(v_num)] for _ in range(e_num): s, t = map(int, input().split()) edges[s].append(t) reverse_edges[t].append(s) order = [] visited = [False] * v_num for v in range(v_num): if not visited[v]: dfs(v, visited, edges, order) order.reverse() visited = [False] * v_num parent = [i for i in range(v_num)] for v in order: if not visited[v]: search_strongly_connection(v, visited, reverse_edges, parent) q_num = int(input()) for _ in range(q_num): u, v = map(int, input().split()) if find(parent, u) == find(parent, v): print(1) else: print(0)
Traceback (most recent call last): File "/tmp/tmphtouf1s6/tmpstey6168.py", line 27, in <module> v_num , e_num = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s577430336
p02369
u797673668
1461582089
Python
Python3
py
Runtime Error
20
7712
779
import sys sys.setrecursionlimit(200000) def dfs(i): global edges, visited, root, duplicated visited[i] = True for edge in edges[i]: if visited[edge]: if edge == root: return True duplicated.add(edge) continue if dfs(edge): return True n, m = map(int, input().split()) edges = [set() for _ in range(n)] for _ in range(m): s, t = map(int, input().split()) edges[s].add(t) visited = [False] * n root = 0 duplicated = set() for i in range(n): if not visited[i]: root = i if dfs(i): print(1) break else: for i in duplicated: root = i if dfs(i): print(1) break else: print(0)
Traceback (most recent call last): File "/tmp/tmpm7pfy_fx/tmp31iqfqbe.py", line 19, in <module> n, m = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s710746056
p02369
u603049633
1497844394
Python
Python3
py
Runtime Error
30
7792
722
import heapq as pq inf = float("inf") n,e = map(int, input().split()) M = [[] for i in range(n)] L = list(map(int, range(n))) for i in range(e): s,t = map(int, input().split()) M[s].append(t) if t in L: L.remove(t) def dijkstra(s): color = [0] * n H =[] pq.heappush(H, s) while len(H) != 0: u1 = pq.heappop(H) for v1 in M[u1]: if v1 and color[v1] == 1: print("1") return False else: color[u1] = 1 pq.heappush(H, v1) return True if not L: print("1") for i in L: flag = False if dijkstra(i): flag = dijkstra(i) else: break if flag: print("0")
Traceback (most recent call last): File "/tmp/tmpk4_9zxmv/tmp7q90m8yv.py", line 3, in <module> n,e = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s874350260
p02369
u893844544
1523855949
Python
Python3
py
Runtime Error
0
0
426
nv,ne = [int(i) for i in input().split()] a = [] for i in range(nv): a.append([]) for i in range(ne): s,t = [int(j) for j in input().split()] a[s].append(t) done = [] flag = 0 def put_forword(index): global f if index in done: return done.append(index) for s in a[index]: if s == 0: f = 1 return put_forword(s) put_forword(0) print(f)
Traceback (most recent call last): File "/tmp/tmp2yndt1t_/tmpsfyf208v.py", line 4, in <module> nv,ne = [int(i) for i in input().split()] ^^^^^^^ EOFError: EOF when reading a line
s588145984
p02369
u893844544
1523858360
Python
Python3
py
Runtime Error
0
0
429
nv,ne = [int(i) for i in input().split()] g = [[] for i in range(nv)] for i in range(ne): s,t = [int(j) for j in input().split()] g[s].append(t) done = [] f = 0 def put_forword(index): global f if index in done: return done.append(index) for i in g[index]: if i == 0 and done.count() == nv: f = 1 return put_forword(i) put_forword(0) print(f)
File "/tmp/tmpu4oj553a/tmp7wl466av.py", line 23 if i == 0 and done.count() == nv: ^ SyntaxError: invalid non-printable character U+3000
s531085172
p02370
u011621222
1540444477
Python
Python3
py
Runtime Error
20
5664
442
# -*- coding: utf-8 -*- # 文字列の入力 import math V,E = map(int ,input().split()) X = [] for i in range(E): s_i, t_i = map(int , input().split()) if(s_i in X): if(t_i in X): t = X.index(t_i) s = X.index(s_i) if(s > t): X.remove(t_i) X.insert(s,t_i) else: X.append(t_i) else: if(t_i in X): t = X.index(t_i) X.insert(t, s_i) else: X.append(s_i) X.append(t_i) for i in range(V): print(X[i])
Traceback (most recent call last): File "/tmp/tmpg5br6hgv/tmpsil827km.py", line 6, in <module> V,E = map(int ,input().split()) ^^^^^^^ EOFError: EOF when reading a line
s189501960
p02370
u967553879
1556259677
Python
Python3
py
Runtime Error
0
0
1009
def topologicalSortBfs(n): for i in range(n): color[i] = WHITE for i in range(0, n): if indeg[i] == 0 and color[i] == WHITE: bfs(i) def bfs(s): Q.appendleft(s) color[s] = GRAY while Q: u = Q.popleft() out.append(u) for v in G[u]: indeg[v] -= 1 if indeg[v] == 0 and color[v] == WHITE: color[v] = GRAY Q.append(v) def topologicalSortDfs(n): for i in range(n): color[i] = WHITE for s in range(0, n): if color[s] == WHITE: dfs(s) def dfs(u): color[u] = GRAY for v in G[u]: if color[v] == WHITE: dfs(v) out.appendleft(u) n, e = map(int, input().split()) G = {} for i in range(n): G[i] = [] indeg = [0] * n for i in range(e): s, t = map(int, input().split()) indeg[t] += 1 G[s].append(t) color = [None] * n out = deque([]) WHITE = 0 GRAY = 1 Q = deque([]) topologicalSortDfs(n) print(*out)
Traceback (most recent call last): File "/tmp/tmpjeysyo_2/tmpya8quucu.py", line 42, in <module> n, e = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s311039860
p02370
u099826363
1556717798
Python
Python3
py
Runtime Error
40
6840
701
from sys import exit import queue V,E = [int(n) for n in input().split()] ans = [] # N = int(input()) graph = [[] for _ in range(V)] indeg = [0]*E yet = [True]*E def bfs(s): q = queue.Queue() q.put(s) while(not q.empty()): u = q.get() ans.append(u) yet[u]=False for v in graph[u]: indeg[v]-=1 if(indeg[v]==0): q.put(v) for i in range(E): s, t = [int(n) for n in input().split()] graph[s].append(t) indeg[t]+=1 for i in range(E): if(indeg[i]==0 and yet[i]): bfs(i) for a in ans: print(a) # a = [int(input()) for _ in range(N)] # S = str(input()) # L = len(S) # T = str(input()) # exit()
Traceback (most recent call last): File "/tmp/tmpu6ufdgjy/tmpc__605xt.py", line 3, in <module> V,E = [int(n) for n in input().split()] ^^^^^^^ EOFError: EOF when reading a line
s327024893
p02370
u099826363
1556717874
Python
Python3
py
Runtime Error
40
6840
701
from sys import exit import queue V,E = [int(n) for n in input().split()] ans = [] # N = int(input()) graph = [[] for _ in range(E)] indeg = [0]*E yet = [True]*E def bfs(s): q = queue.Queue() q.put(s) while(not q.empty()): u = q.get() ans.append(u) yet[u]=False for v in graph[u]: indeg[v]-=1 if(indeg[v]==0): q.put(v) for i in range(E): s, t = [int(n) for n in input().split()] graph[s].append(t) indeg[t]+=1 for i in range(E): if(indeg[i]==0 and yet[i]): bfs(i) for a in ans: print(a) # a = [int(input()) for _ in range(N)] # S = str(input()) # L = len(S) # T = str(input()) # exit()
Traceback (most recent call last): File "/tmp/tmp206x559e/tmp8ko_az7m.py", line 3, in <module> V,E = [int(n) for n in input().split()] ^^^^^^^ EOFError: EOF when reading a line
s959517610
p02370
u067972379
1559212067
Python
Python3
py
Runtime Error
0
0
1069
from collections import deque, defaultdict def topological_sort(V, E): ''' Kahn's Algorithm (O(|V| + |E|) time) Input: V = [0, 1, ..., N-1]: a list of vertices of the digraph E: the adjacency list of the digraph (dict) Output: If the input digraph is acyclic, then return a topological sorting of the digraph. Else, return None. ''' indeg = {v: 0 for v in V} for ends in E.values(): for v in ends: indeg[v] += 1 q = deque([v for v in V if indeg[v] == 0]) top_sorted = [] while q: v = q.popleft() top_sorted.append(v) for u in E[v]: indeg[u] -= 1 if indeg[u] == 0: q.append(u) if len(top_sorted) == len(V): # The input digraph is acyclic. return top_sorted else: # There is a directed cycle in the digraph. return None N, M = map(int, input().split()) E = defaultdict(list) for _ in range(M): s, t = map(int, input().split()) E[s].append(t) print(*topological_sort(V, E), sep='\n')
Traceback (most recent call last): File "/tmp/tmptmohfzg7/tmpuewfmgkm.py", line 33, in <module> N, M = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s900224205
p02370
u894114233
1461422655
Python
Python
py
Runtime Error
10
6800
697
from collections import deque def Topologicalsort(): for i in xrange(v): if indeg[i]==0 and color[i]==0: bfs(i) def bfs(s): color[s]=2 q.append(s) while len(q)>0: i=q.popleft() ans.append(i) for j in xrange(v): if m[i][j]!=-1 and color[j]==0 : indeg[j]-=1 if indeg[j]==0: q.append(m[i][j]) color[j]=1 v,e=map(int,raw_input().split()) m=[[-1]*v for i in xrange(v)] indeg=[0]*v color=[0]*v start=[] ans=deque() q=deque() for i in xrange(v): s,t=map(int,raw_input().split()) m[s][t]=t indeg[t]+=1 Topologicalsort() for i in ans: print(i)
Traceback (most recent call last): File "/tmp/tmpzd53sieu/tmp1p3ww9o7.py", line 21, in <module> v,e=map(int,raw_input().split()) ^^^^^^^^^ NameError: name 'raw_input' is not defined
s080724592
p02370
u894114233
1461422906
Python
Python
py
Runtime Error
10
6800
688
from collections import deque def Topologicalsort(): for i in xrange(v): if indeg[i]==0 and color[i]==0: bfs(i) def bfs(s): color[s]=2 q.append(s) while len(q)>0: i=q.popleft() ans.append(i) for j in xrange(v): if m[i][j]!=-1: indeg[j]-=1 if indeg[j]==0 and color[j]==0: q.append(m[i][j]) color[j]=1 v,e=map(int,raw_input().split()) m=[[-1]*v for i in xrange(v)] indeg=[0]*v color=[0]*v ans=deque() q=deque() for i in xrange(v): s,t=map(int,raw_input().split()) m[s][t]=t indeg[t]+=1 Topologicalsort() for i in ans: print(i)
Traceback (most recent call last): File "/tmp/tmpa3f9_752/tmpofj4vrbu.py", line 21, in <module> v,e=map(int,raw_input().split()) ^^^^^^^^^ NameError: name 'raw_input' is not defined
s164985054
p02370
u462831976
1490603572
Python
Python3
py
Runtime Error
40
8676
830
# -*- coding: utf-8 -*- import sys import os import pprint from queue import Queue """Topological Sort""" #fd = os.open('GRL_4_B.txt', os.O_RDONLY) #os.dup2(fd, sys.stdin.fileno()) V, E = list(map(int, input().split())) G = [[] for i in range(V)] indig = [0] * V # ??\?¬???° for i in range(V): s, t = list(map(int, input().split())) G[s].append(t) indig[t] += 1 q = Queue() out = [] def bfs(node_index): q.put(node_index) while not q.empty(): u = q.get() out.append(u) # u?????????????????§u?????????indig???????????? for index in G[u]: indig[index] -= 1 if indig[index] == 0: q.put(index) # bfs????????? indig_copy = indig[:] for i in range(V): if indig_copy[i] == 0: bfs(node_index=i) for v in out: print(v)
Traceback (most recent call last): File "/tmp/tmpctqegmeo/tmp4lwdd7m1.py", line 13, in <module> V, E = list(map(int, input().split())) ^^^^^^^ EOFError: EOF when reading a line
s492553537
p02370
u426534722
1500821701
Python
Python3
py
Runtime Error
0
0
469
from sys import stdin from collections import deque V, E = map(int, stdin.readline().split()) adj_list = [[] for _ in [] * V] is_visited = [False] * V for _ in [] * E: s, t = map(int, stdin.readline().split()) adj_list[s].append(t) out = deque() def dfs(u): is_visited[u] = True for v in adj_list[u]: if not is_visited[v]: dfs(v) out.appendleft(u) for s in range(V): if not is_visited[s]: dfs(s) print(*out, sep='\n')
Traceback (most recent call last): File "/tmp/tmpe_0zq4vc/tmpulzjxza_.py", line 3, in <module> V, E = map(int, stdin.readline().split()) ^^^^ ValueError: not enough values to unpack (expected 2, got 0)
s162708980
p02370
u426534722
1500821728
Python
Python3
py
Runtime Error
0
0
470
from sys import stdin from collections import deque V, E = map(int, stdin.readline().split()) adj_list = [[] for _ in [] * V] is_visited = [False] * V for _ in [0] * E: s, t = map(int, stdin.readline().split()) adj_list[s].append(t) out = deque() def dfs(u): is_visited[u] = True for v in adj_list[u]: if not is_visited[v]: dfs(v) out.appendleft(u) for s in range(V): if not is_visited[s]: dfs(s) print(*out, sep='\n')
Traceback (most recent call last): File "/tmp/tmp44oz__1p/tmpr4_iquvm.py", line 3, in <module> V, E = map(int, stdin.readline().split()) ^^^^ ValueError: not enough values to unpack (expected 2, got 0)
s907833658
p02370
u792145349
1528975773
Python
Python3
py
Runtime Error
0
0
516
N, M = map(int, input().split()) G = [[] for i in range(N)] for i in range(M): s, t = map(int, input().split()) G[s].append(t) def topological_sort(g): topological_list = [] visited = set() def dfs(n): for e in g[n]: if e not in visited: dfs(e) topological_list.append(n) visited.add(n) for i in range(len(g)): if i not in visited: dfs(i) return topological_list[::-1] print(*topological_sort(G), sep="\n")
File "/tmp/tmpr5yjo3wp/tmp68s2c6mj.py", line 8 topological_list = [] ^ SyntaxError: invalid non-printable character U+3000
s257941396
p02371
u138546245
1534991840
Python
Python3
py
Runtime Error
20
6248
1710
#!/usr/bin/env python3 # GRL_5_A: Tree - Diameter of a Tree class Edge: __slots__ = ('v', 'w') def __init__(self, v, w): self.v = v self.w = w def either(self): return self.v def other(self, v): if v == self.v: return self.w else: return self.v class WeightedEdge(Edge): __slots__ = ('v', 'w', 'weight') def __init__(self, v, w, weight): super().__init__(v, w) self.weight = weight class Graph: def __init__(self, v): self.v = v self._edges = [[] for _ in range(v)] def add(self, e): self._edges[e.v].append(e) self._edges[e.w].append(e) def adj(self, v): return self._edges[v] def edges(self): for es in self._edges: for e in es: yield e def diameter(graph): def dfs(s): def _dfs(v, weight): if not visited[v]: visited[v] = True weights[v] = weight for e in graph.adj(v): w = e.other(v) if not visited[w]: _dfs(w, weight + e.weight) visited = [False] * graph.v weights = [0] * graph.v _dfs(s, 0) return weights v0 = 0 ws0 = dfs(v0) v1 = max((wg, v) for v, wg in enumerate(ws0))[1] ws1 = dfs(v1) v2 = max((wg, v) for v, wg in enumerate(ws1))[1] return max(ws0[v2], ws1[v2]) def run(): n = int(input()) g = Graph(n) for _ in range(n-1): s, t, w = [int(i) for i in input().split()] g.add(WeightedEdge(s, t, w)) print(diameter(g)) if __name__ == '__main__': run()
Traceback (most recent call last): File "/tmp/tmp4jvuk4ti/tmpo67z5mza.py", line 85, in <module> run() File "/tmp/tmp4jvuk4ti/tmpo67z5mza.py", line 74, in run n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s723042716
p02371
u943441430
1559280055
Python
Python3
py
Runtime Error
20
5668
934
from heapq import heappush, heappop INF = 10000000000 def dijkstra(r, v, dic): dp = [] q = [] dp = [INF] * v dp[r] = 0 heappush(q, (0, r)) while q: cost, vtx = heappop(q) if dp[vtx] < cost: continue for vtx1, cost1 in dic[vtx]: if cost + cost1 < dp[vtx1]: dp[vtx1] = cost + cost1 heappush(q, (cost + cost1, vtx1)) return dp dp = [] line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] diam = [] dp = dijkstra(0, n, edge) i = [j for j, x in enumerate(dp) if x == max(dp)] dp = dijkstra(i[0], n, edge) diam = max(dp[i] if dp[i] != INF else 0 for i in range(0, n)) print(diam)
Traceback (most recent call last): File "/tmp/tmp6o7gifv4/tmpxfg7ls4r.py", line 22, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s636298060
p02371
u943441430
1559280089
Python
Python3
py
Runtime Error
20
5664
924
from heapq import heappush, heappop INF = 10000000000 def dijkstra(r, v, dic): dp = [] q = [] dp = [INF] * v dp[r] = 0 heappush(q, (0, r)) while q: cost, vtx = heappop(q) if dp[vtx] < cost: continue for vtx1, cost1 in dic[vtx]: if cost + cost1 < dp[vtx1]: dp[vtx1] = cost + cost1 heappush(q, (cost + cost1, vtx1)) return dp dp = [] line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] dp = dijkstra(0, n, edge) i = [j for j, x in enumerate(dp) if x == max(dp)] dp = dijkstra(i[0], n, edge) diam = max(dp[i] if dp[i] != INF else 0 for i in range(0, n)) print(diam)
Traceback (most recent call last): File "/tmp/tmpbonokvq4/tmpofk15duv.py", line 22, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s900104419
p02371
u943441430
1559282570
Python
Python3
py
Runtime Error
20
6220
665
def solve(prev, r, dic): d = 0 t = r if r in dic: for vtx, cost in dic[r]: if vtx == prev: continue d1, t1 = solve(r, vtx, dic) d1 += cost if d < d1: d = d1 t = t1 return d, t line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] r1, t1 = solve(-1, 0, edge) r2, t2 = solve(-1, t1, edge) print(r2)
Traceback (most recent call last): File "/tmp/tmp_g1ug_1n/tmpp2xtv553.py", line 15, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s896418709
p02371
u943441430
1559282654
Python
Python3
py
Runtime Error
20
6220
665
def solve(prev, r, dic): d = 0 t = r if r in dic: for vtx, cost in dic[r]: if vtx == prev: continue d1, t1 = solve(r, vtx, dic) d1 += cost if d < d1: d = d1 t = t1 return d, t line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] r1, t1 = solve(-1, 0, edge) r2, t2 = solve(-1, t1, edge) print(r2)
Traceback (most recent call last): File "/tmp/tmp4ludajos/tmpimn592gg.py", line 15, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s015717608
p02371
u943441430
1559285284
Python
Python3
py
Runtime Error
290
16788
707
import sys sys.setrecursionlimit(10000) def solve(prev, r, dic): d = 0 t = r if r in dic: for vtx, cost in dic[r]: if vtx == prev: continue d1, t1 = solve(r, vtx, dic) d1 += cost if d < d1: d = d1 t = t1 return d, t line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] r1, t1 = solve(-1, 0, edge) r2, t2 = solve(-1, t1, edge) print(r2)
Traceback (most recent call last): File "/tmp/tmp_q_psvyk/tmp96jv71_a.py", line 19, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s948763989
p02371
u943441430
1559285310
Python
Python3
py
Runtime Error
820
43928
707
import sys sys.setrecursionlimit(50000) def solve(prev, r, dic): d = 0 t = r if r in dic: for vtx, cost in dic[r]: if vtx == prev: continue d1, t1 = solve(r, vtx, dic) d1 += cost if d < d1: d = d1 t = t1 return d, t line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] r1, t1 = solve(-1, 0, edge) r2, t2 = solve(-1, t1, edge) print(r2)
Traceback (most recent call last): File "/tmp/tmpzo1vg_0j/tmpyi3kfxkj.py", line 19, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s205742847
p02371
u943441430
1559285339
Python
Python3
py
Runtime Error
750
43924
708
import sys sys.setrecursionlimit(100000) def solve(prev, r, dic): d = 0 t = r if r in dic: for vtx, cost in dic[r]: if vtx == prev: continue d1, t1 = solve(r, vtx, dic) d1 += cost if d < d1: d = d1 t = t1 return d, t line = input() n = int(line) edge = {} for _ in range(0, n - 1): line = input() s, t, d = list(map(int, line.split())) if s not in edge: edge[s] = [[t, d]] else: edge[s] += [[t, d]] if t not in edge: edge[t] = [[s, d]] else: edge[t] += [[s, d]] r1, t1 = solve(-1, 0, edge) r2, t2 = solve(-1, t1, edge) print(r2)
Traceback (most recent call last): File "/tmp/tmpzf2j6cuz/tmp8coogtzb.py", line 19, in <module> line = input() ^^^^^^^ EOFError: EOF when reading a line
s751108930
p02371
u567380442
1428235656
Python
Python3
py
Runtime Error
40
6756
847
def postorder(g): parent_stack = [None] dfs_stack = [(0, None)] while dfs_stack: u, prev = dfs_stack.pop() dfs_stack.extend((t, u) for d, t in g[u]) while parent_stack[-1] != prev: yield parent_stack.pop() parent_stack.append(u) while parent_stack[-1] is not None: yield parent_stack.pop() from sys import stdin from collections import defaultdict readline = stdin.readline n = int(readline()) g = defaultdict(list) for _ in range(n - 1): s, t, d = map(int, readline().split()) g[s].append((d, t)) diameter = [] dp = [0] * n for i in postorder(g): if len(g[i]) == 0: continue candidate = sorted(d + dp[t] for d, t in g[i]) dp[i] = candidate[-1] diameter.append(dp[i] if len(g[i]) == 1 else sum(candidate[-2:])) print(max(diameter))
Traceback (most recent call last): File "/tmp/tmpog3wayur/tmpfkplinui.py", line 18, in <module> n = int(readline()) ^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s832427108
p02371
u567380442
1428236297
Python
Python3
py
Runtime Error
30
6764
945
def root(g): return set(g.keys()).difference({y for x in g.values() for y in x}).pop() def postorder(g): parent_stack = [None] dfs_stack = [(root(g), None)] while dfs_stack: u, prev = dfs_stack.pop() dfs_stack.extend((t, u) for d, t in g[u]) while parent_stack[-1] != prev: yield parent_stack.pop() parent_stack.append(u) while parent_stack[-1] is not None: yield parent_stack.pop() from sys import stdin from collections import defaultdict readline = stdin.readline n = int(readline()) g = defaultdict(list) for _ in range(n - 1): s, t, d = map(int, readline().split()) g[s].append((d, t)) diameter = [] dp = [0] * n for i in postorder(g): if len(g[i]) == 0: continue candidate = sorted(d + dp[t] for d, t in g[i]) dp[i] = candidate[-1] diameter.append(dp[i] if len(g[i]) == 1 else sum(candidate[-2:])) print(max(diameter))
Traceback (most recent call last): File "/tmp/tmp0z4a5crc/tmp1aa7f14o.py", line 21, in <module> n = int(readline()) ^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s849577171
p02371
u072053884
1477707059
Python
Python3
py
Runtime Error
230
15936
553
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[None] * n for i in range(n)] for l_i in f_i: s, t, w = map(int, l_i.split()) edges[s][t] = w edges[t][s] = w # Tree Walk and Output distance = [0] * n def tree_walk(u, d, p): for v, e in enumerate(edges[u]): if e != None and v != p: c_d = d + e distance[v] = c_d tree_walk(v, c_d, u) tree_walk(0, 0, None) end_point = distance.index(max(distance)) tree_walk(end_point, 0, None) print(max(distance))
Traceback (most recent call last): File "/tmp/tmpmyfen8r2/tmpegy7vwdb.py", line 7, in <module> n = int(f_i.readline()) ^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s793960812
p02371
u072053884
1477759074
Python
Python3
py
Runtime Error
30
8028
1470
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[None] * n for i in range(n)] for l_i in f_i: s, t, w = map(int, l_i.split()) edges[s][t] = w edges[t][s] = w # dfs tree walk 1 import collections path = collections.deque() path.append(0) distance = 0 max_distance = 0 unvisited = [False] + [True] * (n - 1) while True: u = path[-1] adj_e = edges[u] for v, e in enumerate(adj_e): if unvisited[v] and e != None: path.append(v) distance += e unvisited[v] = False if max_distance < distance: max_distance = distance end_point = v break if u == path[-1]: path.pop() if path: distance -= edges[u][path[-1]] else: break # Measurement of diameter(dfs tree walk 2) path.append(end_point) distance = 0 diameter = 0 unvisited = [True] * n unvisited[end_point] = False while True: u = path[-1] adj_e = edges[u] for v, e in enumerate(adj_e): if unvisited[v] and e != None: path.append(v) distance += e unvisited[v] = False if diameter < distance: diameter = distance end_point = v break if u == path[-1]: path.pop() if path: distance -= edges[u][path[-1]] else: break # output print(diameter)
Traceback (most recent call last): File "/tmp/tmpm3gg__lh/tmphpxkl021.py", line 7, in <module> n = int(f_i.readline()) ^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s307578639
p02371
u072053884
1477830715
Python
Python3
py
Runtime Error
150
20612
566
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[] for i in range(n)] for l_i in f_i: s, t, w = map(int, l_i.split()) edges[s].append([t, w]) edges[t].append([s, w]) # Tree Walk and Output distance = [0] * n sys.setrecursionlimit(10000) def tree_walk(u, d, p): for v, w in edges[u]: if v != p: c_d = d + w distance[v] = c_d tree_walk(v, c_d, u) tree_walk(0, 0, None) end_point = distance.index(max(distance)) tree_walk(end_point, 0, None) print(max(distance))
Traceback (most recent call last): File "/tmp/tmpcfi2vfbc/tmp3bxsn7ok.py", line 7, in <module> n = int(f_i.readline()) ^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s361206091
p02371
u072053884
1477830786
Python
Python3
py
Runtime Error
500
94944
567
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[] for i in range(n)] for l_i in f_i: s, t, w = map(int, l_i.split()) edges[s].append([t, w]) edges[t].append([s, w]) # Tree Walk and Output distance = [0] * n sys.setrecursionlimit(100000) def tree_walk(u, d, p): for v, w in edges[u]: if v != p: c_d = d + w distance[v] = c_d tree_walk(v, c_d, u) tree_walk(0, 0, None) end_point = distance.index(max(distance)) tree_walk(end_point, 0, None) print(max(distance))
Traceback (most recent call last): File "/tmp/tmp59f8dcog/tmp0ffm68e_.py", line 7, in <module> n = int(f_i.readline()) ^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s378573805
p02371
u072053884
1477830822
Python
Python3
py
Runtime Error
510
94916
567
# Acceptance of input import sys f_i = sys.stdin n = int(f_i.readline()) edges = [[] for i in range(n)] for l_i in f_i: s, t, w = map(int, l_i.split()) edges[s].append([t, w]) edges[t].append([s, w]) # Tree Walk and Output distance = [0] * n sys.setrecursionlimit(200000) def tree_walk(u, d, p): for v, w in edges[u]: if v != p: c_d = d + w distance[v] = c_d tree_walk(v, c_d, u) tree_walk(0, 0, None) end_point = distance.index(max(distance)) tree_walk(end_point, 0, None) print(max(distance))
Traceback (most recent call last): File "/tmp/tmp03f2h6na/tmpkfgmf7ux.py", line 7, in <module> n = int(f_i.readline()) ^^^^^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s725953730
p02371
u260980560
1500111153
Python
Python
py
Runtime Error
0
0
591
while 1: n = input() if n == 0: break G = [[] for i in xrange(n)] for i in xrange(n-1): s, t, w = map(int, raw_input().split()) G[s].append((t, w)) G[t].append((s, w)) def dfs(s, prev): if prev != -1 and len(G[s]) == 1: return (0, s) res = (0, -1) for t, w in G[s]: if t == prev: continue cost, v = dfs(t, s) res = max(res, (cost + w, v)) return res co, t = dfs(0, -1) if t != -1: print dfs(t, -1)[0] else: print 0
File "/tmp/tmpssw16_43/tmp8c95j8ok.py", line 23 print dfs(t, -1)[0] ^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s830985785
p02371
u260980560
1500111176
Python
Python
py
Runtime Error
0
0
631
import sys sys.setrecursionlimit(10**6) while 1: n = input() if n == 0: break G = [[] for i in xrange(n)] for i in xrange(n-1): s, t, w = map(int, raw_input().split()) G[s].append((t, w)) G[t].append((s, w)) def dfs(s, prev): if prev != -1 and len(G[s]) == 1: return (0, s) res = (0, -1) for t, w in G[s]: if t == prev: continue cost, v = dfs(t, s) res = max(res, (cost + w, v)) return res co, t = dfs(0, -1) if t != -1: print dfs(t, -1)[0] else: print 0
File "/tmp/tmp3x4l1fvp/tmphhhm41n2.py", line 25 print dfs(t, -1)[0] ^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s285166837
p02371
u260980560
1500111221
Python
Python
py
Runtime Error
470
91060
509
import sys sys.setrecursionlimit(10**6) n = input() G = [[] for i in xrange(n)] for i in xrange(n-1): s, t, w = map(int, raw_input().split()) G[s].append((t, w)) G[t].append((s, w)) def dfs(s, prev): if prev != -1 and len(G[s]) == 1: return (0, s) res = (0, -1) for t, w in G[s]: if t == prev: continue cost, v = dfs(t, s) res = max(res, (cost + w, v)) return res co, t = dfs(0, -1) if t != -1: print dfs(t, -1)[0] else: print 0
File "/tmp/tmpv7i2db4_/tmpp5xlxbu1.py", line 22 print dfs(t, -1)[0] ^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s656967478
p02371
u260980560
1500111591
Python
Python
py
Runtime Error
490
95028
474
import sys sys.setrecursionlimit(10**6) n = input() G = [[] for i in xrange(n)] for i in xrange(n-1): s, t, w = map(int, raw_input().split()) G[s].append((t, w)) G[t].append((s, w)) def dfs(s, prev): if prev != -1 and len(G[s]) == 1: return (0, s) res = (0, s) for t, w in G[s]: if t == prev: continue cost, v = dfs(t, s) res = max(res, (cost + w, v)) return res co, t = dfs(0, -1) print dfs(t, -1)[0]
File "/tmp/tmp_plq0y0t/tmpzg7_fgoz.py", line 21 print dfs(t, -1)[0] ^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s319285156
p02371
u426534722
1501104919
Python
Python3
py
Runtime Error
0
0
718
import sys readline = sys.stdin.readline from collections import deque from math import isinf INF = float("inf") sys.setrecursionlimit(200000) n = int(readline()) G = [[] for _ in range(n)] for _ in [0] * (n - 1): s, t, w = map(int, readline().split()) G[s].append([t, w]) G[t].append([s, w]) def bfs(s, D): dq = deque([s]) D = [INF] * n D[s] = 0 while dq: u = dq.popleft() for i in range(len(G[u])): e = G[u][i] if isinf(D[e[0]]): D[e[0]] = D[u] + e[1] dq.append(e[0]) return D D = bfs(0, D) D = bfs(max([i for i in range(n) if not isinf(D[i]) and 0 < D[i]])) print(max(D[i] for i in range(n) if not isinf(D[i])))
Traceback (most recent call last): File "/tmp/tmp7cnohsx2/tmpq9zqklw_.py", line 7, in <module> n = int(readline()) ^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s350380047
p02371
u798803522
1508662368
Python
Python3
py
Runtime Error
60
8412
868
from collections import defaultdict def dfs(source, used, all_weight, connect): max_weight = all_weight max_source = source used[source] = 1 for target, weight in connect[source]: if not used[target]: now_weight = all_weight + weight this_source, this_weight = dfs(target, used, now_weight, connect) if max_weight < this_weight: max_weight = this_weight max_source = this_source return [max_source, max_weight] vertice = int(input()) connect = defaultdict(list) for _ in range(vertice - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight]) connect[v2].append([v1, weight]) answer = 0 start_v = 0 for i in range(2): used = [0 for n in range(vertice)] start_v, answer = dfs(start_v, used, 0, connect) print(answer)
Traceback (most recent call last): File "/tmp/tmpeggsgf74/tmpqc990l7c.py", line 16, in <module> vertice = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s906142384
p02371
u798803522
1508662414
Python
Python3
py
Runtime Error
830
54372
909
from collections import defaultdict import sys sys.setrecursionlimit(100000) def dfs(source, used, all_weight, connect): max_weight = all_weight max_source = source used[source] = 1 for target, weight in connect[source]: if not used[target]: now_weight = all_weight + weight this_source, this_weight = dfs(target, used, now_weight, connect) if max_weight < this_weight: max_weight = this_weight max_source = this_source return [max_source, max_weight] vertice = int(input()) connect = defaultdict(list) for _ in range(vertice - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight]) connect[v2].append([v1, weight]) answer = 0 start_v = 0 for i in range(2): used = [0 for n in range(vertice)] start_v, answer = dfs(start_v, used, 0, connect) print(answer)
Traceback (most recent call last): File "/tmp/tmpy2s8a874/tmpbpxi5aqv.py", line 18, in <module> vertice = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s366159095
p02372
u138546245
1535095693
Python
Python3
py
Runtime Error
20
5684
3004
#!/usr/bin/env python3 # GRL_5_B: Tree - Height of a Tree import bisect class Edge: __slots__ = ('v', 'w') def __init__(self, v, w): self.v = v self.w = w def either(self): return self.v def other(self, v): if v == self.v: return self.w else: return self.v class WeightedEdge(Edge): __slots__ = ('v', 'w', 'weight') def __init__(self, v, w, weight): super().__init__(v, w) self.weight = weight class Graph: def __init__(self, v): self.v = v self._edges = [[] for _ in range(v)] def add(self, e): self._edges[e.v].append(e) self._edges[e.w].append(e) def adj(self, v): return self._edges[v] def edges(self): for es in self._edges: for e in es: yield e def heights(graph): def select_root(): leaves = [v for v in range(graph.v) if len(graph.adj(v)) == 1] visited = [False] * graph.v while len(leaves) > 2: v, *leaves = leaves if not visited[v]: visited[v] = True for e in graph.adj(v): w = e.other(v) if not visited[w]: visited[w] = True leaves.append(w) return leaves.pop() def dfs(s): visited = [False] * graph.v stack = [s] while stack: v = stack.pop() if not visited[v]: visited[v] = True stack.append(v) for e in graph.adj(v): w = e.other(v) if not visited[w]: edge_to[w] = e stack.append(w) else: e = edge_to[v] if e is not None: w = e.other(v) bisect.insort(dists[w], (dists[v][-1][0] + e.weight, v)) def _heights(s): hs = [0] * graph.v visited = [False] * graph.v stack = [s] while stack: v = stack.pop() if not visited[v]: visited[v] = True hs[v] = dists[v][-1][0] for e in graph.adj(v): w = e.other(v) if not visited[w]: for x, xv in reversed(dists[v]): if xv != w: bisect.insort(dists[w], (x + e.weight, v)) break stack.append(w) return hs root = select_root() edge_to = [None] * graph.v dists = [[(0, i)] for i in range(graph.v)] dfs(root) return _heights(root) def run(): n = int(input()) g = Graph(n) for _ in range(n-1): s, t, w = [int(i) for i in input().split()] g.add(WeightedEdge(s, t, w)) for w in heights(g): print(w) if __name__ == '__main__': run()
Traceback (most recent call last): File "/tmp/tmpgrdhrqng/tmpjnu90afc.py", line 123, in <module> run() File "/tmp/tmpgrdhrqng/tmpjnu90afc.py", line 111, in run n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s522342851
p02372
u567380442
1428286644
Python
Python3
py
Runtime Error
30
6800
1400
from sys import stdin from collections import defaultdict readline = stdin.readline def main(): n = int(readline()) g = defaultdict(list) for _ in range(n - 1): s, t, d = map(int, readline().split()) g[s].append([d, t]) dp = defaultdict(dict) r = root(g) for i in postorder(g, r): if len(g[i]) == 0: continue for di, ti in g[i]: dp[i][ti] = di if g[ti]: dp[i][ti] += max(dp[ti].values()) height = [None] * n for i in preorder(g, r): height[i] = max(dp[i].values()) for d, t in g[i]: dp[t][i] = d if 1 < len(dp[i]): dp[t][i] += max(v for k, v in dp[i].items() if k != t) print('\n'.join(map(str, height))) def root(g): return set(g.keys()).difference({t for x in g.values() for d, t in x}).pop() def preorder(g, r): dfs_stack = [r] while dfs_stack: u = dfs_stack.pop() yield(u) dfs_stack.extend(t for d, t in g[u]) def postorder(g, r): parent_stack = [] dfs_stack = [(r, None)] while dfs_stack: u, prev = dfs_stack.pop() dfs_stack.extend((t, u) for d, t in g[u]) while parent_stack and parent_stack[-1] != prev: yield parent_stack.pop() parent_stack.append(u) while parent_stack: yield parent_stack.pop() main()
Traceback (most recent call last): File "/tmp/tmphe_3hh4a/tmpwxslstve.py", line 56, in <module> main() File "/tmp/tmphe_3hh4a/tmpwxslstve.py", line 8, in main n = int(readline()) ^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s750439590
p02372
u797673668
1461663787
Python
Python3
py
Runtime Error
30
7752
1027
import sys from operator import itemgetter sys.setrecursionlimit(200000) def dfs(u, prev): global post_order, pre_order pre_order.append((u, prev)) for w, t in edges[u]: if t != prev: dfs(t, u) post_order.append(u) n = int(input()) edges = [set() for _ in range(n)] for _ in range(n - 1): s, t, w = map(int, input().split()) edges[s].add((w, t)) edges[t].add((w, s)) post_order, pre_order = [], [] farthest = [{} for _ in range(n)] height = [0] * n dfs(0, None) for i in post_order: for w, t in edges[i]: farthest[i][t] = w + max((d for tt, d in farthest[t].items() if tt != i), default=0) for i, parent in pre_order: sorted_farthest = sorted(farthest[i].items(), key=itemgetter(1)) max_t, max_d = sorted_farthest.pop() height[i] = max_d for w, t in edges[i]: if t == parent: continue farthest[t][i] = w + ((max_d if t != max_t else sorted_farthest[-1][1]) if sorted_farthest else 0) for h in height: print(h)
Traceback (most recent call last): File "/tmp/tmp27bc8kf2/tmps121ofln.py", line 17, in <module> n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s899231018
p02372
u798803522
1508670418
Python
Python3
py
Runtime Error
0
0
1041
from collections import defaultdict import sys sys.setrecursionlimit(200000) def dfs(source, edge_index, connect): if connect[source][edge_index][2] >= 0: return connect[source][edge_index][2] this_edge = connect[source][edge_index] this_edge[2] = this_edge[1] dest = this_edge[0] for i, (next_dest, weight, all_weight) in enumerate(connect[dest]): if next_dest != source: this_edge[2] = max(this_edge[2], dfs(dest, i, connect) + this_edge[1]) return this_edge[2] v_num = int(input()) connect = defaultdict(list) for _ in range(v_num - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight, -1]) connect[v2].append([v1, weight, -1]) for v in range(vertice): for i, (next_dest, weight, all_weight) in enumerate(connect[v]): connect[v][i][2] = dfs(v, i, connect) for v in range(vertice): answer = 0 for i, (next_dest, weight, all_weight) in enumerate(connect[v]): answer = max(answer, all_weight) print(answer)
Traceback (most recent call last): File "/tmp/tmpphc72mst/tmpgydr5khl.py", line 16, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s175574039
p02372
u798803522
1508671622
Python
Python3
py
Runtime Error
0
0
1321
from collections import defaultdict import sys sys.setrecursionlimit(200000) def dfs(source, edge_index, connect, used): if used[source] or connect[source][edge_index][2] >= 0: return connect[source][edge_index][2] used[source] = 1 this_edge = connect[source][edge_index] this_edge[2] = this_edge[1] dest = this_edge[0] for i, (next_dest, weight, all_weight) in enumerate(connect[dest]): if next_dest != source: if connect[dest][i][2] < 0: this_edge[2] = max(this_edge[2], dfs(dest, i, connect) + this_edge[1], used) else: this_edge[2] = max(this_edge[2], connect[dest][i][2] + this_edge[1]) return this_edge[2] v_num = int(input()) connect = defaultdict(list) for _ in range(v_num - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight, -1]) connect[v2].append([v1, weight, -1]) for v in range(v_num): for i, (next_dest, weight, all_weight) in enumerate(connect[v]): if connect[v][i][2] < 0: used = [0 for n in range(v_num)] connect[v][i][2] = dfs(v, i, connect, used) for v in range(v_num): answer = 0 for i, (next_dest, weight, all_weight) in enumerate(connect[v]): answer = max(answer, all_weight) print(answer)
Traceback (most recent call last): File "/tmp/tmp4ye3froy/tmpcaxoqwow.py", line 20, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s403533908
p02372
u798803522
1508675521
Python
Python3
py
Runtime Error
0
0
1645
# for undirected graph from collections import defaultdict import sys sys.setrecursionlimit(200000) def dfs(source, edge_index, connect, used): if connect[source][edge_index][3] or connect[source][edge_index][2] >= 0: return connect[source][edge_index][2] connect[source][edge_index][3] = 1 this_edge = connect[source][edge_index] this_edge[2] = this_edge[1] dest = this_edge[0] rev_i = this_edge[4] for i, (next_dest, weight, all_weight, f, rev_i) in enumerate(connect[dest]): if next_dest != source: if connect[dest][i][2] < 0: this_edge[2] = max(this_edge[2], dfs(dest, i, connect, used) + this_edge[1]) connect[dist][rev_i][2] = this_edge[2] else: this_edge[2] = max(this_edge[2], connect[dest][i][2] + this_edge[1]) connect[dist][rev_i][2] = this_edge[2] return this_edge[2] v_num = int(input()) connect = defaultdict(list) for _ in range(v_num - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight, -1, 0, len(connect[v2])]) connect[v2].append([v1, weight, -1, 0, len(connect[v1]) - 1]) for v in range(v_num): for i, (next_dest, weight, all_weight, f, rev_i) in enumerate(connect[v]): if connect[v][i][2] < 0: used = [0 for n in range(v_num)] connect[v][i][2] = dfs(v, i, connect, used) connect[next_dest][rev_i][2] = connect[v][i][2] for v in range(v_num): answer = 0 for i, (next_dest, weight, all_weight, f) in enumerate(connect[v]): answer = max(answer, all_weight) print(answer)
Traceback (most recent call last): File "/tmp/tmp9hsi2rdw/tmpw7rfytpc.py", line 25, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s076566577
p02372
u798803522
1508681398
Python
Python3
py
Runtime Error
0
0
1721
# for undirected graph from collections import defaultdict import sys sys.setrecursionlimit(200000) def dfs(source, edge_index, connect): if connect[source][edge_index][3] or connect[source][edge_index][2] >= 0: return connect[source][edge_index][2] connect[source][edge_index][3] = 1 this_edge = connect[source][edge_index] this_edge[2] = this_edge[1] dest = this_edge[0] rev_i = this_edge[4] for i, (next_dest, weight, all_weight, f, rev_i) in enumerate(connect[dest]): if next_dest != source: if connect[dest][i][2] < 0: this_edge[2] = max(this_edge[2], dfs(dest, i, connect) + this_edge[1]) # connect[dest][rev_i][2] = this_edge[2] else: this_edge[2] = max(this_edge[2], connect[dest][i][2] + this_edge[1]) # connect[dest][rev_i][2] = this_edge[2] return this_edge[2] v_num = int(input()) connect = defaultdict(list) for _ in range(v_num - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight, -1, 0, len(connect[v2])]) connect[v2].append([v1, weight, -1, 0, len(connect[v1]) - 1]) for v in range(v_num): for i, (next_dest, weight, all_weight, f, rev_i) in enumerate(connect[v]): if connect[v][i][2] < 0 and connect[next_dest][rev_i] < 0: connect[v][i][2] = dfs(v, i, connect) # connect[next_dest][rev_i][2] = connect[v][i][2] else: connect[v][i][2] = max(connect[next_dest][rev_i], connect[v][i][2]) for v in range(v_num): answer = 0 for i, (next_dest, weight, all_weight, f, rev) in enumerate(connect[v]): answer = max(answer, all_weight) print(answer)
Traceback (most recent call last): File "/tmp/tmp48c0saz8/tmpfhwtt8ow.py", line 25, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s050646246
p02372
u798803522
1508681433
Python
Python3
py
Runtime Error
0
0
1687
# for undirected graph from collections import defaultdict import sys sys.setrecursionlimit(200000) def dfs(source, edge_index, connect): if connect[source][edge_index][3] or connect[source][edge_index][2] >= 0: return connect[source][edge_index][2] connect[source][edge_index][3] = 1 this_edge = connect[source][edge_index] this_edge[2] = this_edge[1] dest = this_edge[0] rev_i = this_edge[4] for i, (next_dest, weight, all_weight, f, rev_i) in enumerate(connect[dest]): if next_dest != source: if connect[dest][i][2] < 0: this_edge[2] = max(this_edge[2], dfs(dest, i, connect) + this_edge[1]) # connect[dest][rev_i][2] = this_edge[2] else: this_edge[2] = max(this_edge[2], connect[dest][i][2] + this_edge[1]) # connect[dest][rev_i][2] = this_edge[2] return this_edge[2] v_num = int(input()) connect = defaultdict(list) for _ in range(v_num - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight, -1, 0, len(connect[v2])]) connect[v2].append([v1, weight, -1, 0, len(connect[v1]) - 1]) for v in range(v_num): for i, (next_dest, weight, all_weight, f, rev_i) in enumerate(connect[v]): if connect[v][i][2] < 0: connect[v][i][2] = dfs(v, i, connect) # connect[next_dest][rev_i][2] = connect[v][i][2] else: connect[v][i][2] = max(connect[next_dest][rev_i], connect[v][i][2]) for v in range(v_num): answer = 0 for i, (next_dest, weight, all_weight, f, rev) in enumerate(connect[v]): answer = max(answer, all_weight) print(answer)
Traceback (most recent call last): File "/tmp/tmp0g4_sth7/tmp4hd4k4my.py", line 25, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s656616893
p02373
u082657995
1556376524
Python
Python3
py
Runtime Error
20
5624
1752
class Lca: def __init__(self, E, root): import sys sys.setrecursionlimit(500000) self.root = root self.E = E # V<V> self.n = len(E) # 頂点数 self.logn = 0 while self.n > (1<<self.logn): self.logn += 1 # parent[n][v] = ノード v から 1<<n 個親をたどったノード self.parent = [[-1]*self.n for _ in range(self.logn)] self.depth = [0] * self.n self.dfs(root, -1, 0) for k in range(self.logn-1): for v in range(self.n): p_ = self.parent[k][v] if p_ >= 0: self.parent[k+1][v] = self.parent[k][p_] def dfs(self, v, p, dep): # ノード番号、親のノード番号、深さ self.parent[0][v] = p self.depth[v] = dep for e in self.E[v]: if e != p: self.dfs(e, v, dep+1) def get(self, u, v): if self.depth[u] > self.depth[v]: u, v = v, u # self.depth[u] <= self.depth[v] dep_diff = self.depth[v]-self.depth[u] for k in range(self.logn): if dep_diff >> k & 1: v = self.parent[k][v] if u==v: return u for k in range(self.logn-1, -1, -1): if self.parent[k][u] != self.parent[k][v]: u = self.parent[k][u] v = self.parent[k][v] return self.parent[0][u] n = int(input()) E = [[] for _ in range(n)] for i in range(n): kc = list(map(int, input().split())) k = kc[0] for c in kc[1:]: E[i].append(c) E[c].append(i) lca = Lca(E, 0) Q = int(input()) for _ in range(Q): u, v = map(int, input().split()) print(lca.get(u, v))
Traceback (most recent call last): File "/tmp/tmpmi6_9pdi/tmp07qa_mux.py", line 46, in <module> n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s977228897
p02373
u894114233
1471704708
Python
Python
py
Runtime Error
480
19080
1345
# -*- coding: utf-8 -*- from math import log def dfs(v,p,d): parent[0][v]=p depth[v]=d for i in xrange(len(G[v])): if G[v][i]!=p: dfs(G[v][i],v,d+1) def init(vn,root): dfs(root,-1,0) k=0 while k+1<log(vn,2): for v in xrange(vn): if parent[k][v]<0: parent[k+1][v]=-1 else: parent[k+1][v]=parent[k][parent[k][v]] k+=1 def lca(u,v,vn): if depth[u]>depth[v]:u,v=v,u k=0 while k<log(vn,2): if (depth[v]-depth[u])>>k&1: v=parent[k][v] k+=1 if u==v:return u for k in xrange(int(log(vn,2))-1,-1,-1): if parent[k][u]!=parent[k][v]: u=parent[k][u] v=parent[k][v] return parent[0][u] vn=int(raw_input()) #????????° G=[[] for _ in xrange(vn)] #??£??\????????? parent=[[-1]*vn for _ in xrange(int(log(vn,2))+1)] #parent[k][u]:??????u??????2^k????????????????????£?????¨???????????? depth=[0]*vn #depth[u]:??????u?????????????????±??????????????±??????0 root=0 #?????????????????? for i in xrange(vn): en=map(int,raw_input().split()) c=en[1:] #????????????i?????? G[i]=c init(vn,root) q=int(raw_input()) for _ in xrange(q): u,v=map(int,raw_input().split()) print(lca(u,v,vn))
Traceback (most recent call last): File "/tmp/tmp7kkup063/tmpchaf8z59.py", line 37, in <module> vn=int(raw_input()) #????????° ^^^^^^^^^ NameError: name 'raw_input' is not defined
s649570266
p02373
u894114233
1471704919
Python
Python
py
Runtime Error
1080
82012
1385
# -*- coding: utf-8 -*- from math import log import sys sys.setrecursionlimit(10**7) def dfs(v,p,d): parent[0][v]=p depth[v]=d for i in xrange(len(G[v])): if G[v][i]!=p: dfs(G[v][i],v,d+1) def init(vn,root): dfs(root,-1,0) k=0 while k+1<log(vn,2): for v in xrange(vn): if parent[k][v]<0: parent[k+1][v]=-1 else: parent[k+1][v]=parent[k][parent[k][v]] k+=1 def lca(u,v,vn): if depth[u]>depth[v]:u,v=v,u k=0 while k<log(vn,2): if (depth[v]-depth[u])>>k&1: v=parent[k][v] k+=1 if u==v:return u for k in xrange(int(log(vn,2))-1,-1,-1): if parent[k][u]!=parent[k][v]: u=parent[k][u] v=parent[k][v] return parent[0][u] vn=int(raw_input()) #????????° G=[[] for _ in xrange(vn)] #??£??\????????? parent=[[-1]*vn for _ in xrange(int(log(vn,2))+1)] #parent[k][u]:??????u??????2^k????????????????????£?????¨???????????? depth=[0]*vn #depth[u]:??????u?????????????????±??????????????±??????0 root=0 #?????????????????? for i in xrange(vn): en=map(int,raw_input().split()) c=en[1:] #????????????i?????? G[i]=c init(vn,root) q=int(raw_input()) for _ in xrange(q): u,v=map(int,raw_input().split()) print(lca(u,v,vn))
Traceback (most recent call last): File "/tmp/tmpkdtwvtfa/tmpip4k8epx.py", line 39, in <module> vn=int(raw_input()) #????????° ^^^^^^^^^ NameError: name 'raw_input' is not defined
s423519560
p02373
u894114233
1471704965
Python
Python
py
Runtime Error
1100
81980
1385
# -*- coding: utf-8 -*- from math import log import sys sys.setrecursionlimit(10**9) def dfs(v,p,d): parent[0][v]=p depth[v]=d for i in xrange(len(G[v])): if G[v][i]!=p: dfs(G[v][i],v,d+1) def init(vn,root): dfs(root,-1,0) k=0 while k+1<log(vn,2): for v in xrange(vn): if parent[k][v]<0: parent[k+1][v]=-1 else: parent[k+1][v]=parent[k][parent[k][v]] k+=1 def lca(u,v,vn): if depth[u]>depth[v]:u,v=v,u k=0 while k<log(vn,2): if (depth[v]-depth[u])>>k&1: v=parent[k][v] k+=1 if u==v:return u for k in xrange(int(log(vn,2))-1,-1,-1): if parent[k][u]!=parent[k][v]: u=parent[k][u] v=parent[k][v] return parent[0][u] vn=int(raw_input()) #????????° G=[[] for _ in xrange(vn)] #??£??\????????? parent=[[-1]*vn for _ in xrange(int(log(vn,2))+1)] #parent[k][u]:??????u??????2^k????????????????????£?????¨???????????? depth=[0]*vn #depth[u]:??????u?????????????????±??????????????±??????0 root=0 #?????????????????? for i in xrange(vn): en=map(int,raw_input().split()) c=en[1:] #????????????i?????? G[i]=c init(vn,root) q=int(raw_input()) for _ in xrange(q): u,v=map(int,raw_input().split()) print(lca(u,v,vn))
Traceback (most recent call last): File "/tmp/tmpt38u4y8x/tmpzzrjmhdg.py", line 39, in <module> vn=int(raw_input()) #????????° ^^^^^^^^^ NameError: name 'raw_input' is not defined
s241355782
p02373
u022407960
1481776093
Python
Python3
py
Runtime Error
30
7968
1801
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 16 3 1 2 3 3 4 5 6 0 2 7 8 0 2 9 10 2 14 15 0 0 0 3 11 12 13 0 0 0 0 0 10 1 3 4 5 4 9 4 10 4 13 9 13 9 14 9 8 13 14 13 7 output: 0 1 1 1 1 5 1 0 1 0 """ import sys import math def dfs(v, v_parent, v_depth): parent[0][v] = v_parent depth[v] = v_depth for adj in adj_table[v]: if adj != v_parent: dfs(adj, v, v_depth + 1) return None def dfs_init(): dfs(root, -1, 0) for k in range(v_log - 1): for v in range(v_num): if parent[k][v] < 0: parent[k + 1][v] = -1 else: parent[k + 1][v] = parent[k][parent[k][v]] return None def lca(u, v): if depth[u] > depth[v]: u, v = v, u for k in range(v_log): if (depth[v] - depth[u]) >> k & 1: v = parent[k][v] if u == v: return u for k in range(v_log - 1, -1, -1): if parent[k][u] != parent[k][v]: u = parent[k][u] v = parent[k][v] return parent[0][u] def cmd_exec(): for query in c_queries: q1, q2 = map(int, query) print(lca(q1, q2)) return None if __name__ == '__main__': _input = sys.stdin.readlines() v_num = int(_input[0]) c_edges = map(lambda x: x.split(), _input[1:v_num + 1]) q_num = int(_input[v_num + 1]) c_queries = map(lambda x: x.split(), _input[v_num + 2:]) v_log = math.floor(math.log2(v_num)) parent = [[-1] * v_num for _ in range(v_log)] depth = [-1] * v_num root = 0 adj_table = tuple([] for _ in range(v_num)) for idx, edge in enumerate(c_edges): c_num, *children = map(int, edge) # assert len(children) == c_num adj_table[idx].extend(children) dfs_init() cmd_exec()
Traceback (most recent call last): File "/tmp/tmp58g5y3ir/tmpwvs5p8rr.py", line 103, in <module> v_num = int(_input[0]) ~~~~~~^^^ IndexError: list index out of range
s474160311
p02373
u022407960
1481776135
Python
Python3
py
Runtime Error
20
7904
1834
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 16 3 1 2 3 3 4 5 6 0 2 7 8 0 2 9 10 2 14 15 0 0 0 3 11 12 13 0 0 0 0 0 10 1 3 4 5 4 9 4 10 4 13 9 13 9 14 9 8 13 14 13 7 output: 0 1 1 1 1 5 1 0 1 0 """ import sys import math sys.setrecursionlimit(int(1e5)) def dfs(v, v_parent, v_depth): parent[0][v] = v_parent depth[v] = v_depth for adj in adj_table[v]: if adj != v_parent: dfs(adj, v, v_depth + 1) return None def dfs_init(): dfs(root, -1, 0) for k in range(v_log - 1): for v in range(v_num): if parent[k][v] < 0: parent[k + 1][v] = -1 else: parent[k + 1][v] = parent[k][parent[k][v]] return None def lca(u, v): if depth[u] > depth[v]: u, v = v, u for k in range(v_log): if (depth[v] - depth[u]) >> k & 1: v = parent[k][v] if u == v: return u for k in range(v_log - 1, -1, -1): if parent[k][u] != parent[k][v]: u = parent[k][u] v = parent[k][v] return parent[0][u] def cmd_exec(): for query in c_queries: q1, q2 = map(int, query) print(lca(q1, q2)) return None if __name__ == '__main__': _input = sys.stdin.readlines() v_num = int(_input[0]) c_edges = map(lambda x: x.split(), _input[1:v_num + 1]) q_num = int(_input[v_num + 1]) c_queries = map(lambda x: x.split(), _input[v_num + 2:]) v_log = math.floor(math.log2(v_num)) parent = [[-1] * v_num for _ in range(v_log)] depth = [-1] * v_num root = 0 adj_table = tuple([] for _ in range(v_num)) for idx, edge in enumerate(c_edges): c_num, *children = map(int, edge) # assert len(children) == c_num adj_table[idx].extend(children) dfs_init() cmd_exec()
Traceback (most recent call last): File "/tmp/tmprz7al9dp/tmpovc8ryln.py", line 105, in <module> v_num = int(_input[0]) ~~~~~~^^^ IndexError: list index out of range
s869009376
p02373
u022407960
1481806461
Python
Python3
py
Runtime Error
30
7960
1939
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 16 3 1 2 3 3 4 5 6 0 2 7 8 0 2 9 10 2 14 15 0 0 0 3 11 12 13 0 0 0 0 0 10 1 3 4 5 4 9 4 10 4 13 9 13 9 14 9 8 13 14 13 7 output: 0 1 1 1 1 5 1 0 1 0 """ import sys import math sys.setrecursionlimit(int(1e5)) def dfs(v, v_parent, v_depth): # print(parent, v, v_parent) parent[0][v] = v_parent depth[v] = v_depth for adj in adj_table[v]: if adj != v_parent: dfs(adj, v, v_depth + 1) return None def dfs_init(): dfs(root, -1, 0) for k in range(v_log - 1): for v in range(v_num): if parent[k][v] < 0: parent[k + 1][v] = -1 else: parent[k + 1][v] = parent[k][parent[k][v]] return None def lca(u, v): if depth[u] > depth[v]: u, v = v, u for k in range(v_log): if ((depth[v] - depth[u]) >> k) & 1: v = parent[k][v] if u == v: return u for k in range(v_log - 1, -1, -1): if parent[k][u] != parent[k][v]: u = parent[k][u] v = parent[k][v] return parent[0][u] def cmd_exec(): for query in c_queries: q1, q2 = map(int, query) print(lca(q1, q2)) return None if __name__ == '__main__': # with open('test.txt', 'r') as f: # _input = f.readlines() _input = sys.stdin.readlines() v_num = int(_input[0]) c_edges = map(lambda x: x.split(), _input[1:v_num + 1]) q_num = int(_input[v_num + 1]) c_queries = map(lambda x: x.split(), _input[v_num + 2:]) v_log = math.floor(math.log2(v_num)) parent = [[-1] * v_num for _ in range(v_log)] depth = [-1] * v_num root = 0 adj_table = tuple([] for _ in range(v_num)) for idx, edge in enumerate(c_edges): c_num, *children = map(int, edge) assert len(children) == c_num adj_table[idx].extend(children) dfs_init() cmd_exec()
Traceback (most recent call last): File "/tmp/tmpsm9d8ieh/tmp3w0v9ca3.py", line 108, in <module> v_num = int(_input[0]) ~~~~~~^^^ IndexError: list index out of range
s560784192
p02373
u022407960
1481806815
Python
Python3
py
Runtime Error
860
105140
1939
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 16 3 1 2 3 3 4 5 6 0 2 7 8 0 2 9 10 2 14 15 0 0 0 3 11 12 13 0 0 0 0 0 10 1 3 4 5 4 9 4 10 4 13 9 13 9 14 9 8 13 14 13 7 output: 0 1 1 1 1 5 1 0 1 0 """ import sys import math sys.setrecursionlimit(int(1e5)) def dfs(v, v_parent, v_depth): # print(parent, v, v_parent) parent[0][v] = v_parent depth[v] = v_depth for adj in adj_table[v]: if adj != v_parent: dfs(adj, v, v_depth + 1) return None def dfs_init(): dfs(root, -1, 0) for k in range(v_log - 1): for v in range(v_num): if parent[k][v] < 0: parent[k + 1][v] = -1 else: parent[k + 1][v] = parent[k][parent[k][v]] return None def lca(u, v): if depth[u] > depth[v]: u, v = v, u for k in range(v_log): if ((depth[v] - depth[u]) >> k) & 1: v = parent[k][v] if u == v: return u for k in range(v_log - 1, -1, -1): if parent[k][u] != parent[k][v]: u = parent[k][u] v = parent[k][v] return parent[0][u] def cmd_exec(): for query in c_queries: q1, q2 = map(int, query) print(lca(q1, q2)) return None if __name__ == '__main__': # with open('test.txt', 'r') as f: # _input = f.readlines() _input = sys.stdin.readlines() v_num = int(_input[0]) c_edges = map(lambda x: x.split(), _input[1:v_num + 1]) q_num = int(_input[v_num + 1]) c_queries = map(lambda x: x.split(), _input[v_num + 2:]) v_log = math.floor(math.log2(v_num)) + 1 parent = [[-1] * v_num for _ in range(v_log)] depth = [-1] * v_num root = 0 adj_table = tuple([] for _ in range(v_num)) for idx, edge in enumerate(c_edges): c_num, *children = map(int, edge) assert len(children) == c_num adj_table[idx].extend(children) dfs_init() cmd_exec()
Traceback (most recent call last): File "/tmp/tmpywjijz1b/tmpyvow28d7.py", line 108, in <module> v_num = int(_input[0]) ~~~~~~^^^ IndexError: list index out of range
s989481025
p02373
u022407960
1481971667
Python
Python3
py
Runtime Error
0
0
1685
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 3 0.0 0.0 2.0 0.0 1.0 1.0 output: 1.41421356237 """ import sys from operator import attrgetter class ClosestPair(object): def __init__(self, in_data): """ Init closest pairs points set. """ self.p_num = int(in_data[0]) points = map(lambda x: x.split(), in_data[1:]) p_list = [complex(float(x), float(y)) for x, y in points] # pre_sort by axis_X self.p_list = sorted(p_list, key=attrgetter('real')) def closest_pair(self, array, array_length): if array_length <= 1: return float('inf') mid_idx = array_length // 2 div_line = array[mid_idx].real d_min = min(self.closest_pair(array[:mid_idx], mid_idx), self.closest_pair(array[mid_idx:], array_length - mid_idx)) # sort array_part by axis_Y while recursively comparing array.sort(key=attrgetter('imag')) min_stack = list() for ele in array: size = len(min_stack) # eliminate p which distance(p,div_line) >= d if abs(ele.real - div_line) >= d_min: continue for j in range(size): alt = ele - min_stack[size - j - 1] if alt.imag >= d_min: break d_min = min(d_min, abs(alt)) min_stack.append(ele) return d_min def solve(self): return self.closest_pair(array=self.p_list, array_length=self.p_num) if __name__ == '__main__': _input = sys.stdin.readlines() case = ClosestPair(in_data=_input) print('{:.6f}'.format(case.solve()))
Traceback (most recent call last): File "/tmp/tmpaucrqku2/tmps51z4z1m.py", line 65, in <module> case = ClosestPair(in_data=_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/tmpaucrqku2/tmps51z4z1m.py", line 24, in __init__ self.p_num = int(in_data[0]) ~~~~~~~^^^ IndexError: list index out of range
s095425140
p02373
u022407960
1481971749
Python
Python3
py
Runtime Error
0
0
1697
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 3 0.0 0.0 2.0 0.0 1.0 1.0 output: 1.41421356237 """ import sys from operator import attrgetter class ClosestPair(object): def __init__(self, in_data): """ Init closest pairs points set. """ self.p_num = int(in_data[0]) points = map(lambda x: x.split(), in_data[1:]) p_list = [complex(float(x), float(y)) for x, y in points] # pre_sort by axis_X p_list.sort(key=attrgetter('real')) self.p_list = p_list def closest_pair(self, array, array_length): if array_length <= 1: return float('inf') mid_idx = array_length // 2 div_line = array[mid_idx].real d_min = min(self.closest_pair(array[:mid_idx], mid_idx), self.closest_pair(array[mid_idx:], array_length - mid_idx)) # sort array_part by axis_Y while recursively comparing array.sort(key=attrgetter('imag')) min_stack = list() for ele in array: size = len(min_stack) # eliminate p which distance(p,div_line) >= d if abs(ele.real - div_line) >= d_min: continue for j in range(size): alt = ele - min_stack[size - j - 1] if alt.imag >= d_min: break d_min = min(d_min, abs(alt)) min_stack.append(ele) return d_min def solve(self): return self.closest_pair(array=self.p_list, array_length=self.p_num) if __name__ == '__main__': _input = sys.stdin.readlines() case = ClosestPair(in_data=_input) print('{:.6f}'.format(case.solve()))
Traceback (most recent call last): File "/tmp/tmph2_177ou/tmppuboy4zx.py", line 66, in <module> case = ClosestPair(in_data=_input) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/tmp/tmph2_177ou/tmppuboy4zx.py", line 24, in __init__ self.p_num = int(in_data[0]) ~~~~~~~^^^ IndexError: list index out of range
s530115706
p02373
u408260374
1482328592
Python
Python3
py
Runtime Error
640
35864
2003
class Edge: def __init__(self, dst, weight): self.dst, self.weight = dst, weight def __lt__(self, e): return self.weight > e.weight class Graph: def __init__(self, V): self.V = V self.E = [[] for _ in range(V)] def add_edge(self, src, dst, weight): self.E[src].append(Edge(dst, weight)) class HeavyLightDecomposition: def __init__(self, g, root=0): self.g = g self.vid = [0] * g.V self.head = [-1] * g.V self.heavy = [-1] * g.V self.parent = [-1] * g.V self.dfs(root) self.bfs(root) def dfs(self, v, par=-1): self.parent[v] = par sub, max_sub = 1, 0 for e in self.g.E[v]: child = e.dst if child != par: child_sub = self.dfs(child, v) sub += child_sub if child_sub > max_sub: max_sub = child_sub self.heavy[v] = child return sub def bfs(self, root=0): from collections import deque k, que = 0, deque([root]) while que: r = v = que.popleft() while v != -1: self.vid[v], self.head[v] = k, r for e in self.g.E[v]: if e.dst != self.parent[v] and e.dst != self.heavy[v]: que.append(e.dst) k += 1 v = self.heavy[v] def lca(self, u, v): while self.head[u] != self.head[v]: if self.vid[u] > self.vid[v]: u, v = v, u v = self.parent[self.head[v]] else: if self.vid[u] > self.vid[v]: u, v = v, u return u N = int(input()) g = Graph(N) for i in range(N): for c in map(int, input().split()[1:]): g.add_edge(i, c, 1) g.add_edge(c, i, 1) hld = HeavyLightDecomposition(g) Q = int(input()) for _ in range(Q): u, v = map(int, input().split()) print(hld.lca(u, v))
Traceback (most recent call last): File "/tmp/tmp7kp3fmq7/tmpjw5tj7my.py", line 64, in <module> N = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s665391174
p02373
u408260374
1482328676
Python
Python3
py
Runtime Error
980
95504
2045
import sys sys.setrecursionlimit(10**6) class Edge: def __init__(self, dst, weight): self.dst, self.weight = dst, weight def __lt__(self, e): return self.weight > e.weight class Graph: def __init__(self, V): self.V = V self.E = [[] for _ in range(V)] def add_edge(self, src, dst, weight): self.E[src].append(Edge(dst, weight)) class HeavyLightDecomposition: def __init__(self, g, root=0): self.g = g self.vid = [0] * g.V self.head = [-1] * g.V self.heavy = [-1] * g.V self.parent = [-1] * g.V self.dfs(root) self.bfs(root) def dfs(self, v, par=-1): self.parent[v] = par sub, max_sub = 1, 0 for e in self.g.E[v]: child = e.dst if child != par: child_sub = self.dfs(child, v) sub += child_sub if child_sub > max_sub: max_sub = child_sub self.heavy[v] = child return sub def bfs(self, root=0): from collections import deque k, que = 0, deque([root]) while que: r = v = que.popleft() while v != -1: self.vid[v], self.head[v] = k, r for e in self.g.E[v]: if e.dst != self.parent[v] and e.dst != self.heavy[v]: que.append(e.dst) k += 1 v = self.heavy[v] def lca(self, u, v): while self.head[u] != self.head[v]: if self.vid[u] > self.vid[v]: u, v = v, u v = self.parent[self.head[v]] else: if self.vid[u] > self.vid[v]: u, v = v, u return u N = int(input()) g = Graph(N) for i in range(N): for c in map(int, input().split()[1:]): g.add_edge(i, c, 1) g.add_edge(c, i, 1) hld = HeavyLightDecomposition(g) Q = int(input()) for _ in range(Q): u, v = map(int, input().split()) print(hld.lca(u, v))
Traceback (most recent call last): File "/tmp/tmpzmvgiz0t/tmpl0mhnyur.py", line 68, in <module> N = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s810976017
p02373
u797673668
1488274450
Python
Python3
py
Runtime Error
450
27696
1025
from math import log2 def build(): dfs(tree, 0, -1, 0) pk0 = parent[0] for k in range(1, logn): pk1 = parent[k] for v in range(n): pkv = pk0[v] pk1[v] = -1 if pkv < 0 else pk0[pkv] pk0 = pk1 def dfs(tree, v, p, d): parent[0][v] = p depth[v] = d for child in tree[v]: if child != p: dfs(tree, child, v, d + 1) def get(u, v): du, dv = depth[u], depth[v] if du > dv: u, v = v, u du, dv = dv, du for k in range(logn): if (dv - du) >> k & 1: v = parent[k][v] if u == v: return u for k in range(logn - 1, -1, -1): pk = parent[k] if pk[u] != pk[v]: u, v = pk[u], pk[v] return parent[0][u] n = int(input()) tree = [set(map(int, input().split()[1:])) for _ in range(n)] logn = int(log2(n)) + 1 parent = [[0] * n for _ in range(logn)] depth = [0] * n build() q = int(input()) for _ in range(q): print(get(*map(int, input().split())))
Traceback (most recent call last): File "/tmp/tmps4s8exqh/tmp7zstlnap.py", line 40, in <module> n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s841520214
p02373
u797673668
1488274533
Python
Python3
py
Runtime Error
1340
96168
1068
import sys sys.setrecursionlimit(100000) from math import log2 def build(): dfs(tree, 0, -1, 0) pk0 = parent[0] for k in range(1, logn): pk1 = parent[k] for v in range(n): pkv = pk0[v] pk1[v] = -1 if pkv < 0 else pk0[pkv] pk0 = pk1 def dfs(tree, v, p, d): parent[0][v] = p depth[v] = d for child in tree[v]: if child != p: dfs(tree, child, v, d + 1) def get(u, v): du, dv = depth[u], depth[v] if du > dv: u, v = v, u du, dv = dv, du for k in range(logn): if (dv - du) >> k & 1: v = parent[k][v] if u == v: return u for k in range(logn - 1, -1, -1): pk = parent[k] if pk[u] != pk[v]: u, v = pk[u], pk[v] return parent[0][u] n = int(input()) tree = [set(map(int, input().split()[1:])) for _ in range(n)] logn = int(log2(n)) + 1 parent = [[0] * n for _ in range(logn)] depth = [0] * n build() q = int(input()) for _ in range(q): print(get(*map(int, input().split())))
Traceback (most recent call last): File "/tmp/tmpbmp8hsug/tmpr6f5lagk.py", line 44, in <module> n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s972907851
p02373
u260980560
1500114790
Python
Python
py
Runtime Error
20
6688
1079
from collections import deque n = input() C = [None]*n P = [None]*n for i in xrange(n): ipt = map(int, raw_input().split()) C[i] = ipt[1:] for c in C[i]: P[c] = i LEV = (n+1).bit_length() parent = [[None]*(LEV + 1) for i in xrange(n)] for i in xrange(n): parent[i][0] = P[i] for k in xrange(1, LEV): for i in xrange(n): if parent[i][k] is None: parent[i][k+1] = None else: parent[i][k+1] = parent[parent[i][k]][k] depth = [None]*n deq = deque() deq.append(0) depth[0] = 0 while deq: v = deq.popleft() for c in C[v]: deq.append(c) depth[c] = depth[v] + 1 q = input() for t in xrange(q): u, v = map(int, raw_input().split()) if not depth[u] < depth[v]: u, v = v, u for k in xrange(LEV+1): if ((depth[v] - depth[u]) >> k) & 1: v = parent[v][k] if u == v: print u continue for k in xrange(LEV, -1, -1): if parent[u][k] != parent[v][k]: u = parent[u][k] v = parent[v][k] print parent[u][0]
File "/tmp/tmpkkiek6vi/tmpx98xiox9.py", line 40 print u ^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s551298889
p02373
u798803522
1508767122
Python
Python3
py
Runtime Error
70
8152
1225
import math from collections import defaultdict def dfs(here, p, d, parent, depth, connect): parent[0][here] = p depth[here] = d for next_v in connect[here]: dfs(next_v, here, d + 1, parent, depth, connect) def lca(v1, v2, parent, depth): if depth[v2] > depth[v1]: v1, v2 = v2, v1 for i in range(max_log): if depth[v1] - depth[v2] >> i & 1: v1 = parent[i][v1] if v1 == v2: return v2 for i in range(max_log - 1, -1, -1): if parent[i][v1] != parent[i][v2]: v1 = parent[i][v1] v2 = parent[i][v2] return parent[0][v2] connect = defaultdict(list) v_num = int(input()) root = 0 for i in range(v_num): edge = [int(n) for n in input().split(" ")] connect[i].extend(edge[1:]) depth = [-1 for n in range(v_num)] max_log = int(math.log(v_num, 2)) parent = [[-1 for n in range(v_num)] for m in range(max_log)] dfs(root, -1, 0, parent, depth, connect) for i in range(max_log - 1): for v in range(v_num): parent[i + 1][v] = -1 if parent[i][v] < 0 else parent[i][parent[i][v]] q_num = int(input()) for _ in range(q_num): v1, v2 = (int(n) for n in input().split(" ") ) print(lca(v1, v2, parent, depth))
Traceback (most recent call last): File "/tmp/tmpn79v33d9/tmp7rgtaqc4.py", line 25, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s807828051
p02373
u798803522
1508767226
Python
Python3
py
Runtime Error
720
24764
1233
import math from collections import defaultdict def dfs(here, p, d, parent, depth, connect): parent[0][here] = p depth[here] = d for next_v in connect[here]: dfs(next_v, here, d + 1, parent, depth, connect) def lca(v1, v2, parent, depth): if depth[v2] > depth[v1]: v1, v2 = v2, v1 for i in range(max_log): if depth[v1] - depth[v2] >> i & 1: v1 = parent[i][v1] if v1 == v2: return v2 for i in range(max_log - 1, -1, -1): if parent[i][v1] != parent[i][v2]: v1 = parent[i][v1] v2 = parent[i][v2] return parent[0][v2] connect = defaultdict(list) v_num = int(input()) root = 0 for i in range(v_num): edge = [int(n) for n in input().split(" ")] connect[i].extend(edge[1:]) depth = [-1 for n in range(v_num)] max_log = max(int(math.log(v_num, 2)), 1) parent = [[-1 for n in range(v_num)] for m in range(max_log)] dfs(root, -1, 0, parent, depth, connect) for i in range(max_log - 1): for v in range(v_num): parent[i + 1][v] = -1 if parent[i][v] < 0 else parent[i][parent[i][v]] q_num = int(input()) for _ in range(q_num): v1, v2 = (int(n) for n in input().split(" ") ) print(lca(v1, v2, parent, depth))
Traceback (most recent call last): File "/tmp/tmpi2n1i50d/tmpvb5o89ps.py", line 25, in <module> v_num = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s496228135
p02374
u847485844
1559098521
Python
Python
py
Runtime Error
0
0
3808
N = 10 ** 5 prt = [0] * (N + 1) left = [-1] + [0] * N right = [-1] + [0] * N sz = [0] + [1] * N key = [0] * (N + 1) val = [0] * (N + 1) rev = [0] * (N + 1) def update(i, l, r): # assert 1 <= i <= N sz[i] = 1 + sz[l] + sz[r] val[i] = key[i] + val[l] + val[r] def swap(i): if i: left[i], right[i] = right[i], left[i] rev[i] ^= 1 def prop(i): swap(left[i]) swap(right[i]) rev[i] = 0 return 1 def splay(i): # assert 1 <= i <= N x = prt[i] rev[i] and prop(i) li = left[i]; ri = right[i] while x and not left[x] != i != right[x]: y = prt[x] if not y or left[y] != x != right[y]: if rev[x] and prop(x): li, ri = ri, li swap(li); swap(ri) if left[x] == i: left[x] = ri prt[ri] = x update(x, ri, right[x]) ri = x else: right[x] = li prt[li] = x update(x, left[x], li) li = x x = y break rev[y] and prop(y) if rev[x] and prop(x): li, ri = ri, li swap(li); swap(ri) z = prt[y] if left[y] == x: if left[x] == i: v = left[y] = right[x] prt[v] = y update(y, v, right[y]) left[x] = ri; right[x] = y prt[ri] = x update(x, ri, y) prt[y] = ri = x else: left[y] = ri prt[ri] = y update(y, ri, right[y]) right[x] = li prt[li] = x update(x, left[x], li) li = x; ri = y else: if right[x] == i: v = right[y] = left[x] prt[v] = y update(y, left[y], v) left[x] = y; right[x] = li prt[li] = x update(x, y, li) prt[y] = li = x else: right[y] = li prt[li] = y update(y, left[y], li) left[x] = ri prt[ri] = x update(x, ri, right[x]) li = y; ri = x x = z if left[z] == y: left[z] = i update(z, i, right[z]) elif right[z] == y: right[z] = i update(z, left[z], i) else: break update(i, li, ri) left[i] = li; right[i] = ri prt[li] = prt[ri] = i prt[i] = x rev[i] = prt[0] = 0 def expose(i): p = 0 cur = i while cur: splay(cur) right[cur] = p update(cur, left[cur], p) p = cur cur = prt[cur] splay(i) return i def cut(i): expose(i) p = left[i] left[i] = prt[p] = 0 return p def link(i, p): expose(i) expose(p) prt[i] = p right[p] = i def evert(i): expose(i) swap(i) rev[i] and prop(i) def query(v): r = expose(v + 1) return val[r] def query_add(v, w): key[v + 1] += w expose(v + 1) readline = open(0).readline writelines = open(1, 'w').writelines N = int(readline()) for i in range(N): k, *C = map(int, readline().split()) # for c in C: # link(c+1, i+1) if k: expose(i + 1) for c in C: expose(c + 1) prt[c + 1] = i + 1 right[i + 1] = C[0] + 1 Q = int(readline()) ans = [] for q in range(Q): t, *args = map(int, readline().split()) if t: ans.append("%d\n" % query(args[0])) else: query_add(*args) writelines(ans)
Traceback (most recent call last): File "/tmp/tmpb9qu_vt3/tmpplcdj78o.py", line 177, in <module> N = int(readline()) ^^^^^^^^^^^^^^^ ValueError: invalid literal for int() with base 10: ''
s187726677
p02374
u210794057
1559302739
Python
Python
py
Runtime Error
0
0
1132
import sys   sys.setrecursionlimit(int(1e6))     class BIT:     def __init__(self, n):         self.n = n         self.data = [0] * (n + 1)       def get_sum(self, i):         ret = 0         while i > 0:             ret += self.data[i]             i -= (i & -i)         return ret       def add(self, i, w):         if i == 0:             return         while i <= self.n:             self.data[i] += w             i += (i & -i)     def dfs(u, cnt):     cnt += 1     l[u] = cnt     for c in tree[u]:         cnt = dfs(c, cnt)     cnt += 1     r[u] = cnt     return cnt     n = int(input()) l, r = [0] * n, [0] * n tree = [set(map(int, input().split()[1:])) for _ in range(n)]   bit = BIT(dfs(0, 1))   q = int(input()) for _ in range(q):     line = list(map(int, input().split()))     if line[0]:         print(bit.get_sum(r[line[1]] - 1))     else:         bit.add(l[line[1]], line[2])         bit.add(r[line[1]], -line[2])
File "/tmp/tmpzvxdq0uk/tmpm0yddm4a.py", line 2   ^ SyntaxError: invalid non-printable character U+00A0
s013981205
p02374
u210794057
1559302763
Python
Python3
py
Runtime Error
0
0
1132
import sys   sys.setrecursionlimit(int(1e6))     class BIT:     def __init__(self, n):         self.n = n         self.data = [0] * (n + 1)       def get_sum(self, i):         ret = 0         while i > 0:             ret += self.data[i]             i -= (i & -i)         return ret       def add(self, i, w):         if i == 0:             return         while i <= self.n:             self.data[i] += w             i += (i & -i)     def dfs(u, cnt):     cnt += 1     l[u] = cnt     for c in tree[u]:         cnt = dfs(c, cnt)     cnt += 1     r[u] = cnt     return cnt     n = int(input()) l, r = [0] * n, [0] * n tree = [set(map(int, input().split()[1:])) for _ in range(n)]   bit = BIT(dfs(0, 1))   q = int(input()) for _ in range(q):     line = list(map(int, input().split()))     if line[0]:         print(bit.get_sum(r[line[1]] - 1))     else:         bit.add(l[line[1]], line[2])         bit.add(r[line[1]], -line[2])
File "/tmp/tmpubomh9ox/tmprf_gqmj1.py", line 2   ^ SyntaxError: invalid non-printable character U+00A0
s325049396
p02374
u210794057
1559302929
Python
Python
py
Runtime Error
0
0
765
import sys sys.setrecursionlimit(int(1e6)) class BIT: def __init__(self, n): self.n = n self.data = [0] * (n + 1) def get_sum(self, i): ret = 0 while i > 0: ret += self.data[i] i -= (i & -i) return ret def add(self, i, w): if i == 0: return while i <= self.n: self.data[i] += w i += (i & -i) def dfs(u, cnt): cnt += 1 l[u] = cnt for c in tree[u]: cnt = dfs(c, cnt) cnt += 1 r[u] = cnt return cnt n = int(input()) l, r = [0] * n, [0] * n tree = [set(map(int, input().split()[1:])) for _ in range(n)] bit = BIT(dfs(0, 1)) q = int(input()) for _ in range(q): line = list(map(int, input().split())) if line[0]: print(bit.get_sum(r[line[1]] - 1)) else: bit.add(l[line[1]], line[2]) bit.add(r[line[1]], -line[2])
Traceback (most recent call last): File "/tmp/tmp84xt1iz6/tmpr4jo2zed.py", line 34, in <module> n = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s859652526
p02374
u686180487
1559377937
Python
Python3
py
Runtime Error
0
0
843
# -*- coding: utf-8 -*- N = int(input()) G = [None for i in range(N)] for i in range(N): temp = list(map(int, input().split())) G[i] = temp[1:] INF = 2**31 -1 temp = 1 while temp < N: temp *= 2 lis = [INF for i in range(2*temp-1)] def getSum(x): x += temp-1 w_sum = lis[x] while x > 0: x = (x-1) // 2 w_sum += lis[x] return w_sum def add(a, b, x, k=0, l=0, r=temp): if r <= a or b <= l: return 0 if a <= l and r <= b: lis[k] += x return 0 add(a, b, k*2+1, l, (l+r)//2) add(a, b, k*2+2, (l+r)//2, r) arr = [[0,0] for i in range(200010)] def dfs(k, a=0): arr[0][k] = a a += 1 for i in range(len(G[k])): dfs(G[k][i]) arr[1][k] = a dfs() Q = int(intput()) for i in range(Q): querry = list(map(int, input().split())) if querry[0] == 0: add() else: print(getSum()
File "/tmp/tmpwmp4ulzf/tmp6lie5v55.py", line 50 print(getSum() ^ SyntaxError: '(' was never closed
s216836427
p02374
u686180487
1559378172
Python
Python3
py
Runtime Error
0
0
900
# -*- coding: utf-8 -*- N = int(input()) G = [None for i in range(N)] for i in range(N): temp = list(map(int, input().split())) G[i] = temp[1:] INF = 2**31 -1 temp = 1 while temp < N: temp *= 2 lis = [INF for i in range(2*temp-1)] def getSum(x): x += temp-1 w_sum = lis[x] while x > 0: x = (x-1) // 2 w_sum += lis[x] return w_sum def add(a, b, x, k=0, l=0, r=temp): if r <= a or b <= l: return 0 if a <= l and r <= b: lis[k] += x return 0 add(a, b, k*2+1, l, (l+r)//2) add(a, b, k*2+2, (l+r)//2, r) def dfs(k, a=0): arr[0][k] = a a += 1 for i in range(len(G[k])): dfs(G[k][i]) arr[1][k] = a arr = [[0,0] for i in range(200010)] dfs() Q = int(input()) for i in range(Q): querry = list(map(int, input().split())) if querry[0] == 0: add(arr[0][querry[1]], arr[1][querry[1]],w) else: print(getSum(arr[0][querry[1]])
File "/tmp/tmpld0h_huk/tmpowd09eg5.py", line 53 print(getSum(arr[0][querry[1]]) ^ SyntaxError: '(' was never closed
s254865021
p02374
u686180487
1559482826
Python
Python3
py
Runtime Error
130
6344
1026
# -*- coding: utf-8 -*- class weightRtree: def __init__(self, num): self.num = num self.weight = [0] * (num+1) def add(self, v, w): if v == 0: return 0 while v <= self.num: self.weight[v] += w v += (-v) & v def getSum(self, u): temp = 0 while u > 0: temp += self.weight[u] u -= (-u) & u return temp N = int(input()) left = [0] * N right = [0] * N Tree = [set(map(int, input().split()[1:])) for i in range(N)] def DFS(count, source): count += 1 left[source] = count for item in Tree[source]: count = DFS(count, item) count += 1 right[source] = count return count Object = weightRtree(DFS(1, 0)) Q = int(input()) for i in range(Q): alist = list(map(int, input().split())) if not alist[0]: Object.add(left[alist[1]], alist[2]) Object.add(right[alist[1]], -alist[2]) else: print(Object.getSum(right[alist[1]] - 1))
Traceback (most recent call last): File "/tmp/tmpzidnbtd5/tmpwhyjlleb.py", line 21, in <module> N = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s682078303
p02376
u138546245
1535991164
Python
Python3
py
Runtime Error
20
5716
2157
from heapq import heappush, heappop class Edge: __slots__ = ('src', 'dest', 'capacity', 'flow') def __init__(self, v, w, capacity): self.src = v self.dest = w self.capacity = capacity self.flow = 0 def other(self, v): if v == self.src: return self.dest else: return self.src def residual_capacity(self, v): if v == self.src: return self.capacity - self.flow else: return self.flow def add_flow(self, v, f): if v == self.src: self.flow += f else: self.flow -= f class Network: def __init__(self, v): self.v = v self._edges = [[] for _ in range(v)] def add(self, edge): self._edges[edge.src].append(edge) self._edges[edge.dest].append(edge) def adj(self, v): return self._edges[v] def max_flow(network, s, t): def augment_path(): marked = [False] * network.v edge_to = [None] * network.v heap = [(-10**6, s, 0, None)] while heap: cap, v, n, e = heappop(heap) edge_to[v] = e if v == t: return (-cap, edge_to) if not marked[v]: marked[v] = True for ne in network.adj(v): n += 1 w = ne.other(v) c = ne.residual_capacity(v) if not marked[w] and c > 0: heappush(heap, (max(-c, cap), w, n, ne)) return (0, []) while True: cap, path = augment_path() if cap == 0: break v = t e = path[t] while e is not None: v = e.other(v) e.add_flow(v, cap) e = path[v] return sum(e.flow for e in network.adj(s) if e.src == s) def run(): v, e = [int(i) for i in input().split()] s, t = 0, v-1 net = Network(v) for _ in range(e): v, w, c = [int(i) for i in input().split()] net.add(Edge(v, w, c)) print(max_flow(net, s, t)) if __name__ == '__main__': run()
Traceback (most recent call last): File "/tmp/tmpl1ofrruc/tmpi4a9oo9l.py", line 93, in <module> run() File "/tmp/tmpl1ofrruc/tmpi4a9oo9l.py", line 81, in run v, e = [int(i) for i in input().split()] ^^^^^^^ EOFError: EOF when reading a line
s388674928
p02376
u419946054
1559523041
Python
Python3
py
Runtime Error
0
0
2046
V, E = map(int, input().split()) # -*- coding: utf-8 -*- import collections import queue class Dinic(): def __init__(self, N): self.N = N self.edges = collections.defaultdict(list) self.level = [0] * N self.iter = [0] * N def add(self, u, v, c, directed=True): """ 0-indexedでなくてもよいことに注意 u = from, v = to, c = cap directed = Trueなら、有向グラフである """ if directed: self.edges[u].append([v, c, len(self.edges[v])]) self.edges[v].append([u, c, len(self.edges[u])-1]) else: # TODO self.edges[u].append([v, c, len(self.edges[u])]) def bfs(self, s): self.level = [-1] * self.N self.level[s] = 0 que = queue.Queue() que.put(s) while not que.empty(): v = que.get(a) for i in range(len(self.edges[v])): e = self.edges[v][i] if e[1] > 0 and self.level[e[0]] < 0: self.level[e[0]] = self.level[v] + 1 que.put(e[0]) def dfs(self, v, t, f): if v == t: return f for i in range(self.iter[v], len(self.edges[v])): self.iter[v] = i e = self.edges[v][i] if e[1] > 0 and self.level[v] < self.level[e[0]]: d = self.dfs(e[0], t, min(f, e[1])) if d > 0: e[1] -= d self.edges[e[0]][e[2]][1] += d return d return 0 def maxFlow(self, s, t): flow = 0 while True: self.bfs(s) if self.level[t] < 0: return flow self.iter = [0] * (self.N) f = self.dfs(s, t, float('inf')) while f > 0: flow += f f = self.dfs(s, t, float('inf')) graph = Dinic(V) for i in range(E): u, v, c = map(int, input().split()) graph.add(u, v, c) print(graph.maxFlow(0, V-1))
Traceback (most recent call last): File "/tmp/tmp_a4orcti/tmp7_cqs5g4.py", line 1, in <module> V, E = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s029485610
p02376
u647766105
1430392043
Python
Python
py
Runtime Error
0
0
1690
#test def ford_fulkerson(graph, source, sink): N = len(graph) def dfs(cur_node, visited): if cur_node == source: flag = True while flag: flag = False for next_node in xrange(N): if graph[cur_node][next_node] <= 0: continue for r, v in dfs(next_node, visited): flag = True yield [cur_node]+r, v elif cur_node == sink: yield [[], float("inf")] else: for next_node in xrange(N): if visited[next_node] or graph[cur_node][next_node] <= 0: continue visited[next_node] = True for route, volume in dfs(next_node, visited): v = graph[cur_node][next_node] if v <= 0: break volume = min(v, volume) yield [[cur_node]+route, min(v, volume)] visited[next_node] = False for route, v in dfs(source, [False]*N): for s, t in zip(route, route[1:]): graph[s][t] -= v graph[t][s] += v def main(): V, E = map(int, raw_input().split()) source, sink = V, V+1 graph = [[0] * (V+2) for _ in xrange(V+2)] for _ in xrange(E): u, v, c = map(int, raw_input().split()) graph[u][v] = c graph[source][0] = sum(graph[0]) graph[V-1][sink] = sum(zip(*graph)[V-1]) ford_fulkerson(graph, source, sink) print graph[0][source] if __name__ == "__main__": for _ in xrange(2): main()
File "/tmp/tmpzkn_ardr/tmp9k_rnytt.py", line 44 print graph[0][source] ^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s245353289
p02376
u766807750
1432297930
Python
Python3
py
Runtime Error
0
0
1382
def read_data(): nV, nE = map(int, input().split()) cf = [dict() for v in range(nV)] for e in range(nE): u, v, c = map(int, input().split()) if u == t or v == s: continue C[u][v] = c C[v][u] = 0 return C, nV, 0, nV - 1 def dinic(cf, nV, s, t): dist = get_distance(cf, s, t) while dist[t] > 0: df = dfs(dist, cf, s, t, float('inf')) while df: df = dfs(dist, cf, s, t, float('inf')) dist = get_distance(cf, s, t) return sum(cf[t].values()) def get_distance(cf, s, t): dist = [-1] * len(cf) dist[s] = 0 frontiers = [s] while frontiers: new_frontiers = [] for u in frontiers: for v, capacity in cf[u].items(): if dist[v] == -1 and capacity > 0: dist[v] = dist[u] + 1 new_frontiers.append(v) frontiers = new_frontiers return dist def dfs(dist, cf, u, t, df): if u == t: return df for v, capacity in cf[u].items(): if dist[v] > dist[u] and capacity > 0: new_df = dfs(dist, cf, v, t, min(df, capacity)) if new_df > 0: cf[u][v] -= new_df cf[v][u] += new_df return new_df return 0 if __name__ == '__main__': C, nV, s, t = read_data() print(dinic(C, nV, s, t))
Traceback (most recent call last): File "/tmp/tmp8ecorvsf/tmpinawcx98.py", line 48, in <module> C, nV, s, t = read_data() ^^^^^^^^^^^ File "/tmp/tmp8ecorvsf/tmpinawcx98.py", line 2, in read_data nV, nE = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s413670266
p02376
u766807750
1432298046
Python
Python3
py
Runtime Error
0
0
1381
def read_data(): nV, nE = map(int, input().split()) C = [dict() for v in range(nV)] for e in range(nE): u, v, c = map(int, input().split()) if u == t or v == s: continue C[u][v] = c C[v][u] = 0 return C, nV, 0, nV - 1 def dinic(cf, nV, s, t): dist = get_distance(cf, s, t) while dist[t] > 0: df = dfs(dist, cf, s, t, float('inf')) while df: df = dfs(dist, cf, s, t, float('inf')) dist = get_distance(cf, s, t) return sum(cf[t].values()) def get_distance(cf, s, t): dist = [-1] * len(cf) dist[s] = 0 frontiers = [s] while frontiers: new_frontiers = [] for u in frontiers: for v, capacity in cf[u].items(): if dist[v] == -1 and capacity > 0: dist[v] = dist[u] + 1 new_frontiers.append(v) frontiers = new_frontiers return dist def dfs(dist, cf, u, t, df): if u == t: return df for v, capacity in cf[u].items(): if dist[v] > dist[u] and capacity > 0: new_df = dfs(dist, cf, v, t, min(df, capacity)) if new_df > 0: cf[u][v] -= new_df cf[v][u] += new_df return new_df return 0 if __name__ == '__main__': C, nV, s, t = read_data() print(dinic(C, nV, s, t))
Traceback (most recent call last): File "/tmp/tmpemboeeo5/tmpbvquyjzj.py", line 48, in <module> C, nV, s, t = read_data() ^^^^^^^^^^^ File "/tmp/tmpemboeeo5/tmpbvquyjzj.py", line 2, in read_data nV, nE = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s306761428
p02376
u408260374
1433586979
Python
Python3
py
Runtime Error
40
6772
1623
class FordFulkerson: """Ford-Fulkerson Algorithm: find max-flow complexity: O(FE) (F: max flow) """ def __init__(self, V, E, source, sink): """ V: the number of vertexes E: adjacency list source: start point sink: goal point """ self.V = V self.E = [[] for _ in range(V)] for fr in range(V): for to, cap in E[fr]: self.E[fr].append([to, cap, len(self.E[to])]) self.E[to].append([fr, 0, len(self.E[fr])-1]) self.maxflow = self.ford_fulkerson(source, sink) def ford_fulkerson(self, source, sink, INF=10**9): """find max-flow""" def dfs(vertex, sink, flow): """find augmenting path""" if vertex == sink: return flow used[vertex] = True for i, (to, cap, rev) in enumerate(self.E[vertex]): if not used[to] and cap > 0: d = dfs(to, sink, min(flow, cap)) if d > 0: self.E[vertex][i][1] -= d self.E[to][rev][1] += d return d return 0 maxflow = 0 while True: used = [False] * self.V flow = dfs(source, sink, INF) if flow == 0: return maxflow else: maxflow += flow V, E = map(int, input().split()) edge = [[] for _ in range(E)] for _ in range(E): u, v, cap = map(int, input().split()) edge[u].append((v, cap)) print(FordFulkerson(V, edge, 0, V-1).maxflow)
Traceback (most recent call last): File "/tmp/tmpptlk8x0l/tmpzxhlrsjv.py", line 44, in <module> V, E = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s951337363
p02376
u408260374
1433587196
Python
Python3
py
Runtime Error
30
6784
1662
import sys sys.setrecursionlimit(3000) class FordFulkerson: """Ford-Fulkerson Algorithm: find max-flow complexity: O(FE) (F: max flow) """ def __init__(self, V, E, source, sink): """ V: the number of vertexes E: adjacency list source: start point sink: goal point """ self.V = V self.E = [[] for _ in range(V)] for fr in range(V): for to, cap in E[fr]: self.E[fr].append([to, cap, len(self.E[to])]) self.E[to].append([fr, 0, len(self.E[fr])-1]) self.maxflow = self.ford_fulkerson(source, sink) def ford_fulkerson(self, source, sink, INF=10**9): """find max-flow""" def dfs(vertex, sink, flow): """find augmenting path""" if vertex == sink: return flow used[vertex] = True for i, (to, cap, rev) in enumerate(self.E[vertex]): if not used[to] and cap > 0: d = dfs(to, sink, min(flow, cap)) if d > 0: self.E[vertex][i][1] -= d self.E[to][rev][1] += d return d return 0 maxflow = 0 while True: used = [False] * self.V flow = dfs(source, sink, INF) if flow == 0: return maxflow else: maxflow += flow V, E = map(int, input().split()) edge = [[] for _ in range(E)] for _ in range(E): u, v, cap = map(int, input().split()) edge[u].append((v, cap)) print(FordFulkerson(V, edge, 0, V-1).maxflow)
Traceback (most recent call last): File "/tmp/tmp8l1k69tw/tmpwwkhvb86.py", line 46, in <module> V, E = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s023075135
p02376
u408260374
1433661442
Python
Python3
py
Runtime Error
0
0
2352
from collections import deque class Dinic: """Dinic Algorithm: find max-flow complexity: O(EV^2) """ class edge: def __init__(self, to, cap, rev): self.to, self.cap, self.rev = to, cap, rev def __init__(self, V, E, source, sink): """ V: the number of vertexes E: adjacency list source: start point sink: goal point """ self.V = V self.E = [[] for _ in range(V)] for fr in range(V): for to, cap in E[fr]: self.E[fr].append(self.edge(to, cap, len(self.E[to]))) self.E[to].append(self.edge(fr, 0, len(self.E[fr])-1)) self.maxflow = self.dinic(source, sink) def dinic(self, source, sink): """find max-flow""" INF = float('inf') maxflow = 0 while True: self.bfs(source) if self.level[sink] < 0: return maxflow self.itr = [0] * self.V while True: flow = self.dfs(source, sink, INF) if flow > 0: maxflow += flow else: break def dfs(self, vertex, sink, flow): """find augmenting path""" if vertex == sink: return flow for i in range(self.itr[v], len(self.E[vertex])): self.itr[v] = i e = self.E[vertex][i] if e.cap > 0 and self.level[vertex] < self.level[e.to]: d = self.dfs(e.to, sink, min(flow, e.cap)) if d > 0: self.E[vertex][i] -= d self.E[e.to][e.rev].cap += d return d return 0 def bfs(self, start): """find shortest path from start""" que = deque() self.level = [-1] * self.V que.append(start) self.level[start] = 0 while que: fr = que.popleft() for e in self.E[fr]: if e.cap > 0 and self.level[e.to] < 0: self.level[e.to] = self.level[fr] + 1 que.append(e.to) V, E = map(int, input().split()) edge = [[] for _ in range(V)] for _ in range(E): u, v, cap = map(int, input().split()) edge[u].append((v, cap)) print(Dinic(V, edge, 0, V-1).maxflow)
Traceback (most recent call last): File "/tmp/tmpu33vpc24/tmp09g4hgc5.py", line 69, in <module> V, E = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s022150152
p02376
u797673668
1461730468
Python
Python3
py
Runtime Error
60
8776
1225
from queue import deque def hierarchize(source): global edges, hierarchy hierarchy = [-1] * n queue = deque([(0, source)]) while queue: h, v = queue.popleft() if hierarchy[v] != -1: continue hierarchy[v] = h if v == sink: return True queue.extend((h + 1, target) for remain, target, _ in edges[v] if hierarchy[target] == -1 and remain) return False def augment(v, limit): global sink, edges, hierarchy if v == sink: return limit res = 0 for e in edges[v]: remain, target, idx = e if remain == 0 or hierarchy[v] >= hierarchy[target]: continue aug = augment(target, min(limit, remain)) e[0] -= aug edges[target][idx][0] += aug res += aug limit -= aug if not limit: break return res n, m = map(int, input().split()) sink = n - 1 edges = [[] for _ in range(n)] hierarchy = [-1] * n for _ in range(m): s, t, c = map(int, input().split()) edges[s].append([c, t, len(edges[t])]) edges[t].append([0, s, len(edges[s])]) res = 0 while hierarchize(0): res += augment(0, 10000001) print(res)
Traceback (most recent call last): File "/tmp/tmp03vip9u8/tmpl6vd5l8l.py", line 39, in <module> n, m = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s992039711
p02376
u797673668
1461730505
Python
Python3
py
Runtime Error
40
8752
1267
import sys from queue import deque sys.setrecursionlimit(200000) def hierarchize(source): global edges, hierarchy hierarchy = [-1] * n queue = deque([(0, source)]) while queue: h, v = queue.popleft() if hierarchy[v] != -1: continue hierarchy[v] = h if v == sink: return True queue.extend((h + 1, target) for remain, target, _ in edges[v] if hierarchy[target] == -1 and remain) return False def augment(v, limit): global sink, edges, hierarchy if v == sink: return limit res = 0 for e in edges[v]: remain, target, idx = e if remain == 0 or hierarchy[v] >= hierarchy[target]: continue aug = augment(target, min(limit, remain)) e[0] -= aug edges[target][idx][0] += aug res += aug limit -= aug if not limit: break return res n, m = map(int, input().split()) sink = n - 1 edges = [[] for _ in range(n)] hierarchy = [-1] * n for _ in range(m): s, t, c = map(int, input().split()) edges[s].append([c, t, len(edges[t])]) edges[t].append([0, s, len(edges[s])]) res = 0 while hierarchize(0): res += augment(0, 10000001) print(res)
Traceback (most recent call last): File "/tmp/tmpjxp9ksbr/tmpp213ura9.py", line 42, in <module> n, m = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s169232643
p02376
u797673668
1461730565
Python
Python3
py
Runtime Error
40
8824
1267
import sys from queue import deque sys.setrecursionlimit(500000) def hierarchize(source): global edges, hierarchy hierarchy = [-1] * n queue = deque([(0, source)]) while queue: h, v = queue.popleft() if hierarchy[v] != -1: continue hierarchy[v] = h if v == sink: return True queue.extend((h + 1, target) for remain, target, _ in edges[v] if hierarchy[target] == -1 and remain) return False def augment(v, limit): global sink, edges, hierarchy if v == sink: return limit res = 0 for e in edges[v]: remain, target, idx = e if remain == 0 or hierarchy[v] >= hierarchy[target]: continue aug = augment(target, min(limit, remain)) e[0] -= aug edges[target][idx][0] += aug res += aug limit -= aug if not limit: break return res n, m = map(int, input().split()) sink = n - 1 edges = [[] for _ in range(n)] hierarchy = [-1] * n for _ in range(m): s, t, c = map(int, input().split()) edges[s].append([c, t, len(edges[t])]) edges[t].append([0, s, len(edges[s])]) res = 0 while hierarchize(0): res += augment(0, 10000001) print(res)
Traceback (most recent call last): File "/tmp/tmpily28id6/tmp7ivztefq.py", line 42, in <module> n, m = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s307968750
p02376
u797673668
1461731046
Python
Python3
py
Runtime Error
20
7840
1266
def hierarchize(source): global edges, hierarchy l, r = 0, 1 hierarchy = [-1] * n hierarchy[source] = 0 queue[0] = source while l != r: v = queue[l] l += 1 if v == sink: break for remain, target, idx in edges[v]: if hierarchy[target] == -1 and remain != 0: hierarchy[target] = hierarchy[v] + 1 queue[r] = target r += 1 return hierarchy[sink] != -1 def augment(v, limit): global sink, edges, hierarchy if v == sink: return limit res = 0 for e in edges[v]: remain, target, idx = e if remain == 0 or hierarchy[v] >= hierarchy[target]: continue aug = augment(target, min(limit, remain)) e[0] -= aug edges[target][idx][0] += aug res += aug limit -= aug if not limit: break return res n, m = map(int, input().split()) sink = n - 1 edges = [[] for _ in range(n)] queue = [-1] * n hierarchy = [-1] * n for _ in range(m): s, t, c = map(int, input().split()) edges[s].append([c, t, len(edges[t])]) edges[t].append([0, s, len(edges[s])]) res = 0 while hierarchize(0): res += augment(0, 10000001) print(res)
Traceback (most recent call last): File "/tmp/tmph4di6zod/tmplv034zlz.py", line 39, in <module> n, m = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s412754155
p02376
u766163292
1468497395
Python
Python3
py
Runtime Error
2230
17292
2416
#!/usr/bin/env python3 # Based on https://en.wikipedia.org/wiki/Ford???Fulkerson_algorithm import collections import sys import uuid REC_LIMIT = 10000 class Edge(object): def __init__(self, source, sink, capacity): self.source = source self.sink = sink self.capacity = capacity self.rev_edge = None self.edge_id = uuid.uuid4() def __str__(self): return "Edge {} -> {} : {}".format(self.source, self.sink, self.capacity) class Network(object): def __init__(self): self.adj_edges = collections.defaultdict(list) self.flow = dict() self.used_edge = None def get_edges_from(self, vertex): return self.adj_edges[vertex] def add_edge(self, source, sink, capacity): assert source != sink f_edge = Edge(source, sink, capacity) b_edge = Edge(sink, source, 0) f_edge.rev_edge = b_edge b_edge.rev_edge = f_edge self.adj_edges[source].append(f_edge) self.adj_edges[sink].append(b_edge) self.flow[f_edge] = 0 self.flow[b_edge] = 0 def find_path_rec(self, source, sink, path): if source == sink: return path for edge in self.get_edges_from(source): residual = edge.capacity - self.flow[edge] if residual > 0 and not self.used_edge[edge.edge_id]: self.used_edge[edge.edge_id] = True result = self.find_path_rec(edge.sink, sink, path + [edge]) if result is not None: return result def find_path(self, *args): self.used_edge = collections.defaultdict(bool) return self.find_path_rec(*args) def max_flow(self, source, sink): while True: path = self.find_path(source, sink, []) if path is None: break flow = min(edge.capacity - self.flow[edge] for edge in path) for edge in path: self.flow[edge] += flow self.flow[edge.rev_edge] -= flow return sum(self.flow[edge] for edge in self.get_edges_from(source)) def main(): v, e = map(int, input().split()) network = Network() for _ in range(e): network.add_edge(*map(int, input().split())) mf = network.max_flow(0, v - 1) print(mf) if __name__ == '__main__': main()
Traceback (most recent call last): File "/tmp/tmpldfngrav/tmpwla6gpgu.py", line 84, in <module> main() File "/tmp/tmpldfngrav/tmpwla6gpgu.py", line 75, in main v, e = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s182046559
p02376
u022407960
1481010021
Python
Python3
py
Runtime Error
0
0
1933
#!/usr/bin/env python # -*- coding: utf-8 -*- """ input: 4 5 0 1 2 0 2 1 1 2 1 1 3 1 2 3 2 output: 3 """ import sys from collections import deque def graph_bfs(source, target, parent): visited = [False] * v_num queue = deque() queue.appendleft(source) visited[source] = True while queue: current = queue.popleft() for adj, cp in adj_table[current].items(): if cp and not visited[adj]: queue.append(adj) visited[adj] = True parent[adj] = current return True if visited[target] else False def graphFordFulkerson(source, sink): parent = [-1] * v_num max_flow = 0 while graph_bfs(source, sink, parent): path_flow = float('inf') bk_1 = sink while bk_1 is not source: path_flow = min(path_flow, adj_table[parent[bk_1]][bk_1]) bk_1 = parent[bk_1] max_flow += path_flow bk_2 = sink while bk_2 is not source: parent_bk_2 = parent[bk_2] assert parent_bk_2 != -1 adj_table[parent_bk_2].setdefault(bk_2, 0) # adj_table[bk_2].setdefault(parent_bk_2, 0) # print(bk_2, parent_bk_2, path_flow, bk_1, parent[bk_1], parent, adj_table) adj_table[parent_bk_2][bk_2] -= path_flow adj_table[bk_2][parent_bk_2] += path_flow bk_2 = parent[bk_2] return max_flow def generate_adj_table(_edges): for edge in _edges: source, target, cp = map(int, edge) init_adj_table[source][target] = cp return init_adj_table if __name__ == '__main__': _input = sys.stdin.readlines() v_num, e_num = map(int, _input[0].split()) edges = map(lambda x: x.split(), _input[1:]) init_adj_table = [dict() for _ in range(v_num)] adj_table = generate_adj_table(edges) ans = graphFordFulkerson(source=0, sink=v_num - 1) print(ans)
Traceback (most recent call last): File "/tmp/tmpus52giwo/tmp5z_otwss.py", line 77, in <module> v_num, e_num = map(int, _input[0].split()) ~~~~~~^^^ IndexError: list index out of range
s389260098
p02376
u072053884
1486747675
Python
Python3
py
Runtime Error
30
7916
1643
# Acceptance of input import sys file_input = sys.stdin V, E = map(int, file_input.readline().split()) adj_mat = [[0] * V for i in range(V)] for line in file_input: u, v, c = map(int, line.split()) adj_mat[u][v] = c # Dinic's algorithm import collections # BFS for residual capacity network to construct level graph def bfs(start, goal, parent): level = [V] * V queue = collections.deque() queue.append(start) level[start] = 0 while queue: u = queue.popleft() next_level = level[u] + 1 for v, flow in enumerate(adj_mat[u]): if (level[v] >= next_level) and (flow > 0): queue.append(v) level[v] = next_level parent[v].append(u) return level[goal] # DFS for level graph to construct blocking flow # search in reverse direction from sink def dfs(goal, path, parent, aug_path_flow, blocking_flow): v = path[-1] if v == goal: blocking_flow[0] += aug_path_flow for x, y in zip(path[1:], path[:-1]): adj_mat[x][y] -= aug_path_flow adj_mat[y][x] += aug_path_flow else: for u in parent[v]: path.append(u) dfs(goal, path, parent, min(aug_path_flow, adj_mat[u][v]), blocking_flow) path.pop() def dinic(source, sink): parent = [[] for i in range(V)] max_flow = 0 while bfs(source, sink, parent) != V: aug_path_flow = 10000 blocking_flow = [0] path = [sink] dfs(source, path, parent, 5, blocking_flow) max_flow += blocking_flow[0] return max_flow # output print(dinic(0, V - 1))
Traceback (most recent call last): File "/tmp/tmpwt6xr38_/tmpj7otpmry.py", line 7, in <module> V, E = map(int, file_input.readline().split()) ^^^^ ValueError: not enough values to unpack (expected 2, got 0)
s462366180
p02376
u072053884
1486780992
Python
Python3
py
Runtime Error
30
7952
1707
# Acceptance of input import sys file_input = sys.stdin V, E = map(int, file_input.readline().split()) adj_mat = [[0] * V for i in range(V)] for line in file_input: u, v, c = map(int, line.split()) adj_mat[u][v] = c # Dinic's algorithm import collections # BFS for residual capacity network to construct level graph def bfs(start, goal, parent): level = [V] * V queue = collections.deque() queue.append(start) level[start] = 0 while queue: u = queue.popleft() next_level = level[u] + 1 for v, flow in enumerate(adj_mat[u]): if (level[v] >= next_level) and (flow > 0): queue.append(v) level[v] = next_level parent[v].append(u) return level[goal] # DFS for level graph and construct blocking flow def dfs(goal, path, parent, blocking_flow): v = path[-1] if v == goal: aug_path_flow = 10000 for x, y in zip(path[1:], path[:-1]): aug_path_flow = min(aug_path_flow, adj_mat[x][y]) for x, y in zip(path[1:], path[:-1]): adj_mat[x][y] -= aug_path_flow adj_mat[y][x] += aug_path_flow blocking_flow[0] += aug_path_flow else: for u in parent[v]: path.append(u) dfs(goal, path, parent, blocking_flow) path.pop() def dinic(source, sink): parent = [[] for i in range(V)] max_flow = 0 while bfs(source, sink, parent) != V: blocking_flow = [0] path = [sink] # search in reverse direction from sink dfs(source, path, parent, blocking_flow) max_flow += blocking_flow[0] return max_flow # output print(dinic(0, V - 1))
Traceback (most recent call last): File "/tmp/tmp9uydrlpv/tmpebjsz3d_.py", line 7, in <module> V, E = map(int, file_input.readline().split()) ^^^^ ValueError: not enough values to unpack (expected 2, got 0)
s000637086
p02376
u352394527
1528856584
Python
Python3
py
Runtime Error
30
6008
805
from collections import deque INF = 10 ** 20 V, E = map(int, input().split()) net = [[0] * V for _ in range(V)] for _ in range(E): u, v, c = map(int, input().split()) net[u][v] = c start = 0 goal = V - 1 def check_net(node, path, capacity): for i in range(V): cap = net[node][i] if cap != 0: if i == goal: return (path + [i], min(cap, capacity)) else: return check_net(i, path + [i], min(cap, capacity)) else: return ([], 0) def update_net(path, capacity): for fromi, toi in zip(range(len(path) - 1), range(1, len(path))): fr, to = path[fromi], path[toi] net[fr][to] -= capacity net[to][fr] += capacity i = 0 while True: path, cap = check_net(start, [], INF) if path == []: break update_net(path, cap) print(sum(net[start]))
Traceback (most recent call last): File "/tmp/tmpb2fzn3bh/tmpcc9s4jea.py", line 5, in <module> V, E = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s728249748
p02377
u797673668
1461749600
Python
Python3
py
Runtime Error
40
7900
1396
from heapq import heappop, heappush def trace(source, edge_trace): v = source for i in edge_trace: e = edges[v][i] yield e v = e[1] def min_cost_flow(source, sink, required_flow): res = 0 while required_flow: visited = set() queue = [(0, source, tuple())] while queue: total_cost, v, edge_memo = heappop(queue) if v in visited: continue elif v == sink: dist = total_cost edge_trace = edge_memo break for i, (remain, target, cost, _) in enumerate(edges[v]): if remain and target not in visited: heappush(queue, (total_cost + cost, target, edge_memo + (i,))) else: return -1 aug = min(required_flow, min(e[0] for e in trace(source, edge_trace))) required_flow -= aug res += aug * dist for e in trace(source, edge_trace): remain, target, cost, idx = e e[0] -= aug edges[target][idx][0] += aug return res n, m, f = map(int, input().split()) edges = [[] for _ in range(n)] for _ in range(m): s, t, c, d = map(int, input().split()) es, et = edges[s], edges[t] ls, lt = len(es), len(et) es.append([c, t, d, lt]) et.append([0, s, d, ls]) print(min_cost_flow(0, n - 1, f))
Traceback (most recent call last): File "/tmp/tmpc0y4_6q8/tmpytuyxdld.py", line 41, in <module> n, m, f = map(int, input().split()) ^^^^^^^ EOFError: EOF when reading a line
s152935436
p02377
u798803522
1508603195
Python
Python3
py
Runtime Error
0
0
1601
v_num, e_num, flow = (int(n) for n in input().split(" ")) edges = defaultdict(list) for _ in range(e_num): s1, t1, cap, cost = (int(n) for n in input().split(" ")) edges[s1].append([t1, cap, cost, len(edges[t1])]) edges[t1].append([s1, cap, cost, len(edges[s1])]) answer = 0 before_vertice = [float("inf") for n in range(v_num)] before_edge = [float("inf") for n in range(v_num)] sink = v_num - 1 while True: distance = [float("inf") for n in range(v_num)] distance[0] = 0 updated = 1 while updated: updated = 0 for v in range(v_num): if distance[v] == float("inf"): continue for i, (target, cap, cost, trace_i) in enumerate(edges[v]): if cap > 0 and distance[target] > distance[v] + cost: distance[target] = distance[v] + cost before_vertice[target] = v before_edge[target] = i updated = 1 if distance[sink] == float("inf"): print(-1) break decreased = flow trace_i = sink while trace_i != 0: decreased = min(decreased, edges[before_vertice[trace_i]][before_edge[trace_i]][1]) trace_i = before_vertice[trace_i] flow -= decreased answer += decreased * distance[sink] trace_i = sink while trace_i != 0: this_edge = edges[before_vertice[trace_i]][before_edge[trace_i]] this_edge[1] -= decreased trace_i = before_vertice[trace_i] edges[trace_i][this_edge[3]][1] += decreased if flow <= 0: print(answer) break
Traceback (most recent call last): File "/tmp/tmpnois9c1s/tmpa07simbb.py", line 1, in <module> v_num, e_num, flow = (int(n) for n in input().split(" ")) ^^^^^^^ EOFError: EOF when reading a line
s891004489
p02377
u798803522
1508662467
Python
Python3
py
Runtime Error
0
0
909
from collections import defaultdict import sys sys.setrecursionlimit(200000) def dfs(source, used, all_weight, connect): max_weight = all_weight max_source = source used[source] = 1 for target, weight in connect[source]: if not used[target]: now_weight = all_weight + weight this_source, this_weight = dfs(target, used, now_weight, connect) if max_weight < this_weight: max_weight = this_weight max_source = this_source return [max_source, max_weight] vertice = int(input()) connect = defaultdict(list) for _ in range(vertice - 1): v1, v2, weight = (int(n) for n in input().split(" ")) connect[v1].append([v2, weight]) connect[v2].append([v1, weight]) answer = 0 start_v = 0 for i in range(2): used = [0 for n in range(vertice)] start_v, answer = dfs(start_v, used, 0, connect) print(answer)
Traceback (most recent call last): File "/tmp/tmpq63fmuoh/tmpgbxw1jid.py", line 18, in <module> vertice = int(input()) ^^^^^^^ EOFError: EOF when reading a line
s072263069
p02377
u176732165
1523420075
Python
Python
py
Runtime Error
0
0
2851
# -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import sys import csv import argparse import time import heapq INF = sys.maxint/3 class Edge: def __init__(self, to, cap, cost, rev): self.to = to self.cap = cap self.cost = cost self.rev = rev def print_attributes(self): print "to: {0}, cap: {1}, cost: {2}, rev: {3}".format(self.to, self.cap, self.cost, self.rev) class MinimumCostFlow: def __init__(self, V, E): self.V = V self.E = E self.G = [[] for i in range(V)] def add_edge(self, s, t, cap, cost): forward_edge = Edge(t, cap, cost, len(self.G[t])) self.G[s].append(forward_edge) backward_edge = Edge(s, 0, -cost, len(self.G[s])-1) self.G[t].append(backward_edge) def print_edges(self): print "==== print edges ====" for i in range(self.V): print "\nedges from {}".format(i) for e in self.G[i]: e.print_attributes() def minimum_cost_flow(self, s, t, f): res = 0 h = [0] * self.V while f>0: pque = [] dist = [INF for i in range(self.V)] prev_v = [0 for i in range(self.V)] prev_e = [0 for i in range(self.V)] dist[s] = 0 heapq.heappush(pque, (0, s)) while(len(pque)!=0): p = heapq.heappop(pque) v = p[1] if (dist[v] < p[0]): continue for i in range(len(self.G[v])): e = self.G[v][i] if (e.cap>0 and dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]): dist[e.to] = dist[v] + e.cost + h[v] - h[e.to] prev_v[e.to] = v prev_e[e.to] = i heapq.heappush(pque, (dist[e.to], e.to)) if dist[t] == INF: return -1 for v in range(self.V): h[v] += dist[v] d = f v = t while v!=s: d = min(d, self.G[prev_v[v]][prev_e[v]].cap) v = prev_v[v] f -= d res += d * h[t] v = t while v!=s: e = self.G[prev_v[v]][prev_e[v]] e.cap -= d self.G[v][e.rev].cap += d v = prev_v[v] return res def main(): V, E, F = map(int, raw_input().split()) mcf = MinimumCostFlow(V, E) for i in range(E): u, v, c, d = map(int, raw_input().split()) mcf.add_edge(u, v, c, d) #print "minimum cost flow: {}".format(mcf.minimum_cost_flow(0, V-1, F)) print mcf.minimum_cost_flow(0, V-1, F) if __name__ == '__main__': main()
File "/tmp/tmporsifkbd/tmp2a_0za00.py", line 22 print "to: {0}, cap: {1}, cost: {2}, rev: {3}".format(self.to, self.cap, self.cost, self.rev) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
s507700073
p02377
u176732165
1523420102
Python
Python
py
Runtime Error
0
0
2827
import numpy as np import matplotlib.pyplot as plt import seaborn as sns import pandas as pd import sys import csv import argparse import time import heapq INF = sys.maxint/3 class Edge: def __init__(self, to, cap, cost, rev): self.to = to self.cap = cap self.cost = cost self.rev = rev def print_attributes(self): print "to: {0}, cap: {1}, cost: {2}, rev: {3}".format(self.to, self.cap, self.cost, self.rev) class MinimumCostFlow: def __init__(self, V, E): self.V = V self.E = E self.G = [[] for i in range(V)] def add_edge(self, s, t, cap, cost): forward_edge = Edge(t, cap, cost, len(self.G[t])) self.G[s].append(forward_edge) backward_edge = Edge(s, 0, -cost, len(self.G[s])-1) self.G[t].append(backward_edge) def print_edges(self): print "==== print edges ====" for i in range(self.V): print "\nedges from {}".format(i) for e in self.G[i]: e.print_attributes() def minimum_cost_flow(self, s, t, f): res = 0 h = [0] * self.V while f>0: pque = [] dist = [INF for i in range(self.V)] prev_v = [0 for i in range(self.V)] prev_e = [0 for i in range(self.V)] dist[s] = 0 heapq.heappush(pque, (0, s)) while(len(pque)!=0): p = heapq.heappop(pque) v = p[1] if (dist[v] < p[0]): continue for i in range(len(self.G[v])): e = self.G[v][i] if (e.cap>0 and dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]): dist[e.to] = dist[v] + e.cost + h[v] - h[e.to] prev_v[e.to] = v prev_e[e.to] = i heapq.heappush(pque, (dist[e.to], e.to)) if dist[t] == INF: return -1 for v in range(self.V): h[v] += dist[v] d = f v = t while v!=s: d = min(d, self.G[prev_v[v]][prev_e[v]].cap) v = prev_v[v] f -= d res += d * h[t] v = t while v!=s: e = self.G[prev_v[v]][prev_e[v]] e.cap -= d self.G[v][e.rev].cap += d v = prev_v[v] return res def main(): V, E, F = map(int, raw_input().split()) mcf = MinimumCostFlow(V, E) for i in range(E): u, v, c, d = map(int, raw_input().split()) mcf.add_edge(u, v, c, d) #print "minimum cost flow: {}".format(mcf.minimum_cost_flow(0, V-1, F)) print mcf.minimum_cost_flow(0, V-1, F) if __name__ == '__main__': main()
File "/tmp/tmpr57gn0os/tmp_nfszgub.py", line 21 print "to: {0}, cap: {1}, cost: {2}, rev: {3}".format(self.to, self.cap, self.cost, self.rev) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?