Problem:


両端にリングのついた紐を考える.リングは正整数が付いていて,紐を区別する.紐の両端のリングには異なる数 a, b が付けられる.これを,[a, b] と記述する.複数の紐があり,一方の紐と他方の紐のリングに付いている数が同じ場合,そのリングのところで,これらの紐をつなげることができて,つなげてできたものを鎖と呼ぶことにする.例えば,[1, 3] と [3, 4] という紐からは [1, 3, 4] という鎖ができる.紐と鎖や鎖同志も同じ整数が付いているリングのところでつなげることができるものとする.

例えば,鎖 [1, 3, 4] と紐 [5, 1] からは [5, 1, 3, 4] ができ,鎖 [1, 3, 4] と鎖 [2, 3, 5] からは, 中央でクロスしたような形になる.鎖 [1, 3, 4] と鎖 [4, 6, 1] からは, 輪の形ができる.

このように様々な形ができるが, そのうちの一部で,同じ数の付いたリングは一度だけたどるつながった複数の紐を鎖と呼ぶことにする.例えば,鎖 [1, 3, 4] と鎖[2, 3, 5] からできる,中央でクロスしたような形には, [1, 3, 5],[2, 3, 4] という鎖も含まれ,鎖 [1, 3, 4] と鎖 [4, 6, 1] からできる輪には, [1, 3, 4, 6],[3, 4, 6, 1],[4, 6, 1, 3]などの鎖が含まれる.

これらの鎖に対して,含まれる数の個数を長さと定義する. 与えられた複数の紐に対して,つなげられるものはすべてつなげると1つ以上の
鎖を含む形ができる.そのうちの最大の鎖の長さを求めるプログラムを作成せよ.

n ≤ 100 が書いてあり,つづく n 行のそれぞれには, 空白で区切られた 2 つの整数 a, b が書いてあり 1 ≤ a < b ≤  100 となっている.各行の 2 つの整数は 1 つの紐の両端の整数を表わす.

   出力ファイルのファイル名は “output.txt” である.“output.txt” においては,出力(最大の鎖の長さ)の後に改行を入れること.

-->

   入力データ の最初の行には紐の個数である正整数 1 ≤ n ≤ 100 が書いてあり,つづく n 行のそれぞれには, 空白で区切られた 2 つの整数 a, b が書いてあり 1 ≤ a < b ≤  100 となっている.各行の 2 つの整数は 1 つの紐の両端の整数を表わす.

 

  入力例１
  入力例２
  入力例３
 

7  6  7  

1 3  1 2  1 3  

3 4  2 3  2 4  

1 4  3 4  3 5  

2 7  4 5  4 6  

5 7  1 5  6 7  

6 7  2 6  2 6  

1 7    4 7  

  　

  出力例１
  出力例２
  出力例３

564

入力

入力は複数のデータセットからなる．n が 0 のとき入力が終了する．データセットの数は 10 を超えない．

出力

データセットごとに、最大の鎖の長さを１行に出力する．

入力例

7
1 3
3 4
1 4
2 7
5 7
6 7
1 7
6
1 2
2 3
3 4
4 5
1 5
2 6
7
1 3
2 4
3 5
4 6
6 7
2 6
4 7
0

出力例

5
6
4

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
7
1 3
3 4
1 4
2 7
5 7
6 7
1 7
6
1 2
2 3
3 4
4 5
1 5
2 6
7
1 3
2 4
3 5
4 6
6 7
2 6
4 7
0


Sample output:
5
6
4

Submission status: Wrong Answer

Solution code:
import sys

fp = sys.stdin

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    for i in xrange(n):
        buf = map(int , fp.readline()[:-1].split(" "))

    print "hoge"

Submission status: Accepted

Solution code:
def solve():
    def dfs(ring, chain_len=1):
        unchecked[ring] = False
        unvisited[ring] = False
        len_rec[ring] = max(len_rec[ring], chain_len)
        for r in adj_list[ring]:
            if unvisited[r]:
                dfs(r, chain_len + 1)
        unvisited[ring] = True
    
    import sys
    file_input = sys.stdin
    
    while True:
        n = int(file_input.readline())
        if n == 0:
            break
        
        rings = []
        adj_list = [[] for _ in range(101)]
        for _ in range(n):
            a, b = map(int, file_input.readline().split())
            adj_list[a].append(b)
            adj_list[b].append(a)
            rings.extend([a, b])
        rings = set(rings)
        
        len_rec = [1] * 101
        unchecked = [True] * 101
        unvisited = [True] * 101
        
        for r in rings:
            if unchecked[r]:
                dfs(r)
        dfs(len_rec.index(max(len_rec)))
        
        print(max(len_rec))

