Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list

Introduction

This theme

AtCoder Beginner Contest D - .. (Double Dots) Difficulty: 750

This theme, adjacency list

Breadth-first search is performed from the adjacency list, but the shortest route is obtained by recording the route from the room closest to room 1 and skipping the recorded room. Ruby

ruby.rb


n, m = gets.split.map(&:to_i)
a = Array.new(n){[]}
m.times do
    x, y = gets.split.map(&:to_i)
    a[x - 1] << y - 1
    a[y - 1] << x - 1
end
c = Array.new(n){0}
que = []
que << 0
while que.size > 0
  e = que.shift
  a[e].each do |i|
    next if c[i] > 0
    c[i] = e + 1
    que << i
  end
end
puts "Yes", c[1..-1]

index.rb


    a[x - 1] << y - 1
    a[y - 1] << x - 1

Adapted to array indexes starting at 0. Python

pypy.py


from collections import deque

n, m = map(int, input().split())
a = [[] for _ in range(n)]
for i in range(m):
    x, y = map(int, input().split())
    a[x - 1].append(y - 1)
    a[y - 1].append(x - 1)
c = [0] * n
que = deque([])
que.append(0)
while len(que) > 0:
    e = que.popleft()
    for i in a[e]:
        if c[i] > 0:
            continue
        c[i] = e + 1
        que.append(i)
print("Yes")
for i in range(1, n):
    print(c[i])

Python (networkx)

networkx.py


import networkx

n, m = map(int, input().split())
g = networkx.Graph()
g.add_nodes_from(list(range(n)))
for i in range(1, m + 1):
    a, b = map(int, input().split())
    g.add_edge(a, b)
#print(g.nodes())
#print(g.edges())
print("Yes")
for i in range(2, n + 1):
    print(networkx.shortest_path(g, 1, i)[-2])

shortest_path.py


    print(networkx.shortest_path(g, 1, i)[-2])

You can get the shortest path with networkx.shortest_path. ~~ No breadth-first search is required ~~ Fortunately, this problem is TLE, but networkx is amazing.

Ruby Python PyPy Python (networkx)
Code length(Byte) 335 459 459 321
Execution time(ms) 335 696 451 TLE
memory(KB) 32748 36920 94388 187248

Summary

Referenced site networkx Tutorial

Recommended Posts

Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solve AtCoder ABC168 with python (A ~ D)
Solving with Ruby AtCoder ABC110 C String Manipulation
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solve AtCoder ABC166 with python
AtCoder ARC080 D simulation solved in Ruby and Python
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
AtCoder ABC 182 Python (A ~ D)
Solve AtCoder ABC 186 with Python
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
AtCoder ABC155 Problem D Pairs Review Note 2 NumPy and Python
AtCoder ABC168 A case expression solved in Ruby and Python
Scraping with Node, Ruby and Python
Solve ABC166 A ~ D with Python
Solved AtCoder ABC 114 C-755 with Python3
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder ABC 174 Python
AtCoder ABC 175 Python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Solving the Lorenz 96 model with Julia and Python
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
Solve AtCoder 167 with python
Ruby, Python and map
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
Initialize list with python
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!