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
s567177851
p02239
u913305001
1594631580
Python
Python3
py
Accepted
30
6092
741
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 _ in range(N)] D[0] = 0 Q = collections.deque() Q.append(0) while len(Q) > 0: # print("bfs",Q) cur = ...
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,482
s173089690
p02239
u748843150
1594473707
Python
Python3
py
Accepted
30
6112
759
from collections import deque Q=deque() n=int(input()) graph={} d=[0 for i in range(n+1)] for i in range(n): tmp=list(map(int,input().split())) graph[tmp[0]]=[0 for j in range(n+1)] for j in range(tmp[1]): graph[tmp[0]][tmp[2+j]]=1 Q.append(1) while len(Q) is not 0: number=Q[0] if 1 in gra...
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,483
s260096794
p02239
u075087498
1594292999
Python
Python3
py
Accepted
30
6032
355
from collections import deque n=int(input()) edge=[[] for _ in range(n+1)] for i in range(n): v,k,*u=map(int,input().split()) edge[v]=u p=[-1]*(n+1) p[1]=0 q=deque([]) q.append(1) v=[1]*(n+1) v[1]=0 while q: now=q.popleft() for i in edge[now]: if v[i]: q.append(i) p[i]=p[now]+1 v[i]=0 for ...
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,484
s963028170
p02239
u878424017
1594182064
Python
Python3
py
Accepted
30
6032
363
from collections import deque n = int(input()) data = [None]*n for _ in range(n): a,*b = map(int,input().split()) a -= 1 data[a] = [i-1 for i in b[1:]] d = [-1]*n dq = deque([(0,0)]) while dq: p,q = dq.popleft() if d[p] == -1: d[p] = q for i in data[p]: dq.append((i,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,485
s656324426
p02239
u626932242
1593936755
Python
Python3
py
Accepted
30
6044
699
import sys from collections import deque sys.setrecursionlimit(10 ** 7) input = sys.stdin.readline f_inf = float('inf') mod = 10 ** 9 + 7 def resolve(): n = int(input()) edge = [[] for _ in range(n)] for _ in range(n): u, k, *V = map(int, input().split()) for v in V: edge[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,486
s113065608
p02239
u757541954
1593870448
Python
Python3
py
Accepted
20
6032
446
from collections import deque from collections import defaultdict n = int(input()) graph = defaultdict(list) for i in range(n): u, k, *v = map(int, input().split()) for v in v: graph[u].append(v) ans = [-1] * (n + 1) q = deque([1]) ans[1] = 0 while q: c = q.popleft() for v in graph[c]: ...
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,487
s221555856
p02239
u272080389
1593831609
Python
Python3
py
Accepted
30
6032
516
from collections import deque n=int(input()) l=[0]+[list(map(int,input().split()))[2:] for _ in range(n)] flg=["white"]*(n+1) dist=[-1]*(n+1) def BFS(x): queue = deque([x]) dist[x]=0 flg[x]="gray" while queue: current=queue.popleft() for i in l[current]: if flg[i]=="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,488
s068607394
p02239
u357050809
1593784679
Python
Python3
py
Accepted
30
6036
447
from collections import deque n = int(input()) adj = [ [] for _ in range(n+1) ] for _ in range(n): lst = list(map(int,input().split())) if lst[1] > 0: adj[lst[0]] = lst[2:] q = deque([1]) dist = [-1] * (n+1) dist[1] = 0 q = deque([1]) while q: node = q.popleft() for nei in adj[node]: if dist[nei] != ...
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,489
s793965828
p02239
u917514663
1593782046
Python
Python3
py
Accepted
30
6036
798
from collections import deque n=int(input()) v=[[] for _ in range(n)] dist=[n+1]*n dist[0]=0 for i in range(n): l=list(map(int,input().split())) if l[1]==0: continue else: for j in l[2:]: v[l[0]-1].append(j-1) flg=1 q=deque() while flg==1: flg=0 q=deque() for i in ra...
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,490
s869961693
p02239
u171087702
1593680148
Python
Python3
py
Accepted
30
6040
607
from collections import deque n = int(input()) u = [] k = [] v = [] for i in range(n): tmp = list(map(int, input().split())) u.append(tmp[0]) k.append(tmp[1]) if tmp[1] != 0: v.append(tmp[2:]) else: v.append([]) d = deque([1]) dis = [-1]*n dis[0] = 0 visit = [False]*n visit...
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,491
s129507381
p02239
u973203074
1593639875
Python
Python3
py
Accepted
40
7068
605
import queue from collections import namedtuple T = namedtuple("T", "u d") n = int(input()) path = dict() ans = [-1] * (n + 1) for i in range(n): a = [int(x) for x in input().split()] a.reverse() u, k = a.pop(), a.pop() path[u] = set() for j in range(k): path[u].add(a.pop()) def bfs(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,492
s691411959
p02239
u646057502
1593081019
Python
Python3
py
Accepted
30
6024
390
from collections import deque n = int(input()) grap = [[]] for i in range(n): u,k,*v = map(int,input().split()) grap.append(v) dis = [-1]*(n+1) dis[0] = 0 dis[1] = 0 d = deque() d.append(1) while d: v = d.popleft() for i in grap[v]: if dis[i] != -1: continue dis[i] = dis[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,493
s408197753
p02239
u472664493
1593011887
Python
Python3
py
Accepted
30
6036
483
N = int(input()) tree = [[] for _ in range(N)] for _ in range(N): u, k, *v = map(int, input().split()) tree[u-1] = v ans = [float('inf') for _ in range(N)] ans[0] = 0 from collections import deque q = deque([(0, 0)]) while q: p, d = q.popleft() for c in tree[p]: if ans[c-1] > d+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,494
s319090861
p02239
u420455834
1592906514
Python
Python3
py
Accepted
50
7080
892
import sys import itertools # import numpy as np import time import math import heapq sys.setrecursionlimit(10 ** 7) from collections import defaultdict read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # map(int, input().split()) # list(map(int, input().spl...
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,495
s482479469
p02239
u647214684
1592818350
Python
Python3
py
Accepted
30
6032
437
import collections N = int(input()) route = [] for i in range(N): a = list(map(int, input().split())) route.append(a[2:]) ans = [-1 for _ in range(N)] ans[0] = 0 check = collections.deque([0])#now distance while check: now = check.popleft() for next in route[now]: next -= 1 if ans[next]...
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,496
s144597361
p02239
u742290340
1592816662
Python
Python3
py
Accepted
30
6032
399
import collections n= int(input()) g=[] for _ in range(n): v,k,*varray= map(int,input().split()) g.append(varray) d= [-1]* (n+10) d[0]=0 q= collections.deque() q.append(0) while len(q)> 0: cur = q.popleft() for next in g[cur]: if d[next-1]== -1: d[next-1]= d[cur]+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,497
s584859976
p02239
u903393228
1592815682
Python
Python3
py
Accepted
30
6096
669
import collections N = int(input()) M = [] for i in range(N): a = list(map(int,input().strip().split())) b = [int(i+1 in a[2:]) for i in range(N)] M.append(b) D = [-1 for _ in range(N)] D[0] = 0 # 始点への距離は 0, 他の距離は-1 Q = collections.deque() Q.append(0) # 始点 while len(Q) > 0: #print("bfs", 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,498
s592978073
p02239
u515473089
1592815475
Python
Python3
py
Accepted
20
6020
341
from collections import deque n=int(input()) d=[-1]*(n+1) d[1]=0 v=[[] for i in range(n+1)] for i in range(n): a=list(map(int,input().split())) v[a[0]]=a[2:] q=deque() q.append(1) while len(q)>0: curr=q.popleft() for i in v[curr]: if d[i]==-1: d[i]=d[curr]+1 q.append(i) for i in range(1,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,499
s148870417
p02239
u921537862
1592813314
Python
Python3
py
Accepted
20
6044
684
import sys sys.setrecursionlimit(10000000) MOD = 10 ** 9 + 7 INF = 10 ** 15 from collections import deque,defaultdict,Counter def main(): N = int(input()) G = [[] for _ in range(N)] for i in range(N): A = list(map(int,input().split())) for a in A[2:]: a -= 1 G[i].app...
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,500
s249529292
p02239
u040807768
1592711921
Python
Python3
py
Accepted
30
6044
445
import collections N = int(input()) UKV = [list(map(int,input().split())) for _ in range(N)] dist = [-1]*N que = collections.deque() G = [[]]*N for ukv in UKV: G[ukv[0]-1] = sorted(ukv[2:]) dist[0] = 0 que.append(1) while len(que) != 0: v = que.popleft() for nv in G[v-1]: if dist[nv-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,501
s386255750
p02239
u057134693
1592387930
Python
Python3
py
Accepted
30
6032
671
# 28 # ALDS_11_C - 幅優先探索 from collections import deque n = int(input()) u_l = [] k_l = [] v_l = [] for i in range(n): _v_l = [] for j, _ in enumerate(input().split()): if j == 0: u_l.append(int(_)) elif j == 1: k_l.append(int(_)) else: _v_l.append(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,502
s816192669
p02239
u593772041
1592232213
Python
Python3
py
Accepted
30
6036
529
from collections import deque N = int(input()) edge = [[] for _ in range(N)] for _ in range(N): t = list(map(int,input().split())) if t[1] != 0: edge[t[0]-1] = list(map(lambda x:int(x)-1, t[2:])) dist = [-1]*N dist[0] = 0 q = deque([0]) while q: now = q.popleft() for nxt in edge[now]...
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,503
s061287161
p02239
u781213648
1592130971
Python
Python3
py
Accepted
30
6040
960
from collections import deque def bfs(maze, visited, x): queue = deque([x]) visited[x] = 1 ans_list[x] = 0 while queue: #queueには訪れた地点が入っている。そこから、移動できるか考え、queueから消す。 y = queue.popleft()#queueに入っていたものを消す。 r = maze[y - 1][2:] for i in r: if visited[i] == 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,504
s395728799
p02239
u622849299
1592043464
Python
Python3
py
Accepted
30
6036
735
from collections import deque n = int(input()) graph_W = [] for _ in range(n): graph_W.append(list(map(int, input().split()))) num_root = [0]*n work_queue = deque([]) visited = set() work_queue.append(1) visited.add(1) cnt = 0 while work_queue: here = work_queue.popleft() - 1 num_node = graph_W[here]...
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,505
s537348325
p02239
u476858890
1592037980
Python
Python3
py
Accepted
20
6032
584
from collections import deque n = int(input()) graph = [] for _ in range(n): u = list(map(int, input().split())) if u[1] == 0: graph.append([]) else: graph.append(list(map(lambda x: x - 1, u[2: u[1] + 2]))) dist = [-1] * n def bfs(x): q = deque([]) dist[x] = 0 q.append(x) ...
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,506
s529792823
p02239
u928664635
1591949593
Python
Python3
py
Accepted
20
5628
395
n = int(input()) G = [[] for i in range(n)] #G(i): 頂点iと隣接する頂点のlist for i in range(n): a = [int(i) for i in input().split()] G[a[0]-1] = [i-1 for i in a[2:]] d = [-1] * n todo = [0] d[0] = 0 while(todo != []): v = todo.pop(0) for i in G[v]: if d[i] == -1: d[i] = d[v]+1 t...
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,507
s894307351
p02239
u210664395
1591797602
Python
Python3
py
Accepted
20
5636
650
def main(): import sys N = int(sys.stdin.readline()) L1 = [list(map(int, sys.stdin.readline().split())) for i in range(N)] L = [[] for _ in range(N+1)] for row in L1: L[row[0]] += row[2:] #print(L) d = [-1 for i in range(N+1)] #距離 def bfs(p, queue=[]): queue+=[p] ...
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,508
s844817184
p02239
u156732351
1591715100
Python
Python3
py
Accepted
40
6876
438
import queue n = int(input()) graph = [] for _ in range(n): line = list(map(int, input().split())) graph.append([v - 1 for v in line[2:]]) q = queue.Queue() q.put(0) dist = [-1] * n dist[0] = 0 while True: v = q.get() for next_v in graph[v]: if dist[next_v] == -1: q.put(next_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,509
s660494713
p02239
u502283627
1591621715
Python
Python3
py
Accepted
20
5628
394
n = int(input()) X = [] for _ in range(n): x = list(map(int, input().split())) if x[1] == 0: X.append([]) else: X.append(x[2:]) Y = [-1 for _ in range(n)] def search(i = 0, d = 0): if Y[i] > d or Y[i] < 0: Y[i] = d for x in X[i]: search(x - 1, d + 1) sea...
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,510
s209210912
p02239
u693473839
1591594816
Python
Python3
py
Accepted
50
7068
1,288
import math import string import collections from collections import Counter from collections import deque from decimal import Decimal import sys import fractions def readints(): return list(map(int, input().split())) def nCr(n, r): return math.factorial(n)//(math.factorial(n-r)*math.factorial(r)) def has...
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,511
s267813056
p02239
u450519077
1591534750
Python
Python3
py
Accepted
20
6036
429
import sys n = int(input()) G = [None]*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: #already 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,512
s067980782
p02239
u941938943
1591347709
Python
Python3
py
Accepted
30
6028
647
from collections import deque n = int(input()) G = [] for _ in range(n): u, k, *v = map(int, input().split()) u -= 1 G.append(v) q = deque() seen = [False] * n dist = [-1] * n def main(): q.append(0) seen[0] = True dist[0] = 0 while len(q) > 0: now_v = q.popleft() for new_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,513
s251006281
p02239
u759035995
1591291711
Python
Python3
py
Accepted
30
6048
484
from collections import deque u = int(input()) g = [[i for i in map(int, input().split())] for _ in range(u)] graph = [[] for _ in range(u)] ans = [-1] * u for i in range(u): for j in range(g[i][1]): graph[i].append(g[i][2 + j] - 1) que = deque() que.append(0) ans[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,514
s394246793
p02239
u208184442
1591183012
Python
Python3
py
Accepted
30
6044
542
from collections import deque n = int(input()) u = [[] for i in range(n+1)] #隣接リスト for i in range(n): v = list(map(int, input().split())) u[v[0]] = v[1:] #v = [次数, 頂点...] d = [-1] * (n+1) visited = [False] * (n+1) d[1] = 0 visited[1] = True que = deque([1]) while len(que) > 0: c = que.popleft() for 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,515
s153870839
p02239
u845612949
1591159680
Python
Python3
py
Accepted
30
6044
1,851
from collections import deque N = int(input()) # 頂点数 conj = [[] for _ in range(N)] for i in range(N): # 各頂点での進める先の候補の集合 L = list(map(int, input().split())) for a in L[2:]: conj[i].append(a-1) # 頂点番号をデクリメント start = 0 visit = [False]*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,516
s320383137
p02239
u367619546
1590938635
Python
Python3
py
Accepted
20
5680
572
# ALDS_11_C - 幅優先探索 import sys import heapq N = int(input()) G = [] for _ in range(N): u, k, *v = map(int, sys.stdin.readline().strip().split()) v = [i - 1 for i in v] G.append(v) distance = [-1] * N q = [] fp = [True] * N # 探索可能か記録する heapq.heapify(q) heapq.heappush(q, (0, 0)) fp[0] = False while 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,517
s368407554
p02239
u800829827
1590925582
Python
Python3
py
Accepted
30
6012
831
from collections import deque import sys INFTY=sys.maxsize WHITE=0 GRAY=1 BLACK=2 color=[] d=[] Q=deque([]) n=0 L=[] def readinput(): global n global L n=int(input()) for i in range(n+1): L.append([]) for i in range(n): inp=list(map(int,input().split())) L[inp[0]]=inp[2:...
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,518
s927486101
p02239
u698024824
1590917754
Python
Python3
py
Accepted
30
6032
1,735
from collections import deque import sys def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def I(): return int(sys.stdin.buffer.readline()) class Graph: def __init__(self, n, is_directed=False): """ :param n: 頂点数 :param is_directed: 有向グラフ(True)か無向グラフか(False) ""...
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,519
s963939461
p02239
u286314212
1590885673
Python
Python3
py
Accepted
20
5624
187
N=int(input()) M=[input().split()[2:]for _ in[0]*N] q=[0] d=[0]+[-1]*N while q: u=q.pop(0) for v in M[u]: v=int(v)-1 if d[v]<0:d[v]=d[u]+1;q+=[v] 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,520
s834703962
p02239
u356546564
1590767581
Python
Python3
py
Accepted
30
6036
489
from collections import deque n = int(input()) AL = [None for _ in range(n + 1)] for i in range(1, n + 1): ukv = [int(x) for x in input().split()] AL[ukv[0]] = ukv[2:] que = deque([1]) visited = [False] + [True] + [False] * (n - 1) dist = [-1] + [0] + [-1] * (n - 1) while que: u = que.popleft() for 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,521
s818152471
p02239
u153034461
1590765976
Python
Python3
py
Accepted
30
6368
728
import sys import itertools sys.setrecursionlimit(1000000000) from heapq import heapify,heappop,heappush,heappushpop import math import collections import copy def bfs(start): d = [-1]*n d[start] = 0 q = collections.deque([start]) while q: p = q.popleft() next_v = edge[p] for 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,522
s403716452
p02239
u061207034
1590760896
Python
Python3
py
Accepted
30
6040
550
from collections import deque def main(): while que: v, d = que.popleft() for nv in G[v]: nv -= 1 if D[nv] == -1: D[nv] = D[v] + 1 que.append((nv, D[nv])) for i in range(N): print(i+1, D[i]) if __name__ == "__main__": N = 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,523
s981175937
p02239
u430457144
1590583114
Python
Python3
py
Accepted
30
6028
496
from collections import deque N = int(input()) nodes = [tuple(map(int, input().split(' '))) for _ in range(N)] distances = [-1] * (N + 1) queue = deque() queue.append((1, 0)) while len(queue) > 0: node, distance = queue.popleft() if distances[node] != -1: continue distances[node] = distance ...
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,524
s244805645
p02239
u481465546
1590501288
Python
Python3
py
Accepted
20
6028
434
from collections import deque n=int(input()) l=[list(map(int,input().split())) for i in range(n)] d=deque() d.append(1) check=[0]*n check[0]=1 ans=[0]*n kyori=[0]*n while len(d)!=0: v=d.popleft() for i in l[v-1][2:]: if check[i-1]==0: check[i-1]=1 ans[i-1]=(kyori[v-1]+1) kyori[i-1]=(kyori[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,525
s934111842
p02239
u545131994
1590333408
Python
Python3
py
Accepted
30
6044
1,112
from collections import deque icase=0 if icase==0: n=int(input()) u=[0]*n # g = [deque([]) for _ in range(n)] g = [[] for _ in range(n)] for i in range(n): ukv=list(map(int,input().split())) u[i]=ukv[0]-1 ki=ukv[1] v=ukv[2:] for vi in v: g[u[i]].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,526
s859660280
p02239
u420091741
1590125746
Python
Python3
py
Accepted
20
6028
623
from collections import deque if __name__ == "__main__": n = int(input()) graph = [] for _ in range(n): v, k, *tmp = map(int, input().split()) for j in range(len(tmp)): tmp[j] -= 1 graph.append(tmp) visited = [False] * n qu = deque() qu.appendleft((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,527
s068984213
p02239
u256637043
1590059622
Python
Python3
py
Accepted
50
6844
611
# ALDS1_11_C.py import queue def printNode(): for i, node in enumerate(glaph): print(i, node) N = int(input()) glaph = [[] for i in range(N+1)] for i in range(1, N+1): glaph[i] = list(map(int, input().split()))[2:] # printNode() step = [-1]*(N+1) step[1] = 0 q = queue.Queue() q.put(1) while not 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,528
s116374082
p02239
u652807546
1590000078
Python
Python3
py
Accepted
30
6044
697
from collections import deque # リストでグラフを表現する n = int(input()) G = [input().split()[2:] for _ in range(n)] # 頂点1から各頂点への距離を-1 (頂点1からたどり着けない)で初期化する d = [-1] * n que = deque() # 訪問予定の頂点を格納する # 初期状態をキューに入れる que.append(0) d[0] = 0 while que: p = que.popleft() # FIFOでキューの要素を取り出し、その頂点を訪ねる。 for v in G[p]: ...
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,529
s540869885
p02239
u917391429
1589969900
Python
Python3
py
Accepted
20
5624
406
n = int(input()) a = [] for i in range(n): tmp = list(map(int, input().split())) a.append(tmp[2:]) ans = [-1 for _ in range(n + 1)] ans[1] = 0 que = [1] while len(que) > 0: current = que.pop(0) if len(a[current - 1]) == 0: continue for next in a[current - 1]: if ans[next] == -1: que.append(next)...
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,530
s133711830
p02239
u767630905
1589892568
Python
Python3
py
Accepted
30
6044
548
from collections import deque n = int(input()) G = [[] for _ in range(n)] for i in range(n): u, k, *vs = map(int, input().split()) u -= 1 vs = list(map(lambda x: x - 1, vs)) for v in vs: G[u].append(v) dist = [-1] * n dist[0] = 0 que = deque([0]) while len(que) > 0: now_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,531
s705497239
p02239
u789716290
1589873992
Python
Python3
py
Accepted
30
6040
640
from collections import deque N = int(input()) inf = 10 ** 6 graph = [[] for i in range(N)] distance = [inf for i in range(N)] for i in range(N): a = list(map(int,input().split())) a = list(map(lambda x: x - 1,a)) k,n = a[0],a[1] if n >= 0: graph[k] = a[2:] def bfs(path,root,dist): dist[root] = 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,532
s215554231
p02239
u417207234
1589860455
Python
Python3
py
Accepted
30
6040
440
from collections import deque n = int(input()) g = [[] for _ in range(n)] for i in range(n): _, _, *edge = map(lambda x: int(x)-1, input().split()) g[i] = edge q = deque() q.append(0) dist = [-1 for _ in range(n)] dist[0] = 0 while(len(q) > 0): v = q.popleft() for nv in g[v]: if(dist[nv] !=...
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,533
s696878676
p02239
u083949342
1589810239
Python
Python3
py
Accepted
50
7016
909
import queue def main(): n = int(input()) rows = [] for i in range(n): rows.append([int(x) for x in input().split()]) vum = {} for row in rows: u,k = row[0],row[1] ul = [] if k > 0: for j in row[2:]: ul.append(j) vum.update({u:ul}...
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,534
s201829308
p02239
u382571188
1589806125
Python
Python3
py
Accepted
50
6932
687
INFTY = 10 ** 10 import queue def bfs(s): q = queue.Queue() q.put(s) d = [INFTY] * n d[s] = 0 while not q.empty(): u = q.get() for v in range(n): if M[u][v] == 0: continue if d[v] != INFTY: continue 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,535
s405690027
p02239
u847316328
1589764271
Python
Python3
py
Accepted
30
6040
625
from collections import deque 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] dist = [-1]*n #全頂点を未訪問に初期化(-1)で未訪問 que = deque([0]) #初期条件 (頂点 0 を初期ノードとする) dist[0] = 0 #0を訪問済み while len(que)>0: now = que.popleft() for i 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,536
s225855727
p02239
u906360845
1589640679
Python
Python3
py
Accepted
50
7040
434
cin = open(0).read().strip().split('\n') n = int(cin[0]) v = [list(map(lambda x: int(x)-1, a.split(' ')[2:])) for a in cin[1:]] import queue q = queue.Queue() q.put(0) dist = [-1]*n dist[0] = 0 seen = [False]*n while not q.empty(): num = q.get() seen[num] = True for a in v[num]: if dist[a] != -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,537
s596446411
p02239
u590535211
1589451168
Python
Python3
py
Accepted
30
6008
547
from collections import deque n = int(input()) C = [[] for _ in range(n+1)] for _ in range(n): stdin = list(map(int,input().split())) for j in range(stdin[1]): C[stdin[0]].append(stdin[2+j]) q = deque() q.append(1) d = [-1]*(n+1) visited = [False]*(n+1) d[1] = 0 visited[1] = True while q: 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,538
s221152231
p02239
u560996731
1589420269
Python
Python3
py
Accepted
30
6064
738
from collections import deque N = int(input()) g = {i: deque() for i in range(1,N+1)} #1-index for i in range(1, N+1): u, k, *v = map(int, input().split()) for j in v: g[u].append(j) seen = [0]*(N+1) #1-index que = deque([]) dist = [-1]*(N+1) #1-index seen[1] = 1 que.append(1) dist[1] = 0 whi...
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,539
s835664664
p02239
u330854367
1589128208
Python
Python3
py
Accepted
30
6072
931
import collections,sys def I(): return int(sys.stdin.readline().rstrip()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) n = I() ukv = [LI() for _ in range(n)] graph = {i:collections.deque() for i in range(1,n+1)} #1_indexed for x in ukv: u,k,*v = x for y in v: graph[u].append(y) ...
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,540
s021770541
p02239
u316065794
1589093061
Python
Python3
py
Accepted
30
6036
511
from collections import deque n = int(input()) G = [] for _ in range(n): tmp = list(map(int, input().split())) u = tmp.pop(0) k = tmp.pop(0) t = [] for i in range(k): a = tmp.pop(0) t.append(a-1) G.append(t) dist = [-1]*n dist[0] = 0 que = deque() que.append(0) while 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,541
s949994462
p02239
u145526353
1589001859
Python
Python3
py
Accepted
20
6036
502
from collections import deque n = int(input()) graph = [[] for _ in range(n)] for i in range(n): graph[i] += list(map(lambda x: x - 1, list(map(int, input().split()))[2:])) visited = [-1] * n que = deque() # 頂点0を入れておく que.append(0) visited[0] = 0 cnt = 0 while que: v = que.popleft() for nv in graph[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,542
s534183902
p02239
u822971508
1588778525
Python
Python3
py
Accepted
20
5704
929
# -*- coding:utf-8 -*- def solve(): import sys import heapq N = int(sys.stdin.readline()) graph = [] for _ in range(N): _in = list(map(int, sys.stdin.readline().split())) if len(_in) == 2: graph.append([]) continue u, k, Vs = _in[0], _in[1], _in[2:] ...
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,543
s768486799
p02239
u804526325
1588753912
Python
Python3
py
Accepted
30
6036
423
from collections import deque n = int(input()) edges = [[]] + [list(map(int, input().split())) for _ in range(n)] inf = 10 ** 4 d = [inf for _ in range(n+1)] que = deque() d[1] = 0 que.append(1) while que: cur = que.popleft() for to in edges[cur]: if d[to] == inf: d[to] = d[cur] + 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,544
s367690403
p02239
u555063680
1588381799
Python
Python3
py
Accepted
20
5632
570
N = int(input()) edge = [[] for _ in range(N)] for _ in range(N): l = list(map(int, input().split())) for j in range(l[1]): edge[l[0]-1].append(l[2+j]-1) now_list = [0] next_list = [] dist = 0 dists = [-1] * N while len(now_list) > 0: for _ in range(len(now_list)): v = now_list.pop() ...
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,545
s185721446
p02239
u481381669
1588353246
Python
Python3
py
Accepted
30
6020
456
from collections import deque n = int(input()) g = [[] for _ in range(n)] for _ in range(n): u, k, *V = list(map(int, input().split())) V.sort() for v in V: g[u-1].append(v-1) # g[v-1].append(u-1) d = [-1] * n d[0] = 0 que = deque([]) que.append(0) while que: v = que.popleft() for...
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,546
s450271607
p02239
u720435112
1588324457
Python
Python3
py
Accepted
30
6076
711
from collections import deque n = int(input()) G = [[0]*n for _ in range(n)] for _ in range(n): u, k, *V = map(int, input().split()) if k == 0: continue else: for v in V: G[u-1][v-1] = 1 def bfs(u=0): Q = deque([]) color = ['White']*n d = [-1]*n d[0] = 0 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,547
s343469554
p02239
u486183171
1588317223
Python
Python3
py
Accepted
40
6876
399
import queue N = int(input()) G = [] for _ in range(N): u, n, *K = map(int, input().split()) G.append((u - 1, [k - 1 for k in K])) Q = queue.Queue() Q.put(G[0]) dist = [-1] * N dist[0] = 0 while not Q.empty(): U = Q.get() for u in U[1]: if dist[u] == -1: Q.put(G[u]) dis...
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,548
s644485195
p02239
u940492895
1588016750
Python
Python3
py
Accepted
40
6876
451
# 幅優先探索 Breadth first search: BFS import queue n = int(input()) Adj = [[] for i in range(n)] for i in range(n): dat = [int(i) for i in input().split()] Adj[dat[0] - 1] = dat[2:] D = [-1 for i in range(n)] D[0] = 0 Q = queue.Queue() Q.put(1) while not Q.empty(): u = Q.get() for v in Adj[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,549
s059168625
p02239
u980837785
1587786454
Python
Python3
py
Accepted
30
6040
447
from collections import deque N = int(input()) graph = [[] for _ in range(N)] for i in range(N): X = list(map(int, input().split())) for j in range(2, len(X)): graph[i].append(X[j]-1) dist = [-1] * N dist[0] = 0 q = deque([]) q.append(0) while q: x = q.popleft() for next_x in graph[x]: if(dist[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,550
s052020898
p02239
u174107425
1587481784
Python
Python3
py
Accepted
20
6036
581
from collections import deque def bfs(start): Q = deque() Q.append(start) dist[start] = 0 while Q: v = Q.popleft() for nv in G[v]: if dist[nv] != -1: continue # print(Q) dist[nv] = dist[v] + 1 Q.append(nv) N = int(inp...
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,551
s827796447
p02239
u069607647
1587453720
Python
Python3
py
Accepted
30
6024
548
from collections import deque import sys read = sys.stdin.read readline = sys.stdin.readline def main(): n = int(readline()) E = [[]] for _ in range(n): u,k,*v = map(int, readline().split()) E.append(v) d = [-1] * (n+1) queue = deque([1]) d[1] = 0 while queue: ...
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,552
s293214986
p02239
u909811933
1587388726
Python
Python3
py
Accepted
30
6032
494
n = int(input()) G = [[0]]+[list(map(int, input().split()))[2:] for _ in range(n)] # print(G) from collections import deque d = deque([1]) dist = [10**10 for _ in range(n+1)] dist[1] = 0 while len(d) != 0: now = d.popleft() # print(now) for nexte in G[now]: if dist[now] +1 < dist[nexte]: ...
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,553
s456558596
p02239
u647694976
1587353031
Python
Python3
py
Accepted
20
6044
604
from collections import deque def BFS(num): color=["white" for _ in range(n+1)] D=[-1 for _ in range(n+1)] color[num]="gray" D[num]=0 queue=deque([num]) while len(queue)>0: u=queue.popleft() for i in M[u]: if color[i]=="white": color[i]="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,554
s436600824
p02239
u690713461
1587273706
Python
Python3
py
Accepted
30
6132
895
import sys, math from itertools import permutations, combinations from collections import defaultdict, Counter, deque from math import factorial#, gcd from bisect import bisect_left #bisect_left(list, value) sys.setrecursionlimit(10**7) enu = enumerate MOD = 10**9+7 def input(): return sys.stdin.readline()[:-1] def pri...
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,555
s908992276
p02239
u776243834
1587249343
Python
Python3
py
Accepted
30
6036
378
from collections import deque n = int(input()) vdata = [list(map(int,input().split())) for _ in range(n)] d = [-1]*n q = deque() q.append(vdata[0]) d[0] = 0 while len(q) > 0: node = q.popleft() c = d[node[0]-1] a = node[1] for i in range(a): b = node[i+2]-1 if d[b] == -1: d[b] = c+1 q.appe...
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,556
s545538490
p02239
u259225866
1587116797
Python
Python3
py
Accepted
30
6088
1,108
import sys input = sys.stdin.readline #入力と違って0オリジンとなっているので注意 n = int(input()) li = [[0] * n for _ in range(n)] #隣接行列、0オリジン li[i][j]:iからjへの有向グラフがある for i in range(n): x, y, *v = [int(x)-1 for x in input().split()] for j in v: li[i][j] = 1 #有向グラフがあるところは1、ないところは0となる from collections import deque Q = dequ...
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,557
s592117127
p02239
u332878765
1587024865
Python
Python3
py
Accepted
30
6036
429
from collections import deque n = int(input()) graph = [[0]] for _ in range(n): u, k, *v = map(int, input().split()) graph.append(v) queue = deque([1]) ans = [-1 for _ in range(n + 1)] ans[1] = 0 while queue: cur = queue.popleft() for next in graph[cur]: if ans[next] != -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,558
s317651569
p02239
u531262486
1587019926
Python
Python3
py
Accepted
30
6036
555
from collections import deque graph = [] n = int(input()) dist = [[int(c),-1] for c in range(1,n+1)] process_q = deque() for i in range(n): graph.append([c-1 for c in list(map(int,input().split()))][2:]) def bfs(): while len(process_q) > 0: parent = process_q.popleft() for child in graph[parent...
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,559
s807034923
p02239
u496924602
1587015100
Python
Python3
py
Accepted
30
6008
550
from collections import deque n = int(input()) g = [None] * (n+1) ans = [0] * (n+1) for _ in range(n): u,k,*vk = map(int,input().split()) g[u] = vk def bfs(now): que = deque() que.append(now) while que.__len__() != 0: now = que.popleft() for ne in g[now]: if ans[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,560
s947854894
p02239
u951519498
1586877275
Python
Python3
py
Accepted
30
6020
1,046
#!/usr/bin/env python # -*- coding: utf-8 -*- # # FileName: breadth_first_search # CreatedDate: 2020-04-14 23:40:14 +0900 # LastModified: 2020-04-15 00:14:09 +0900 # import os import sys from collections import deque def bfs(matrix,n): s=0 color=[0]*n color[s]=1 Q=deque() Q.append(s) distan...
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,561
s861009769
p02239
u512884261
1586781588
Python
Python3
py
Accepted
30
6032
530
import sys input = lambda: sys.stdin.readline().rstrip() from collections import deque def resolve(): n = int(input()) ukv = [list(map(int, input().split())) for _ in range(n)] dist = [-1]*n que = deque() que.append(0) dist[0] = 0 while len(que)>0: v = que.popleft() for 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,562
s879500602
p02239
u486404145
1586769387
Python
Python3
py
Accepted
30
6040
735
# -*- coding: utf-8 -*- import sys from collections import deque sys.setrecursionlimit(10**9) INF=10**18 MOD=10**9+7 input=lambda: sys.stdin.readline().rstrip() YesNo=lambda b: print('Yes' if b else 'No') YESNO=lambda b: print('YES' if b else 'NO') def main(): N=int(input()) edge=[[] for _ in range(N)] for...
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,563
s953619149
p02239
u013374192
1586352216
Python
Python3
py
Accepted
50
7020
1,403
import math import copy import sys import fractions # import numpy as np # import statistics import decimal import heapq import collections import itertools import bisect # from operator import mul sys.setrecursionlimit(100001) # input = sys.stdin.readline # sys.setrecursionlimit(10**6) # ===FUNCTION=== def getIn...
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,564
s102598206
p02239
u344362704
1586323546
Python
Python3
py
Accepted
30
6032
423
from collections import deque N = int(input()) graph = [list(map(int, input().split())) for _ in range(N)] seen = [-1]*(N+1) todo = deque() def bfs(v): u = v-1 next_v = graph[u][2:] for i in next_v: if seen[i] == -1: seen[i] = seen[v] + 1 todo.append(i) todo.append(1) se...
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,565
s355950946
p02239
u902441874
1586100312
Python
Python3
py
Accepted
20
5616
901
#from collections import deque import sys readline = sys.stdin.readline sys.setrecursionlimit(10**6) #def mypopleft(que, index): # if len(que) > index: # return que.pop(index) # else: # return [] def main(): # input n = int(input()) graph = [] for i in range(n): u, k, *t...
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,566
s977729636
p02239
u115626571
1585978046
Python
Python3
py
Accepted
30
6004
517
from collections import deque N = int(input()) #グラフ入力受取 G = [[] for _ in range(N)] for _ in range(N): a = list(map(int, input().split())) for i in a[2:]: G[a[0]-1].append(i-1) #頂点1をスタートとした探索 dist = [-1]*N que = deque() dist[0] = 0 que.append(0) while len(que)!=0: v = que.popleft() for nv in G[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,567
s294372916
p02239
u820842907
1585940501
Python
Python3
py
Accepted
30
6076
822
from collections import deque def bfs(adj_mat, d): Q = deque() N = len(d) color = ["W" for n in range(N)] Q.append(0) d[0] = 0 while len(Q) != 0: u = Q.popleft() for i in range(N): if adj_mat[u][i] == 1 and color[i] == "W": color[i] = "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,568
s189380086
p02239
u622912304
1585927758
Python
Python3
py
Accepted
30
6036
869
# https://onlinejudge.u-aizu.ac.jp/problems/ALDS1_11_C from collections import deque class Data: def __init__(self, index, depth): super().__init__() self.index = index self.depth = depth if __name__ == "__main__": n = int(input()) d = {} for _ in range(n): u, k, *li...
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,569
s287340287
p02239
u258049727
1585807868
Python
Python3
py
Accepted
30
6100
563
from collections import deque n = int(input()) M = [[0]*n for _ in range(n)] d = [-1]*n st = [0]*n for _ in range(n): i = [int(i) for i in input().split()] u = i[0] k = i[1] V = i[2:] for v in V: M[u-1][v-1] = 1 def bfs(s): Q = deque() Q.append(s) st[s] = 1 d[s] = 0 while 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,570
s419240257
p02239
u044232230
1585748924
Python
Python3
py
Accepted
30
6032
406
from collections import deque n = int(input()) adj = [[] for _ in range(n)] for i in range(n): u,k,*v = map(int, input().split()) v = [w-1 for w in v] adj[u-1].extend(v) queue = deque([0]) ans = [-1] * n ans[0] = 0 while queue: now = queue.popleft() for u in adj[now]: if ans[u] < 0:# 未訪問 ans[...
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,571
s406635177
p02239
u737337423
1585738101
Python
Python3
py
Accepted
30
6040
516
# Breadth First Search from collections import deque import sys input = sys.stdin.readline n = int(input()) E = (tuple(map(int, input().split())) for _ in range(n)) E = [[v-1 for v in V] for i, k, *V in E] q = deque() visited = [False]*n D = [-1]*n # 初期条件 s = 0 q.append(s) visited[s] = True D[s] = 0 # BFS while 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,572
s391951090
p02239
u838900850
1585669721
Python
Python3
py
Accepted
30
6036
616
from collections import deque def main(): inf = 1000000007 n = int(input()) e = [[] for _ in range(n)] d = [inf]*n for i in range(n): m = list(map(int,input().split())) for j in range(2,len(m)): e[m[0]-1].append(m[j]-1) d[0] = 0 dq = deque([]) dq.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,573
s893932520
p02239
u037820925
1585556739
Python
Python3
py
Accepted
30
6036
474
from collections import deque INF = 1145141919810364364334 s = [None] r = [None] n = int(input()) for _ in range(n): temp = list(map(int,input().split())) s.append(temp[2:]) d = [INF] * (n+1) d[1] = 0 q = deque() q.append(1) while q: x = q.popleft() for i in s[x]: if d[i] == 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,574
s691533693
p02239
u951319620
1585494761
Python
Python3
py
Accepted
60
9040
724
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time from collections import deque sys.setrecursionlimit(10**7) inf = 10**20 mod = 10**9 + 7 stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().rstrip() # ign...
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,575
s205291723
p02239
u665008200
1585487258
Python
Python3
py
Accepted
30
6032
526
import sys from collections import deque input = sys.stdin.readline sys.setrecursionlimit(2 * 10**6) def inpl(): return list(map(int, input().split())) N = int(input()) edge = {} for _ in range(N): tmp = inpl() u, k = tmp[:2] edge[u] = tmp[2:] d = {i: -1 for i in range(1, N + 1)} d[1] = 0 Q = dequ...
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,576
s039649726
p02239
u176870835
1585485329
Python
Python3
py
Accepted
20
5632
509
#!/usr/bin/env python3 import sys sys.setrecursionlimit(15000) n = int(sys.stdin.readline()) ret = [] inp = sys.stdin.readlines() G = [None]*(n+1) depths = [-1]*(n+1) for i in range(1,n+1): v = list(map(int,inp[i-1].split())) G[v[0]] = list(map(int,v[2:])) #print(G) nodes = [1] depth = 0 while nodes: new = [] ...
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,577
s134048904
p02239
u942920884
1585341703
Python
Python3
py
Accepted
40
6872
534
import queue def bfs(g, s=1): global q dist = [-1] * len(g) dist[1] = 0 q = queue.Queue() q.put(s) while not q.empty(): v = q.get() if not G[v][1]: continue for vi in G[v][2:]: if dist[vi] >= 0: continue q.put(vi) ...
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,578
s786270994
p02239
u870171689
1585318968
Python
Python3
py
Accepted
30
6016
672
#!/usr/bin/env python3 from collections import deque n = int(input()) edges = [[] for _ in range(n)] for _ in range(n): line = list(map(int, input().split())) u = line[0] - 1 if len(line) != 2: v_list = line[1:] for v in v_list: edges[u].append(v - 1) # print(n, edges) dist =...
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,579
s627889525
p02239
u560613143
1585310541
Python
Python3
py
Accepted
30
6032
514
n=int(input()) G=[] for i in range(n): u,k,*l=map(int,input().split()) G.append(l) #print(G) #BFS from collections import deque def bfs(G,v,p): que=deque() que.append((v,p)) log=[-1]*n log[0]=0 while que: #queの先頭を取り出す V,P=que.popleft() for next_v in G[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,580
s580090297
p02239
u490760133
1585220130
Python
Python3
py
Accepted
30
6028
511
from collections import deque n = int(input()) g = [list(map(int, input().split()))[2:] for _ in range(n)] que = deque([0]) dist = [-1] * n dist[0] = 0 # 初期条件 while que: # queが空なるまで u = que.popleft() # queの左から順に取り出す for v in g[u]: v -= 1 # 0-indexedに変更 if dist[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,581