solve()

Submission status: Runtime Error

Solution code:
from itertools import combinations


def connect(history, last):
    global links, chains
    branched = False
    for ring in links[last]:
        if ring in history:
            continue
        history.append(ring)
        connect(history, ring)
        history.pop()
        branched = True
    if not branched:
        chains.append(set(history))


while True:
    n = int(input())
    if not n:
        break
    links = {i: set() for i in range(1, 101)}

    for _ in range(n):
        a, b = map(int, input().split())
        links[a].add(b)
        links[b].add(a)

    links = {i: link for i, link in links.items() if link}
    rings = set(links.keys())

    current_max = 0

    while rings:
        chains = []
        first = rings.pop()
        connect([first], first)
        for chain1, chain2 in combinations(chains, 2):
            if len(chain1 & chain2) == 1:
                current = len(chain1) + len(chain2) - 1
                current_max = max(current, current_max)
            else:
                current_max = max(current_max, len(chain1), len(chain2))

        for chain in chains:
            rings -= chain
    print(current_max)

Submission status: Runtime Error

Solution code:
def connect(history, last):
    global links, chains
    branched = False
    for ring in links[last]:
        if ring in history:
            continue
        history.append(ring)
        connect(history, ring)
        history.pop()
        branched = True
    if not branched:
        chains.append(set(history))


while True:
    n = int(input())
    if not n:
        break
    links = {i: set() for i in range(1, 101)}

    for _ in range(n):
        a, b = map(int, input().split())
        links[a].add(b)
        links[b].add(a)

    links = {i: link for i, link in links.items() if link}
    rings = set(links.keys())

    current_max = 0

    while rings:
        chains = []
        first = rings.pop()
        connect([first], first)
        for chain in chains:
            current_max = max(current_max, len(chain))
    print(current_max)

Submission status: Runtime Error

Solution code:
def connect(history, last):
    global links, max_length
    branched = False
    for ring in links[last]:
        if ring in history:
            continue
        history.append(ring)
        connect(history, ring)
        history.pop()
        branched = True
    if not branched:
        max_length = max(max_length, len(history))


while True:
    n = int(input())
    if not n:
        break
    links = {i: set() for i in range(1, 101)}

    for _ in range(n):
        a, b = map(int, input().split())
        links[a].add(b)
        links[b].add(a)

    links = {i: link for i, link in links.items() if link}
    rings = set(links.keys())

    max_length = 0

    while rings:
        start = rings.pop()
        connect([start], start)
    print(max_length)

Submission status: Runtime Error

Solution code:
def connect(last):
    global links, used
    used[last] = 1
    result = max([connect(ring) + 1 for ring in links[last] if not used[ring]], default=0)
    used[last] = 0
    return result


used = [0] * 101

while True:
    n = int(input())
    if not n:
        break
    links = [set() for i in range(101)]

    for _ in range(n):
        a, b = map(int, input().split())
        links[a].add(b)
        links[b].add(a)

    rings = set(i for i, l in enumerate(links) if l)

    max_length = max([connect(ring) + 1 for ring in rings], default=2)

    print(max_length)

Submission status: Runtime Error

Solution code:
from queue import deque


def longest(i):
    visited = (i,)
    q = deque((r, visited) for r in links[i])
    while q:
        ring, visited = q.popleft()
        for r in links[ring]:
            if r not in visited:
                q.append((r, visited + (ring,)))
    return len(visited) + 1


while True:
    n = int(input())
    if not n:
        break

    links = [set() for _ in range(n)]
    for _ in range(n):
        s, t = map(int, input().split())
        s, t = s - 1, t - 1
        links[s].add(t)
        links[t].add(s)

    print(max(longest(i) for i in range(n)))

Submission status: Runtime Error

