s_id
stringlengths
10
10
p_id
stringlengths
6
6
u_id
stringlengths
10
10
date
stringlengths
10
10
language
stringclasses
1 value
original_language
stringclasses
11 values
filename_ext
stringclasses
1 value
status
stringclasses
1 value
cpu_time
int64
0
100
memory
stringlengths
4
6
code_size
int64
15
14.7k
code
stringlengths
15
14.7k
problem_id
stringlengths
6
6
problem_description
stringlengths
358
9.83k
input
stringlengths
2
4.87k
output
stringclasses
807 values
__index_level_0__
int64
1.1k
1.22M
s765501996
p02239
u009853378
1585153461
Python
Python3
py
Accepted
20
6036
643
ii = lambda : int(input()) li = lambda : list(map(int,input().split())) from collections import deque class tree: def __init__(self,n): self.n = n self.arr = [[] for i in range(self.n)] self.root = [-1]*n def add_arr(self, u, v): if v[0] != 0: self.arr[u-1] = v[1:]...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,582
s316463261
p02239
u610816226
1585108683
Python
Python3
py
Accepted
30
6032
498
from collections import deque n = int(input()) Ps = [] for _ in range(n): lis = list(map(int, input().split())) if len(lis)>2: Ps.append(lis[2:]) else: Ps.append([]) ans = [-1]*n ans[0] = 0 queue = deque() checked = deque() [queue.append([p, 1]) for p in Ps[0]] checked.append(1) while queue: t, pa = que...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,583
s356290662
p02239
u260266858
1585053997
Python
Python3
py
Accepted
20
5624
349
n = int(input()) G = {} D = [-1] * n for _ in range(n): A = list(map(int, input().split())) G[A[0] - 1] = [a - 1 for a in A[2:]] # print(G) q = [(0, 0)] while len(q) > 0: v, d = q[0] del q[0] if D[v] >= 0: continue D[v] = d for nv in G[v]: q.append((nv, d+1)) for i in ran...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,584
s198328929
p02239
u341188593
1585037613
Python
Python3
py
Accepted
30
6036
335
from collections import deque def bfs(): que=deque([]) que.append(0) v[0]=0 while que: p=que.popleft() for l in L[p]: if v[l-1]==-1: que.append(l-1) v[l-1]=v[p]+1 n=int(input()) L=[] for i in range(n): A=[] A=list(map(int,input().split())) L.append(A[2:]) v=[-1]*n bfs() for i in range(n): pr...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,585
s385568056
p02239
u705234550
1585026666
Python
Python3
py
Accepted
20
5656
578
n = int(input()) A = [[] for i in range(n)] for i in range(n): A[i] = list(map(int, input().split())) f_inf = float("inf") B = [[f_inf for i in range(2)] for j in range(n)] B[0][1] = 0 for i in range(n): B[i][0] = i+1 def dbs(a): global order l = len(A[a-1])-2 if l: for i in range(l): depth = B...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,586
s387228726
p02239
u093588014
1584971231
Python
Python3
py
Accepted
30
6040
486
from collections import deque def bfs(g,v): q=deque([v]) ; vis=[0]*n vis[v]=1 ; stg[v]=0 while q: v=q[0] for p in g[v]: if vis[p]==0: q.append(p) vis[p]=1 stg[p]=stg[v]+1 q.popleft() n=int(input()) g=[[] for i in range(n)] ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,587
s093853231
p02239
u639594975
1584961409
Python
Python3
py
Accepted
20
5612
480
n = int(input()) adj_list = [[] for _ in range(n + 1)] for i in range(n): tmp = list(map(int, input().split())) u = tmp[0] k = tmp[1] for v in range(2, 2 + k): adj_list[u].append(tmp[v]) d = [-1] * (n + 1) q = [] q.append(1) d[1] = 0 while len(q) > 0: u = q.pop(0) for v in adj_list[u...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,588
s601118316
p02239
u447255592
1584664487
Python
Python3
py
Accepted
40
6876
491
import queue n = int(input()) v = [[]] for _ in range(n): edges = sorted([int(x) for x in input().split()][2:]) v.append(edges) answers = [[idx, -1] for idx in range(n + 1)] q = queue.Queue() q.put(1) answers[1][1] = 0 while not q.empty(): node = q.get() for next_node in v[node]: if answers[ne...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,589
s213453868
p02239
u920279959
1584584137
Python
Python3
py
Accepted
20
6032
590
n = int(input()) E = [[]]*n for i in range(n): inputs = list(map(int, input().split())) if inputs[1]>0: for e in inputs[2:]: E[i] = E[i] + [e-1] visited = [-1]*n dist = [-1]*n dist[0] = 0 from collections import deque d = deque() d.append(0) visited[0] = 1 while len(d): nd = d.pople...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,590
s542678007
p02239
u483082413
1584535305
Python
Python3
py
Accepted
20
5632
999
n = int(input()) graphs = [] for _ in range(n): graphs.append(list(map(int, input().split()))[2:]) def bfs(graphs, seen_points, score, scores, queue): while len(queue) > 0: start_point = queue.pop(0) if seen_points[start_point - 1] == 2: continue else: next_po...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,591
s371584915
p02239
u230072054
1584422604
Python
Python3
py
Accepted
30
6032
418
N = int(input()) G = [None for i in range(N)] for i in range(N): u, k, *vs = map(int, input().split()) G[u-1] = [e-1 for e in vs] from collections import deque dist = [-1]*N que = deque([0]) dist[0] = 0 while que: v = que.popleft() d = dist[v] for w in G[v]: if dist[w] > -1: co...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,592
s656372993
p02239
u217435805
1584318688
Python
Python3
py
Accepted
30
6080
956
import sys # sys.stdin = open('input.txt') from collections import deque def fillMatrix(): for i in range(n): u, k, *v = map(int, input().split()) for j in v: matrix[i][j-1] = 1 def bfs(s): status[s] = VISITING distance[s] = 0 Q.append(s) while len(Q) > 0: u = Q...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,593
s651375770
p02239
u799595944
1584231815
Python
Python3
py
Accepted
30
6000
640
import sys from collections import deque sys.setrecursionlimit(50000) n = int(input()) data = [[] for _ in range(n)] answer = [-1]*n answer[0] = 0 for i in range(n): line = list(map(int, input().split(" "))) if line[1] != 0: for e in line[2:]: data[i].append(e) def bfs(vs, count=0): ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,594
s374795899
p02239
u076951214
1584185764
Python
Python3
py
Accepted
40
6876
594
#============================================================================= # Breadth First Search #============================================================================= import queue N=int(input()) Graph=[[] for _ in range(N+1)] dist=[-1 for _ in range(N+1)] for i in range(1,N+1): Graph[i]=list(map(int,...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,595
s744180525
p02239
u853158149
1584047896
Python
Python3
py
Accepted
30
6104
1,169
#!usr/bin/env python3 from collections import defaultdict,deque from heapq import heappush, heappop from itertools import permutations import sys import math import bisect def LI(): return [int(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def LS():return [list(x) for x in sys.stdi...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,596
s852772462
p02239
u284842807
1584008116
Python
Python3
py
Accepted
30
6056
1,265
n = int(input()) g = [input().split()[2:] for _ in range(n)] from collections import deque que = deque() que.append(0) d = [0] + [-1] * n while que: u = que.popleft() for v in g[u]: v = int(v) - 1 if d[v] < 0: d[v] = d[u] + 1 que.append(v) for i in range(n): prin...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,597
s294544167
p02239
u327242612
1583910976
Python
Python3
py
Accepted
30
6376
1,033
if __name__ == '__main__': from sys import stdin input = stdin.readline from collections import deque, defaultdict from copy import deepcopy INFTY = -1 # 探索しなかったやつを-1としてprintしやすくするために-1とした。 n = int(input()) M = [[0]*n for _ in range(n)] def bfs(s): q = deque() q.ap...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,598
s805544018
p02239
u942532706
1583842362
Python
Python3
py
Accepted
20
6008
509
from collections import deque import sys input = sys.stdin.readline N = int(input()) G = [[] for _ in range(N)] for _ in range(N): ls = list(map(int, input().split())) u = ls[0] - 1 for v in ls[2:]: G[u].append(v - 1) dist = [-1] * N que = deque([0]) dist[0] = 0 while que: v = que.popleft() ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,599
s854279009
p02239
u925219592
1583825176
Python
Python3
py
Accepted
30
6032
451
from collections import deque n = int(input()) g = [None] * n visited = [0] * n dist = [-1] * n Q = deque() while n: l = list(map(int, input().split())) g[l[0]-1] = [i-1 for i in l[2:]] n -= 1 Q.append((0, 0)) visited[0] = 1 while Q: i, d = Q.popleft() dist[i] = d for c in g[i]: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,600
s020835871
p02239
u655589806
1583756782
Python
Python3
py
Accepted
30
6040
539
n = int(input()) d = [-1]*(n+1) adjacent_list = [[] for _ in range(n+1)] for _ in range(n): edge = list(map(int,input().split())) if len(edge) >= 3: adjacent_list[edge[0]].extend(edge[2:]) from collections import deque que = deque() que.append(1) d[1] = 0 while len(que) != 0: v = que.popleft() ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,601
s362952198
p02239
u007578272
1583033870
Python
Python3
py
Accepted
20
5620
397
n = int(input()) v = [] for i in range(n): v_i = list(map(int,input().split()))[2:] v.append(v_i) d = [-1]*n d[0]=0 queue = [1] dis = 1 while len(queue)!=0: queue2 = [] for i in queue: for nex in v[i-1]: if d[nex-1]==-1: d[nex-1]=dis queue2.append...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,602
s644716639
p02239
u037175009
1582805099
Python
Python3
py
Accepted
40
7008
498
import sys import queue input = sys.stdin.readline n = int(input()) g = [[] for _ in range(n+1)] d = [-1 for _ in range(n+1)] for _ in range(n): c = list(map(int,input().split())) g[c[0]] = c[2:] depth = 0 q = queue.Queue() p = queue.Queue() q.put(1) while not q.empty(): while not q.empty(): s = q....
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,603
s013348585
p02239
u440554255
1582789616
Python
Python3
py
Accepted
50
6876
416
import queue n = int(input()) graph = {} for _ in range(n): i = list(map(int,input().split())) graph[i[0]] = i[2:] dist = [-1 for i in range(n)] todo = queue.Queue() seen = set() dist[0] = 0 todo.put(1) while (not todo.empty()): now = todo.get() for nx in graph[now]: if (dist[nx-1] != -1): continu...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,604
s244225064
p02239
u240839092
1582735865
Python
Python3
py
Accepted
30
6032
542
from collections import deque def main(): n = int(input()) V = [None]*(n+1) ans = [None]*(n+1) for i in range(1, n+1): _, _, *v = map(int, input().split()) V[i] = v q = deque([1]) ans[1] = 0 while q: node = q.pop() d = ans[node]+1 for v in V[node]: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,605
s242266276
p02239
u411823106
1582731700
Python
Python3
py
Accepted
30
6040
695
import sys from collections import deque n = int(sys.stdin.readline().strip()) edges = [[] for _ in range(n)] for _ in range(n): tmp = list(map(int, sys.stdin.readline().strip().split(" "))) for node in tmp[2:]: edges[tmp[0] - 1].append(node-1) # print(edges) distance = [0] * n q = deque() q.append((0...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,606
s027845477
p02239
u350045793
1582722221
Python
Python3
py
Accepted
30
6032
495
import collections n = int(input()) adj_list = [] for _ in range(n): adj = list(map(int, input().split(" "))) adj_list.append([c - 1 for c in adj[2:]]) distances = [-1 for _ in range(n)] queue = collections.deque() queue.append(0) distances[0] = 0 while queue: p = queue.popleft() for next_p in adj_l...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,607
s952536746
p02239
u915818487
1582711039
Python
Python3
py
Accepted
40
6868
1,874
from heapq import heappush, heappop, heapify from collections import deque, defaultdict, Counter import itertools from itertools import permutations, combinations, accumulate import sys import bisect import string import math import time def I(): return int(input()) def S(): return input() def MI(): return map(in...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,608
s041982980
p02239
u588702034
1582446903
Python
Python3
py
Accepted
20
5628
466
N = int(input()) E = [[] for _ in range(N+1)] for _ in range(N): tmp = list(map(int, input().split())) if tmp[1] == 0: continue E[tmp[0]] = tmp[2:] cnt = [0 for _ in range(N+1)] q = [1] while q: cp = q.pop(0) for np in E[cp]: if cnt[np] != 0: continue cnt[np] = cnt[cp] + 1 q.append(np) for ind, co...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,609
s047019609
p02239
u670802622
1582288418
Python
Python3
py
Accepted
30
6032
803
from collections import deque N = int(input()) nodes = [] G ={} is_visited = {} q = deque() for _ in range(N): lst = list(map(int, input().split())) idx = lst[0] nodes.append(idx) is_visited[idx] = False degree = lst[1] if degree > 0: G[idx] = lst[2:] else: G[idx] = [] INF...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,610
s673095711
p02239
u924675516
1581558218
Python
Python3
py
Accepted
50
7084
1,652
#!/usr/bin/env python3 import sys # SimpleQueue でOKなのだが AOJ の環境では Python3.7 が使えない from queue import Queue class BFS(): INFTY = sys.maxsize def __init__(self, M, n, debug=False): self.M = M self.n = n # 距離で訪問状態(color)を管理する self.d = [self.INFTY] * n self.debug = debug ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,611
s501678678
p02239
u299931928
1580198408
Python
Python3
py
Accepted
30
6028
923
# -*- coding: utf-8 -*- ''' 所要時間は、分位であった。 ''' # ライブラリのインポート #import re #正規表現 import sys #import heapq #import bisect from collections import deque #import math def main(): inf = 10000 n = int(input()) D = [inf for i in range(n)] L = [[] for i in range(n)] for i, line in enumerate(sys.stdin): ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,612
s762811849
p02239
u369455271
1579763497
Python
Python3
py
Accepted
20
5672
1,462
class Myqueue(): def __init__(self, size): self.size = size self.N = [None for _ in range(size)] self.head = 0 self.tail = 0 def is_empty(self): return self.head == self.tail def is_full(self): return self.head == (self.tail + 1) % self.size def enqueu...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,613
s799195381
p02239
u230220367
1579418001
Python
Python3
py
Accepted
30
6048
722
from collections import deque #キューを使った幅優先探索の実装 l = int(input()) rlist = [] for i in range(l): li = list(map(int, input().split())) rlist.append(deque([ll-1 for ll in li[2:]])) color = ['white'] * l queue = deque([]) d = [10000000] * l def bfs(u): color[u] = 'gray' d[u] = 0 queue.append(u) w...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,614
s028961170
p02239
u529459816
1577526907
Python
Python3
py
Accepted
30
6076
891
from collections import deque def breadth_first_search(adj_matrix, n): distance = [0] + [-1] * (n - 1) searching = deque([0]) while len(searching) > 0: start = searching.popleft() for end in range(n): if adj_matrix[start][end] and distance[end] == -1: searching....
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,615
s276410894
p02239
u194126754
1577434966
Python
Python3
py
Accepted
30
6040
1,037
from collections import deque # データの入力 N = int(input()) DG = {} for i in range(N): tmp = input().split() if int(tmp[1]) != 0: DG[i] = [int(x)-1 for x in tmp[2:]] else: DG[i] = [] result = {} is_visited = {key: False for key in range(N)} # bfs # 初期queue que = deque([(0, 0)]) # 必要なものはnodei...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,616
s666815633
p02239
u894062759
1577089524
Python
Python3
py
Accepted
30
6032
581
from collections import deque n = int(input()) g = [] # ans = [[-1]*n for _ in range(n)] ans = [-1] * n done = set() q = deque() for i in range(n): temp = list(map(int, input().split())) g.append(temp[2:]) # print(g) q.append(0) # dis = 1 ans[0] = 0 done.add(1) while q: idx = q.popleft() adj = g[id...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,617
s419719625
p02239
u123563150
1576479997
Python
Python3
py
Accepted
30
6032
352
n=int(input()) a=[] from collections import deque as dq for i in range(n): aaa=list(map(int,input().split())) aa=aaa[2:] a.append(aa) dis=[-1]*n dis[0]=0 queue=dq([0]) while queue: p=queue.popleft() for i in range(len(a[p])): if dis[a[p][i]-1]==-1: queue.append(a[p][i]-1) dis[a[p][i]-1]=dis[p]+1 for i in r...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,618
s140824156
p02239
u794140916
1575950753
Python
Python3
py
Accepted
40
6944
775
import queue WHITE = 0 GRAY = 1 BLACK = 2 NIL = -1 INF = 1000000000 def bfs(u): global Q Q.put(u) for i in range(n): d[i] = NIL d[u] = 0 while not Q.empty(): u = Q.get() for v in range(n): if (m[u][v] == 1) and (d[v] == NIL): d[v] = d[u] + 1 ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,619
s177701361
p02239
u504636463
1575296494
Python
Python3
py
Accepted
20
5704
1,044
if __name__ == '__main__': n = int(input()) graph = [[0 for i in range(n)] for _ in range(n)] frist = [0 for _ in range(n)] end = [0 for _ in range(n)] visit = [False for _ in range(n)] # get the group for i in range(n): datas = [int(num) for num in input().split(' ')] if dat...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,620
s393668659
p02239
u793120772
1574169060
Python
Python3
py
Accepted
20
5628
414
n=int(input()) ps=[0 for j in range(n+1)] for i in range(n): p,d,*vs=[int(j) for j in input().split()] ps[p]=vs dist=[-1 for i in range(n+1)] d=0 queue=[1] while queue: pos=queue.pop(0) if dist[pos] == -1: dist[pos]=d for i in ps[pos]: if dist[i]==-1: queu...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,621
s968890905
p02239
u608189632
1574000088
Python
Python3
py
Accepted
20
5696
978
def bfs(s): # たぶんstartのs global n, M, color, d, q q.append(s) d[s] = 0 while len(q) != 0: u = q.pop(0) for v in range(n): if M[u][v] != 1: continue if color[v] == 'WHITE': q.append(v) color[v] = 'GRAY' ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,622
s150686413
p02239
u439083584
1573785471
Python
Python3
py
Accepted
30
6028
330
from collections import deque n=int(input()) E=[0]*n for _ in range(n): u,k,*V=map(int,input().split()) E[u-1]=V d=[-1]*n d[0]=0 todo=deque([1]) while todo: now=todo.popleft() for v in E[now-1]: if d[v-1]==-1: todo.append(v) d[v-1]=d[now-1]+1 for i in range(n): print(...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,623
s327850396
p02239
u899624451
1573664765
Python
Python3
py
Accepted
30
6844
527
from functools import lru_cache @lru_cache(maxsize=None) def solve(i, count): global ans global a if count >= N: return if ans[i][0] == -1: ans[i][0] = count elif ans[i][0] > count: ans[i][0] = count for x in range(a[i][1]): solve(a[i][2 + x] - 1, count + 1) N...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,624
s480051558
p02239
u957523223
1572492483
Python
Python3
py
Accepted
30
6108
1,221
from collections import deque def get_next_node(node, discovery_time, adj_mat): for i, node in enumerate(adj_mat[node]): if discovery_time[i] == 0 and node == 1: return i return None def bfs(node, adj_mat): distances = [-1] * len(adj_mat) distances[node] = 0 q = deque(maxlen=...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,625
s143537906
p02239
u137240270
1572483817
Python
Python3
py
Accepted
30
6096
846
from collections import deque N = 100 n = 0 M = None WHITE = 0 GRAY = 1 BLACK = 2 INF = float('inf') d = None def main(): global n, M, d M = [[0 for _ in range(N)] for _ in range(N)] n = int(input()) d = [INF for _ in range(n)] for _ in range(n): u, k, *v_s = map(int, input().split(' '))...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,626
s980539997
p02239
u525329683
1572192398
Python
Python3
py
Accepted
30
6036
1,219
# https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/11/ALDS1_11_C # 実家のような安心感 # 与えられるノード番号についてはすべて0based indexに治す from collections import deque # データの入力 N = int(input()) DG = {} for i in range(N): tmp = input().split() if int(tmp[1]) != 0: DG[i] = [int(x)-1 for x in tmp[2:]] else: DG...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,627
s862771083
p02239
u639928232
1571999770
Python
Python3
py
Accepted
30
6028
504
#!/usr/bin/env python3 import sys input = lambda: sys.stdin.readline()[:-1] sys.setrecursionlimit(10**8) from collections import deque n=int(input()) G=[0]*n memo=[0]*n dis=[0]*n dis[0]=1 que=deque() que.append((0,1)) for i in range(n): inp=tuple(map(int,input().split())) G[inp[0]-1]=inp[2:] while que: v...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,628
s778459861
p02239
u121563294
1571387162
Python
Python3
py
Accepted
20
6032
365
from collections import deque N = int(input()) E = [None] * (N+1) for _ in range(N): u, k, *vs = list(map(int, input().split())) E[u] = vs d = [-1] * (N+1) d[1] = 0 q = deque([(1, 0)]) while q: v, c = q.popleft() for u in E[v]: if d[u] < 0: d[u] = c+1 q.append((u, c+1))...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,629
s327814469
p02239
u778781355
1571025239
Python
Python3
py
Accepted
60
7244
649
from typing import List from collections import deque def bfs(G: List[int], s: int) -> None: d[s] = 0 color[s] = "GRAY" Q = deque() Q.append(s) while Q: u = Q.popleft() for v in G[u]: if color[v] == "WHITE": color[v] = "GRAY" d[v] = d[u] + 1 pi[v] = u Q.append(v) color[u] = "BLACK" if _...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,630
s489169113
p02239
u153665391
1569850903
Python
Python3
py
Accepted
30
6376
570
from collections import deque from enum import Enum class Color(Enum): WHITE = 0 GRAY = 1 BLACK = 2 N = int(input()) adj = [list(map(int, input().split()))[2:] for i in range(N)] colors = [Color.WHITE for i in range(N)] INF = N d = [-1 for i in range(N)] d[0] = 0 q = deque() q.append(1) colors[0] == Color.G...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,631
s521760554
p02239
u134521115
1569391861
Python
Python3
py
Accepted
20
5684
1,953
import sys sys.setrecursionlimit(10**6) input = sys.stdin.readline INF = 100100100 class Queue: __slots__ = ['Q', 'begin', 'end'] def __init__(self): self.Q = [None] * 100 self.begin = 0 self.end = 0 def full(self) -> bool: if self.begin == 0 and self.end == 99: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,632
s630719321
p02239
u183700122
1569222856
Python
Python3
py
Accepted
20
5628
514
INF = float('inf') v = int(input()) graph = [None] for i in range(v): u = list(map(int, input().split())) graph.append(u[2:]) visited = [False] * (v+1) visited[1] = True cost = [INF] * (v+1) cost[1] = 0 c = 0 q = graph[1] while q: c += 1 q_next = [] for u in q: if not visited[u]: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,633
s601803722
p02239
u482227082
1568210509
Python
Python3
py
Accepted
30
6028
417
N = int(input()) G = [None for i in range(N)] for i in range(N): u, k, *vs = map(int, input().split()) G[u-1] = [e-1 for e in vs] from collections import deque dist = [-1]*N que = deque([0]) dist[0] = 0 while que: v = que.popleft() d = dist[v] for w in G[v]: if dist[w] > -1: co...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,634
s086033920
p02239
u182175955
1568192383
Python
Python3
py
Accepted
30
6040
671
#木ではない #非連結成分 #処理済みの判定、辺の距離はすべて1なので一度到達すればよい from collections import deque n = int(input()) V = [[] for _ in range(n+1)] for _ in range(n): inpt = list(map(int,input().split( ))) if len(inpt)>2: V[inpt[0]] = inpt[2:] lrg = 10000 D = [lrg]*(n+1) End = [0]*(n+1) D[1] = 0 Deq = deque() time = 1 Q = [1]...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,635
s307621754
p02239
u784856415
1566538295
Python
Python3
py
Accepted
30
6036
751
#import pysnooper #import os,re,sys,operator,math,heapq,string from collections import Counter,deque #from operator import itemgetter #from itertools import accumulate,combinations,groupby,combinations_with_replacement from sys import stdin,setrecursionlimit #from copy import deepcopy setrecursionlimit(10**6) input=std...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,636
s408010378
p02239
u631142478
1566498992
Python
Python3
py
Accepted
30
6032
538
from collections import deque def main(): n = int(input()) _next = [[] for _ in range(n)] for _ in range(n): u, k, *v = map(lambda s: int(s) - 1, input().split()) _next[u] = v queue = deque() queue.append(0) d = [-1] * n d[0] = 0 while queue: u = queue.popleft() ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,637
s561016106
p02239
u817810878
1565084622
Python
Python3
py
Accepted
50
7240
995
from collections import deque from typing import List import sys def bfs(adj_list: List[List[int]]) -> List[int]: n = len(adj_list) result: List[int] = [sys.maxsize for _ in range(n)] result[0] = 0 queue: deque = deque() queue.append(0) while len(queue) > 0: cur = queue.popleft() ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,638
s514155082
p02239
u470586273
1564902216
Python
Python3
py
Accepted
20
5628
303
n=int(input()) e=[[] for _ in range(n)] for _ in range(n): u,_,*v=map(lambda x:int(x)-1, input().split()) e[u]=sorted(v) q=[0] d=[-1]*n d[0]=0 while q: i=q.pop(0) for v in e[i]: if d[v]==-1: q.append(v) d[v]=d[i]+1 for i in range(n): print(i+1,d[i])
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,639
s663294298
p02239
u453734454
1564817118
Python
Python3
py
Accepted
30
6036
490
from collections import deque q = deque([]) n = int(input()) G = [] for i in range(n): u, k, *vvv = list(map(int, input().split())) G.append([v-1 for v in vvv]) visited = [False for _ in range(n)] q.append(0) dist = [-1 for _ in range(n)] dist[0] = 0 while q: u = q.popleft() visited[u] = True for v in G[u]: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,640
s058829579
p02239
u638043175
1564125069
Python
Python3
py
Accepted
30
6040
632
import sys input = sys.stdin.readline from collections import deque def main(): n = int(input().strip()) v = [[] for _ in range(n)] for i in range(n): v[i] = list(map(int, input().strip().split())) l = [-1] * (n+1) q = deque() q.append((1, 0)) while len(q) != 0: id, d = q.popleft() # print("id", id) if ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,641
s634825721
p02239
u371539389
1562923959
Python
Python3
py
Accepted
20
5624
561
n = int(input()) l = [[] for i in range(n)] retval=[10**10 for i in range(n)] for i in range(n): _input = [int(j) - 1 for j in input().split(" ")] u = _input[0] k = _input[1] + 1 node = sorted(_input[2::]) if u in node: node.remove(u) l[u] = node retval[0]=0 for step in range(n+1): ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,642
s003671500
p02239
u357267874
1562399362
Python
Python3
py
Accepted
40
6928
443
from queue import Queue n = int(input()) A = [[0 for i in range(n)] for j in range(n)] d = [-1] * n for i in range(n): u, k, *v = list(map(int, input().split())) for j in v: A[int(u)-1][int(j)-1] = 1 q = Queue() q.put(0) d[0] = 0 while not q.empty(): u = q.get() for v in range(n): ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,643
s789196463
p02239
u135880652
1561774750
Python
Python3
py
Accepted
30
6084
948
from collections import deque WHITE = 0 GRAY = 1 BLACK = 2 n = int(input()) M = [[0] * n for i in range(n)] d = [-1] * (n) color = [WHITE] * (n) time = 0 def dfs_visit(u): global time time += 1 color[u] = GRAY d[u] = time for v in range(n): if M[u][v] == 0: continue ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,644
s424252217
p02239
u528682978
1561610644
Python
Python3
py
Accepted
20
5620
574
n = int(input()) adjlis = [] for _ in range(n): lis=list(map(int,input().split())) adjlis.append(lis[2:]) visited = [False]*n dislis = [n+1]*n Q = [[1,0]] def pull(): return Q.pop(0) def push(x): Q.append(x) while len(Q)>0: cur = pull() curpoint = cur[0] count = cur[1] if not visited...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,645
s977738199
p02239
u902579831
1561559568
Python
Python3
py
Accepted
30
6100
846
from collections import deque INFINITY = 10 ** 10 WHITE = 0 GRAY = 1 BLACK = 2 def breadth_first_search(u_start = 0): color[u_start] = GRAY d[u_start] = 0 que.append(u_start) while que: u = que.popleft() for v in range(n): if matrix[u][v] and color[v] == WHITE: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,646
s177952529
p02239
u353547847
1561455757
Python
Python3
py
Accepted
30
6100
539
import collections N = int(input()) P = [[0 for _ in range(N)] for _ in range(N)] for i in range(N): u,k,*varray = map(int,input().split()) # u,k は数, varray は配列 for v in varray: P[u-1][v-1] = 1 D = [-1 for _ in range(N)] D[0] = 0 # 始点への距離は 0, 他の距離は-1 Q = collections.deque() Q.append(0) while len(Q...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,647
s569859826
p02239
u010611609
1561444654
Python
Python3
py
Accepted
20
5616
335
n = int(input()) al = [list(map(lambda x: int(x) - 1, input().split()))[2:] for _ in range(n)] queue = [0] d = [-1] * n d[0] = 0 while len(queue) > 0: u = queue.pop(0) for v in al[u]: if d[v] == -1: d[v] = d[u] + 1 queue.append(v) for i, x in enumerate(d): print('{} {}'.forma...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,648
s512238810
p02239
u026821956
1561390530
Python
Python3
py
Accepted
30
6108
569
import collections n = int(input()) G = [] for i in range(n): T = [0]*n G.append(T) for i in range(n): L = list(map(int,input().split())) for j in range(2,len(L)): G[i][L[j]-1] += 1 d = [float('inf')]*n q = collections.deque() q.append(0) d[0] = 0 while len(q) > 0: start = q.popleft() fo...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,649
s171484301
p02239
u481944101
1561372994
Python
Python3
py
Accepted
20
5628
302
n=int(input()) ne=[[] for _ in range(n+1)] for _ in range(n): u,k,*v=map(int,input().split()) ne[u]=v dis=[-1]*(n+1) dis[1]=0 q=[1] while q: p=q.pop(0) for e in ne[p]: if dis[e]==-1: dis[e]=dis[p]+1 q.append(e) for i in range(1,n+1): print(i,dis[i])
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,650
s673484001
p02239
u698762975
1561372737
Python
Python3
py
Accepted
30
6104
509
import collections n=int(input()) l=[] for i in range(n): l1=list(map(int,input().split())) l2=[] if l1[1]==0: l2=["0" for j in range(n)] else: for j in range(n): if j+1 in l1[2:]: l2.append("1") else: l2.append("0") l.append(l2) D=[-1 for i in range(n)] D[0]=0 q=collection...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,651
s145195687
p02239
u772853944
1561372317
Python
Python3
py
Accepted
20
5636
377
n=int(input()) d=[0]+[float('inf')]*(n-1) v=[[]]*n for i in range(n): tmp=list(map(int,input().split())) tmp=[i-1 for i in tmp[2:]] v[i]=tmp que=[0] while que!=[]: m=que.pop(0) for i in v[m]: if d[i]==float('inf'): que.append(i) d[i]=d[m]+1 for i in range(n): if...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,652
s316436056
p02239
u694199042
1559948698
Python
Python3
py
Accepted
40
7008
426
import queue n = int(input()) adj = [ [] for _ in range (n) ] for i in range(n): _, _, *adj[i] = list(map(int, input().split())) q = queue.Queue() d = [-1] * n flag = [0] * n q.put(1) d[0] = 0 flag[0] = 1 while not q.empty(): u = q.get() for v in adj[u-1]: if flag[v-1] == 0: flag...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,653
s213728595
p02239
u350698365
1558771871
Python
Python3
py
Accepted
30
6100
1,221
# vim: set fileencoding=utf-8: import sys import collections INF = 10000000 queue = collections.deque() # appendleft(), pop() n = int(input()) graph = [[0] * (n+1) for _ in range(n+1)] yet = [i for i in range(1, n+1)] ans = [-1] * (n+1) for i in range(1, n+1): tmp = [int(ele) for ele in input().split()] for ad...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,654
s539078465
p02239
u454636644
1557676062
Python
Python3
py
Accepted
20
6100
878
import sys from collections import deque def bfs(s): flag_list[s] = 1 d_list[s] = 0 q.append(s) while len(q)>0: u = q.popleft() for v in range(n): if m_list[u][v] == 1 and flag_list[v] == 0: flag_list[v] = 1 d_list[v] = d_list[u] + 1 ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,655
s205497505
p02239
u967553879
1555053351
Python
Python3
py
Accepted
30
6040
900
from collections import deque n = int(input()) g_list = {} for _ in range(n): n1, num, *cl = map(int, input().split()) g_list[n1] = cl # 書けないから移す。 GRAY = 1 WHITE = 0 BLACK = 2 class Graph(): def __init__(self, g_list): self.g_list = g_list self.dst = [-1] * (len(g_list)+1) self....
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,656
s962093847
p02239
u597769560
1554527342
Python
Python3
py
Accepted
20
6048
734
# -*- coding: utf-8 -*- """ Created on Sat Apr 6 11:50:13 2019 @author: Yamazaki Kenichi """ from collections import deque n = int(input()) A = [[]] + [list(map(int,input().split())) for i in range(n)] #B = [[0 for i in range(n+1)] for i in range(n+1)] #for i in range(1,n+1): # for j in range(A[i][1]): # ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,657
s318871960
p02239
u062898953
1554183761
Python
Python3
py
Accepted
20
5648
387
n = int(input()) g = [[0]*(n+1) for _ in range(n+1)] for i in range(n): u, k, *v, = map(int, input().split()) for vi in v: g[u][vi] = 1 d = [-1]*(n+1) d[1] = 0 l = [1] while len(l) > 0: u = l.pop(0) for i, v in enumerate(g[u]): if v == 1 and d[i] < 0: d[i] = d[u]+1 ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,658
s959740790
p02239
u065504661
1553955413
Python
Python3
py
Accepted
20
5700
642
n = int(input()) adj = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): l = list(map(int, input().split())) if l[1] == 0: continue else: for j in range(2,len(l)): adj[l[0]-1][l[j]-1] = 1 color = ['white' for _ in range(n)] d = [-1 for _ in range(n)] color[0] = 'grey...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,659
s841875054
p02239
u452220492
1553008400
Python
Python3
py
Accepted
30
6104
808
from collections import deque def bfs(s, m, n): d = [] for _ in range(n): d.append(1 << 21) queue = deque() queue.append(s) d[s] = 0 while len(queue) != 0: u = queue.popleft() for v in range(n): if m[u][v] == 0: continue if d[v] ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,660
s983773000
p02239
u276288047
1552019518
Python
Python3
py
Accepted
40
7004
570
# coding: utf-8 import queue q = queue.Queue() A = [[]*101 for _ in range(101)] d = [-1]*101 visited = [False]*101 n = int(input().rstrip()) for i in range(n): line = [int(j) for j in input().rstrip().split()] A[i+1].extend(line[2:]) d[1] = 0 q.put(1) while not q.empty(): v = q.get() if visited[v] =...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,661
s226758347
p02239
u415714413
1550900456
Python
Python3
py
Accepted
50
6852
1,221
import sys import queue readline = sys.stdin.readline write = sys.stdout.write n = int(input()) G = [None for i in range(n+1)] dist = [-1,]*(n+1) for i in range(n): node_id, k, *adj = map(int, readline().split()) # adj.sort() G[node_id] = adj def bfs(G): #q1が空でなければcntをインクリメントして下のブロックの処理を行う #q1...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,662
s669410556
p02239
u925817380
1550629473
Python
Python3
py
Accepted
20
5620
469
v = int(input()) vlist = [0]*v for num in range(v): temp = list(map(int,input().split())) if temp[1] != 0: vlist[num] = temp[2:] deep = [0]+[-1]*(v-1) waiting = [1] while waiting: vertex = waiting.pop(0) if vlist[vertex-1]: for subv in vlist[vertex-1]: if deep[subv-1] == -1...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,663
s347704027
p02239
u798796508
1549910824
Python
Python3
py
Accepted
30
6088
702
from collections import deque def dfs(s): q.append(s) d = [INF] * n d[s] = 0 while len(q): u = q.popleft() for v in range(n): if M[u][v] == 0: continue if d[v] != INF: continue d[v] = d[u] + 1 q.append(v) ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,664
s449276361
p02239
u978863922
1544190867
Python
Python3
py
Accepted
30
6076
1,291
# http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_C&lang=jp # Breadth First Search: python3 # 2018.12.06 yonezawa import sys input = sys.stdin.readline from collections import deque class node: def __init__(self): n = self.n = int(input()) self.cnt = 0 self.discover = [-...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,665
s597573493
p02239
u025180675
1543383149
Python
Python3
py
Accepted
20
6028
449
import collections N = int(input().strip()) Alst = [] for _ in range(N): u,k,*varray = map(int,input().strip().split(" ")) Alst.append(varray) D = [-1 for _ in range(N)] D[0] = 0 Q = collections.deque() Q.append(0) while len(Q) > 0: cur = Q.popleft() for dst in range(N): if (dst+1 in Alst[cu...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,666
s754006190
p02239
u160320718
1543229671
Python
Python3
py
Accepted
30
6096
469
import collections N = int(input()) G = [[0 for _ in range(N)] for _ in range(N)] for i in range(N): u,k,*varray = map(int,input().split()) for v in varray: G[u-1][v-1] = 1 D=[-1 for i in range(N)] D[0] = 0 Q = collections.deque() Q.append(0) while len(Q) > 0: cur = Q.popleft() for dst in ran...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,667
s344755322
p02239
u800534567
1542410218
Python
Python3
py
Accepted
20
5644
365
import sys n = int(sys.stdin.readline()) dist = [0] + [-1] * (n-1) vv=[list(map(int, sys.stdin.readline().split()))[2:] for _ in range(n)] def walk(k, d): for i in vv[k]: if dist[i-1]<0 or (dist[i-1]>0 and dist[i-1]>d): dist[i-1]=d else: continue walk(i-1, d+1) walk(0, 1) fo...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,668
s751274549
p02239
u125481461
1541803083
Python
Python3
py
Accepted
30
6036
1,054
import sys from collections import deque def enqueue(vtx: int, prev_vtx: int, search_queue: deque, vertices: dict, depth): #print(vtx,prev_vtx,search_queue,vertices,depth) search_queue.append(vtx) [val.remove(vtx) for val in vertices.values() if vtx in val] depth[vtx] = depth[prev_vtx] + 1 def main()...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,669
s887116104
p02239
u637322311
1540110653
Python
Python3
py
Accepted
30
6108
933
from sys import stdin from collections import deque def read_graph(n): A = [ [0]*(n+1) for _ in range(n+1) ] for _ in range(n): line = deque(stdin.readline().strip().split()) i = line.popleft() line.popleft() for k in line: A[int(i)][int(k)] = 1 return A def bfs...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,670
s355970785
p02239
u487861672
1538985840
Python
Python3
py
Accepted
20
5628
863
from sys import maxsize def read_adj(): n = int(input()) adj_list = [[] for _ in range(n + 1)] for _ in range(1, n + 1): u, _, *adj = [int(x) for x in input().split()] adj_list[u] = adj return n, adj_list def shortest_paths(n, adj_list, ini_key): dist = [maxsize] * (n + 1) S...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,671
s454224493
p02239
u260980560
1538757386
Python
Python3
py
Accepted
30
6028
417
N = int(input()) G = [None for i in range(N)] for i in range(N): u, k, *vs = map(int, input().split()) G[u-1] = [e-1 for e in vs] from collections import deque dist = [-1]*N que = deque([0]) dist[0] = 0 while que: v = que.popleft() d = dist[v] for w in G[v]: if dist[w] > -1: co...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,672
s897267595
p02239
u629780968
1537861570
Python
Python3
py
Accepted
30
6028
1,073
from collections import deque n = int(input()) #エッジの個数 edge = [[] for _ in range(n)] #エッジの隣接リスト(お隣さんを格納する) for _ in range(n): u, _, *v = map(lambda x: int(x)-1, input().split()) edge[u] = sorted(v) #入力された隣接頂点をお隣さんリストに格納 dist = [-1] * n #始点から各頂点iまでの距離を測るリスト dist[0] = 0 ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,673
s586957286
p02239
u697145890
1537710182
Python
Python3
py
Accepted
40
6396
990
import heapq from collections import deque from enum import Enum import sys import math from _heapq import heappush, heappop BIG_NUM = 2000000000 MOD = 1000000007 EPS = 0.000000001 class Info: def __init__(self,arg_node_id,arg_sum_cost): self.node_id = arg_node_id self.sum_cost = arg_sum_cost V ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,674
s338826638
p02239
u578148790
1533893411
Python
Python3
py
Accepted
30
6080
769
from collections import deque def bfs(s, n, m): q = deque() q.append(s) INFTY = 1 << 21 d = [INFTY] * n d[s] = 0 while len(q) != 0: u = q[0] q.popleft() for v in range(n): if not m[u][v]: continue if d[v] != INFTY: ...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,675
s685776589
p02239
u079141094
1468235916
Python
Python3
py
Accepted
30
8060
637
# Graph I - Breadth First Search from collections import deque def bfs(A,d): dq = deque() dq.append(0) d[0] = 0 while len(dq): v = dq.pop() for i in range(len(A[v])-1,-1,-1): f = A[v][i] if f == 1 and d[i] == -1: d[i] = d[v] + 1 dq....
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,676
s955188790
p02239
u805411170
1418302793
Python
Python3
py
Accepted
40
6732
617
n=int(input()) G=[[] for i in range(n)] #G=[] Q=[1] d=[-1]*n d[0]=0 visited=[False]*n visited[0]=True for i in range(n): arr=[int(x) for x in input().split()[2:]] for l in arr: G[i].append(l) #G[l-1].append(i+1) kyori=0 while len(Q)!=0: #print(Q) #print(visited) v=Q.pop(0...
p02239
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Breadth First Search</H1> ...
4 1 2 2 4 2 1 4 3 0 4 1 3
1 0 2 1 3 2 4 1
29,677
s228960811
p02241
u684241248
1531720258
Python
Python3
py
Accepted
20
6464
2,392
# -*- coding: utf-8 -*- from heapq import heappop, heappush def prim(edges, *, start=0, adj_matrix=False, default_value=float('inf')): ''' Returns the Minimum Spanning Tree(MST) of a given graph # sample input when given not as adjacency list (adj_matrix=False) (generally works faster) edges = [[(1, ...
p02241
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Minimum Spanning Tree</H1> ...
5 -1 2 3 1 -1 2 -1 -1 4 -1 3 -1 -1 1 1 1 4 1 -1 3 -1 -1 1 3 -1
5
29,678
s542214676
p02241
u684241248
1531736504
Python
Python3
py
Accepted
30
6468
1,385
# -*- coding: utf-8 -*- from heapq import heappop, heappush def prim(edges, *, start=0, adj_matrix=False, default_value=float('inf')): n = len(edges) costs = [float('inf')] * n costs[start] = 0 prevs = [-1] * n pq = [(0, start)] T = [True] * n while pq: tmp_cost, tmp_node = heap...
p02241
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Minimum Spanning Tree</H1> ...
5 -1 2 3 1 -1 2 -1 -1 4 -1 3 -1 -1 1 1 1 4 1 -1 3 -1 -1 1 3 -1
5
29,679
s825834696
p02241
u279605379
1534781288
Python
Python3
py
Accepted
20
6084
369
import heapq n = int(input()) A = [[int(i) for i in input().split()] for i in range(n)] h = [] DP = {} heapq.heappush(h,(0,0)) c = 0 while(len(DP)<n): tmp = heapq.heappop(h) if tmp[1] in DP: continue c += tmp[0] DP[tmp[1]] = 1 for i,j in enumerate(A[tmp[1]]): if j > -1 and not i in ...
p02241
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Minimum Spanning Tree</H1> ...
5 -1 2 3 1 -1 2 -1 -1 4 -1 3 -1 -1 1 1 1 4 1 -1 3 -1 -1 1 3 -1
5
29,680
s316972260
p02241
u657361950
1534983098
Python
Python3
py
Accepted
20
5916
683
import sys def prim(n, m): d = [sys.maxsize]*n p = [-1]*n c = [False]*n d[0] = 0 while True: minv = sys.maxsize u = -1 for i in range(n): if minv > d[i] and c[i] == False: minv = d[i] u = i if u == -1: break c[u] = True for i in range(n): if m[u][i] != sys.maxsize: if m[u][i] < d[i...
p02241
<script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }}); </script> <script language="JavaScript" type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"> </script> <H1>Minimum Spanning Tree</H1> ...
5 -1 2 3 1 -1 2 -1 -1 4 -1 3 -1 -1 1 1 1 4 1 -1 3 -1 -1 1 3 -1
5
29,681