Solution code:
from collections import defaultdict                                              
                                                                                 
                                                                                 
class GraphChain():                                                              
  def __init__(self):                                                            
    self.graph = defaultdict(set)                                                
    self.passed = defaultdict(bool)                                              
    self.route = defaultdict(bool)                                               
    self.ans = 0                                                                 
                                                                                 
  def dfs(self, left, count):                                                    
    '''                                                                          
      depth first search                                                         
    '''                                                                          
    for i in range(len(self.graph[left])):                                       
      if self.passed[self.graph[left][i]] == False:                              
        # mark passed point                                                      
        self.passed[self.graph[left][i]] = True                                  
                                                                                 
        # if next route is more than two, this node is never become start point. 
        if len(self.graph[left]) > 2:                                            
          self.route[self.graph[left][i]] = True                                 
        temp = self.dfs(self.graph[left][i], count + 1)                          
        self.ans = max(self.ans, temp)                                           
                                                                                 
        # delete mark of point.                                                  
        self.passed[self.graph[left][i]] = False                                 
                                                                                 
    return count                                                                 
                                                                                 
  def get_string_pair(self, num):                                                
    '''                                                                          
      get string and make dictionary.                                            
    '''                                                                          
    self.graph = defaultdict(list)                                               
    for i in range(num):                                                         
      left, righ = [int(j) for j in input().strip().split()]                     
      self.graph[left].append(righ)                                              
      self.graph[righ].append(left)                                              
                                                                                 
    return self.graph                                                            
                                                                                 
  def count_chain(self, num):                                                    
    '''                                                                          
      main function of this problem.                                             
    '''                                                                          
    self.get_string_pair(num)                                                    
                                                                                 
    for key in sorted(self.graph.keys()):                                        
      if self.route[key] == False:                                               
        self.dfs(key, 0)                                                         
    return self.ans                                                              
                                                                                 
                                                                                 
while 1:                                                                         
  gc = GraphChain()                                                              
  x = int(input())                                                               
  if x == 0:                                                                     
    break                                                                        
  print(gc.count_chain(x))

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class GraphChain():
  def __init__(self):
    self.graph = defaultdict(set)
    self.passed = defaultdict(bool)
    self.route = defaultdict(bool)
    self.ans = 0

  def dfs(self, left, count):
    '''
      depth first search
    '''
    for i in range(len(self.graph[left])):
      if self.passed[self.graph[left][i]] == False:
        # mark passed point
        self.passed[self.graph[left][i]] = True

        # if next route is more than two, this node is never become start point.
        if len(self.graph[left]) > 2:
          self.route[self.graph[left][i]] = True
        temp = self.dfs(self.graph[left][i], count + 1)
        self.ans = max(self.ans, temp)

        # delete mark of point.
        self.passed[self.graph[left][i]] = False

    return count

  def get_string_pair(self, num):
    '''
      get string and make dictionary.
    '''
    self.graph = defaultdict(list)
    for i in range(num):
      left, righ = [int(j) for j in input().strip().split()]
      self.graph[left].append(righ)
      self.graph[righ].append(left)

    return self.graph

  def count_chain(self, num):
    '''
      main function of this problem.
    '''
    self.get_string_pair(num)

    for key in sorted(self.graph.keys()):
      if self.route[key] == False:
        self.dfs(key, 0)
    return self.ans


while 1:
  gc = GraphChain()
  x = int(input())
  if x == 0:
    break
  print(gc.count_chain(x))

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class GraphChain():
  def __init__(self):
    self.graph = defaultdict(set)
    self.passed = defaultdict(bool)
    self.route = defaultdict(bool)
    self.ans = 0

  def dfs(self, left, count):
    '''
      depth first search
    '''
    for i in range(len(self.graph[left])):
      if self.passed[self.graph[left][i]] == False:
        # mark passed point
        self.passed[self.graph[left][i]] = True

        # if next route is more than two, this node is never become start point.
        if len(self.graph[left]) > 2:
          self.route[self.graph[left][i]] = True
        temp = self.dfs(self.graph[left][i], count + 1)
        self.ans = max(self.ans, temp)

        # delete mark of point.
        self.passed[self.graph[left][i]] = False

    return count

  def get_string_pair(self, num):
    '''
      get string and make dictionary.
    '''
    self.graph = defaultdict(list)
    for i in range(num):
      left, righ = [int(j) for j in input().strip().split()]
      self.graph[left].append(righ)
      self.graph[righ].append(left)

    return self.graph

  def count_chain(self, num):
    '''
      main function of this problem.
    '''
    self.get_string_pair(num)

    for key in sorted(self.graph.keys()):
      if self.route[key] == False:
        self.dfs(key, 1)
    return self.ans - 1


while 1:
  gc = GraphChain()
  x = int(input())
  if x == 0:
    break
  print(gc.count_chain(x))

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class GraphChain():
  def __init__(self):
    self.graph = defaultdict(set)
    self.passed = defaultdict(bool)
    self.route = defaultdict(bool)
    self.ans = 0

  def dfs(self, left, count):
    '''
      depth first search
    '''
    for i in range(len(self.graph[left])):
      if self.passed[self.graph[left][i]] == False:
        # mark passed point
        self.passed[self.graph[left][i]] = True

        # if next route is more than two, this node is never become start point.
        if len(self.graph[left]) > 2:
          self.route[self.graph[left][i]] = True
        temp = self.dfs(self.graph[left][i], count + 1)

        # delete mark of point.
        self.passed[self.graph[left][i]] = False

    return count

  def get_string_pair(self, num):
    '''
      get string and make dictionary.
    '''
    self.graph = defaultdict(list)
    for i in range(num):
      left, righ = [int(j) for j in input().strip().split()]
      self.graph[left].append(righ)
      self.graph[righ].append(left)

    return self.graph

  def count_chain(self, num):
    '''
      main function of this problem.
    '''
    self.get_string_pair(num)

    for key in sorted(self.graph.keys()):
      if self.route[key] == False:
        self.dfs(key, 1)
    return self.ans - 1


while 1:
  gc = GraphChain()
  x = int(input())
  if x == 0:
    break
  print(gc.count_chain(x))

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class GraphChain():
  def __init__(self):
    self.graph = defaultdict(set)
    self.passed = defaultdict(bool)
    self.route = defaultdict(bool)
    self.ans = 0

  def dfs(self, left, count):
    '''
      depth first search
    '''
    for i in range(len(self.graph[left])):
      if self.passed[self.graph[left][i]] == False:
        # mark passed point
        self.passed[self.graph[left][i]] = True

        # if next route is more than two, this node is never become start point.
        if len(self.graph[left]) > 2:
          self.route[self.graph[left][i]] = True
        temp = self.dfs(self.graph[left][i], count + 1)
        self.ans = self.ans if self.ans > temp else temp

        # delete mark of point.
        self.passed[self.graph[left][i]] = False

    return count

  def get_string_pair(self, num):
    '''
      get string and make dictionary.
    '''
    self.graph = defaultdict(list)
    for i in range(num):
      left, righ = [int(j) for j in input().strip().split()]
      self.graph[left].append(righ)
      self.graph[righ].append(left)

    return self.graph

  def count_chain(self, num):
    '''
      main function of this problem.
    '''
    self.get_string_pair(num)

    for key in sorted(self.graph.keys()):
      if self.route[key] == False:
        self.dfs(key, 1)

    return self.ans - 1


while 1:
  gc = GraphChain()
  x = int(input())
  if x == 0:
    break
  print(gc.count_chain(x))
  del gc

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class GraphChain():
  def __init__(self):
    self.graph = defaultdict(set)
    self.passed = defaultdict(bool)
    self.route = defaultdict(bool)
    self.ans = 0

  def dfs(self, left, count):
    '''
      depth first search
    '''
    for i in range(len(self.graph[left])):
      if self.passed[self.graph[left][i]] == False:
        # mark passed point
        self.passed[self.graph[left][i]] = True

        # if next route is more than two, this node is never become start point.
        if len(self.graph[left]) > 2:
          self.route[self.graph[left][i]] = True
        temp = self.dfs(self.graph[left][i], count + 1)
        self.ans = self.ans if self.ans > temp else temp

        # delete mark of point.
        self.passed[self.graph[left][i]] = False

    return count

  def get_string_pair(self, num):
    '''
      get string and make dictionary.
    '''
    self.graph = defaultdict(list)
    for i in range(num):
      left, righ = [int(j) for j in input().strip().split()]
      self.graph[left].append(righ)
      self.graph[righ].append(left)

    return self.graph

  def count_chain(self, num):
    '''
      main function of this problem.
    '''
    self.get_string_pair(num)
    print(self.graph)
    for key in sorted(self.graph.keys()):
      if self.route[key] == False:
        self.dfs(key, 1)

    return self.ans - 1


while 1:
  gc = GraphChain()
  x = int(input())
  if x == 0:
    break
  print(gc.count_chain(x))
  del gc

Submission status: Runtime Error

Solution code:
from collections import defaultdict


class GraphChain():
  def __init__(self):
    self.graph = defaultdict(set)
    self.passed = defaultdict(bool)
    self.route = defaultdict(bool)
    self.ans = 0

  def dfs(self, left, count):
    '''
      depth first search
    '''
    for point in self.graph[left]:
      if self.passed[point] == False:
        # mark passed point
        self.passed[point] = True

        # if next route is more than two, this node is never become start point.
        if len(self.graph[left]) > 2:
          self.route[point] = True
        temp = self.dfs(point, count + 1)
        self.ans = self.ans if self.ans > temp else temp

        # delete mark of point.
        self.passed[point] = False

    return count

  def get_string_pair(self, num):
    '''
      get string and make dictionary.
    '''
    self.graph = defaultdict(set)
    for i in range(num):
      left, righ = (int(j) for j in input().strip().split())
      self.graph[left].add(righ)
      self.graph[righ].add(left)

    return self.graph

  def count_chain(self, num):
    '''
      main function of this problem.
    '''
    self.get_string_pair(num)

    for key in sorted(self.graph.keys()):
      if self.route[key] == False:
        self.dfs(key, 1)

    return self.ans - 1


while 1:
  gc = GraphChain()
  x = int(input())
  if x == 0:
    break
  print(gc.count_chain(x))
  del gc

Submission status: Runtime Error

Solution code:
def solve():
    def dfs(start):
        unvisited = [True] * (n + 1)
        queue = [(start, 1)]
        max_chain_len = 1
        while queue:
            ring, chain_len = queue.pop()
            if unvisited[ring]:
                unvisited[ring] = False
                max_chain_len = max(max_chain_len, chain_len)
                for r in adj_list[ring]:
                    if unvisited[r]:
                        queue.append((r, chain_len + 1))
        return max_chain_len
    
    import sys
    file_input = sys.stdin
    
    while True:
        n = int(file_input.readline())
        if n == 0:
            break
        
        adj_list = [[] for _ in range(n + 1)]
        for _ in range(n):
            a, b = map(int, file_input.readline().split())
            adj_list[a].append(b)
            adj_list[b].append(a)
        
        max_len = 0
        for s in range(1, n + 1):
            max_len = max(max_len, dfs(s))
        
        print(max_len)

solve()

Submission status: Runtime Error

Solution code:
def foo(nw, visited):
    last = visited[-1]
    r = 0
    for k in nw[last]:
        if k not in visited:
            v = foo(nw, visited + (k,))
        else:
            v = len(visited)
        if r < v:
            r = v
    return r


def findlongetstpath(nw):
    return max([foo(nw, (k,)) for k in nw.keys()])


def chunker(f):
    while True:
        line = f.readline()
        line = line.strip()
        dsize = int(line)
        if dsize == 0:
            raise StopIteration
        network = {}
        for i in range(dsize):
            line = f.readline()
            line = line.strip()
            s, d = line.split()
            rel = network.get(s, None)
            if rel is None:
                rel = set()
            rel.add(d)
            network[s] = rel
            rel = network.get(d, None)
            if rel is None:
                rel = set()
            rel.add(s)
            network[d] = rel
        yield network

import sys
for nw in chunker(sys.stdin):
    print  findlongetstpath(nw)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python

def foo(nw, visited):
    last = visited[-1]
    r = 0
    for k in nw[last]:
        if k not in visited:
            v = foo(nw, visited + (k,))
        else:
            v = len(visited)
        if r < v:
            r = v
    return r

def findlongetstpath(nw):
    return max([foo(nw, (k,)) for k in nw.keys()])

def chunker(f):
    while True:
        line = f.readline()
        line = line.strip()
        dsize = int(line)
        if dsize == 0:
            raise StopIteration
        network = {}
        for i in range(dsize):
            line = f.readline()
            line = line.strip()
            s, d = line.split()
            rel = network.get(s, None)
            if rel is None:
                rel = set()
            rel.add(d)
            network[s] = rel
            rel = network.get(d, None)
            if rel is None:
                rel = set()
            rel.add(s)
            network[d] = rel
        yield network

import sys
for nw in chunker(sys.stdin):
    print findlongetstpath(nw)

Submission status: Runtime Error

Solution code:
import sys
from copy import deepcopy

#fp = open("input.txt", "r")
fp = sys.stdin

def countLength(chain, start, prev):
    node = chain[start][:]
    if not node:
        return 0
    chain[start] = []
    max_len = 0
    for i in node:
        if i == prev:
            continue
        cloned = deepcopy(chain)
        length = countLength(cloned, i, start) + 1
        if length > max_len:
            max_len = length
    return max_len

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    chain = [[] for i in xrange(n)]
    for i in xrange(n):
        string = map(int , fp.readline()[:-1].split(" "))
        chain[string[0]-1].append(string[1]-1)
        chain[string[1]-1].append(string[0]-1)

    max_len = 0
    for i in xrange(n):
        cloned = deepcopy(chain)
        length = countLength(cloned, i, -1)
        if length > max_len:
            max_len = length
    print max_len

Submission status: Runtime Error

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

def deepcopy(li):
    return [i for i in li]

def countLength(chain, start, prev):
    node = chain[start][:]
    if not node:
        return 0
    chain[start] = []
    max_len = 0
    for i in node:
        if i == prev:
            continue
        cloned = deepcopy(chain)
        length = countLength(cloned, i, start) + 1
        if length > max_len:
            max_len = length
    return max_len

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    chain = [[] for i in xrange(n)]
    for i in xrange(n):
        string = map(int , fp.readline()[:-1].split(" "))
        chain[string[0]-1].append(string[1]-1)
        chain[string[1]-1].append(string[0]-1)

    max_len = 0
    for i in xrange(n):
        cloned = deepcopy(chain)
        length = countLength(cloned, i, -1)
        if length > max_len:
            max_len = length
    print max_len

Submission status: Runtime Error

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

def deepcopy(li):
    return [i for i in li]

def countLength(chain, start, prev):
    node = chain[start][:]
    if not node:
        return 0
    chain[start] = []
    max_len = 0
    for i in node:
        if i == prev:
            continue
        cloned = deepcopy(chain)
        length = countLength(cloned, i, start) + 1
        if length > max_len:
            max_len = length
    return max_len

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    chain = [[] for i in xrange(n)]
    for i in xrange(n):
        string = map(int , fp.readline()[:-1].split(" "))
        chain[string[0]-1].append(string[1]-1)
        chain[string[1]-1].append(string[0]-1)

    max_len = 0
    for i in xrange(n):
        cloned = deepcopy(chain)
        #length = countLength(cloned, i, -1)
        #if length > max_len:
        #    max_len = length
    print max_len

Submission status: Runtime Error

Solution code:
import sys

#fp = open("input.txt", "r")
fp = sys.stdin

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    chain = [[] for i in xrange(n)]
    for i in xrange(n):
        string = map(int , fp.readline()[:-1].split(" "))
        chain[string[0]-1].append(string[1]-1)
        chain[string[1]-1].append(string[0]-1)

Submission status: Runtime Error

Solution code:
import sys

fp = sys.stdin

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    chain = [[] for i in xrange(n)]
    for i in xrange(n):
        string = map(int , fp.readline()[:-1].split(" "))
        chain[string[0]-1].append(string[1]-1)
        chain[string[1]-1].append(string[0]-1)
    print 'hoge'

Submission status: Runtime Error

Solution code:
import sys

fp = sys.stdin

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    cha = [[] for i in xrange(n)]
    for i in xrange(n):
        st = map(int , fp.readline()[:-1].split(" "))
        cha[st[0]-1].append(st[1]-1)
        cha[st[1]-1].append(st[0]-1)
    print 'hoge'

Submission status: Runtime Error

Solution code:
import sys

fp = sys.stdin

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break

    hoge = [[] for i in xrange(n)]
    for i in xrange(n):
        fuga = map(int , fp.readline()[:-1].split(" "))
        hoge[fuga[0]-1].append(fuga[1]-1)
        hoge[fuga[1]-1].append(fuga[0]-1)
    print 'hoge'

Submission status: Runtime Error

Solution code:
import sys

fp = sys.stdin

while True:
    n = int(fp.readline()[:-1])
    if n == 0:
        break
    chain = []
    for i in xrange(n):
        chain.append([])
    for i in xrange(n):
        string = map(int , fp.readline()[:-1].split(" "))
        chain[string[0]-1].append(string[1]-1)
        chain[string[1]-1].append(string[0]-1)
    print 'hoge'