Problem:


パーティー

問題

あなたはクリスマスパーティーに学校内の自分の友達と，自分の友達の友達を招待することにした．
あなたの通う学校の生徒数は n 人であり，それぞれの生徒には 1 から n までの番号が割り振られている．
あなたの番号は 1 である．
あなたの手元には，誰と誰が友達であるかを記したリストがある．
このリストをもとに，
あなたがクリスマスパーティーに招待する生徒数を求めるプログラムを作成せよ．

入力

 

入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．

データセットの1 行目には学校の生徒数 n （2 ≤ n ≤ 500）が，
2 行目にはリストの長さ m （1 ≤ m ≤ 10000）が書かれている．
入力は全部で 2+m 行からなる．2+i 行目（1 ≤ i ≤ m）には 2 つの整数 ai と bi （1 ≤ ai < bi ≤ n）が空白区切りで書かれており，番号 ai と番号 bi の生徒が友達同士であることを表す．
3 行目から 2+m 行目には同じ友達関係を表す行が重複して現れることはない．

n, m がともに 0 のとき入力の終了を示す.データセットの数は 5 を超えない．

出力

データセットごとに，あなたがクリスマスパーティーに招待する生徒数を1 行に出力する．

入出力例

入力例

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

出力例

3
0

1つ目の入力例 において，あなたの友達は番号 2 と番号 3 の生徒の 2 人である．
また，番号 3 と番号 4 の生徒は友達同士であるので，
番号 4 の生徒はあなたの友達の友達である．
番号 5 と番号 6 の生徒はあなたの友達でもなく，あなたの友達の友達でもない．
したがって，あなたは番号 2,3,4 の 3 人の生徒をクリスマスパーティーに招待する．

2つ目の入力例  において，あなたには友達はいない．
したがって，あなたがクリスマスパーティーに招待する生徒数は 0 人である．

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



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


Sample output:
3
0

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    m = int(input())
    if n==0: break
    tab = [[] for _ in range(n)]
    for _ in range(m):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        tab[a].append(b)
        tab[b].append(a)
    ans = set(tab[0])
    foff = set()
    for i in ans:
        foff = foff | set(tab[i])
    print(len((ans|foff)-{1}))

Submission status: Wrong Answer

Solution code:
while True:
    n, m = int(input()), int(input())
    if m == 0:
        break
    d = {k+1:[] for k,_ in enumerate(range(m))}
    for _ in range(m):
        a, b = map(int, input().split())
        d[a].append(b)
    ans = d[1].copy()
    for k in d[1]:
        ans += d[k]
    print(len(set(ans)))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    m = int(input())
    if m == 0:
        break
    dataset = [input().split() for _ in range(m)]
    list_ = {i+1: [] for i in range(n)}
    for data in dataset:
        idx, friend_idx = int(data[0]), int(data[1])
        list_[idx].append(friend_idx)

    ans = list_[1].copy()
    for i in list_[1]:
        ans += list_[i]
    print(len(set(ans)))

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    m = int(input())
    if m == 0:
        break
    dataset = [input().split() for _ in range(m)]
    list_ = {i+1: [] for i in range(n)}
    for data in dataset:
        idx, friend_idx = int(data[0]), int(data[1])
        list_[idx].append(friend_idx)

    ans = list_[1].copy()
    for i in list_[1]:
        ans += list_[i]
    print(len(set(ans))-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0545

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        n = int(input())
        m = int(input())
        if n == 0 and m == 0:
            break

        G = [[] for _ in range(n+1)]
        for _ in range(m):
            a, b = map(int, input().split())
            G[a].append(b)

        invitees = set()
        for f in G[1]:
            invitees.add(f)     #  ??´??\?????????
            for ff in G[f]:
                invitees.add(ff) #  ???????????????

        #print(invitees)
        print(len(invitees))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
while True:
    n=int(input())
    if n==0:break
    m=int(input())
    rel=sorted([[int(s)for s in input().split(" ")]for i in range(m)],key=lambda x:x[0])
    isf=[False for i in range(n)]
    inv=[False for i in range(n)]
    isf[0]=True
    for r in rel:
        if r[0]==1:
            isf[r[1]-1]=True
            inv[r[1]-1]=True
        elif isf[r[0]-1]==True:inv[r[1]-1]=True
    print(inv.count(True))

Submission status: Wrong Answer

Solution code:
while True:
    n=int(input())
    if n==0:break
    m=int(input())
    rel=sorted([[int(s)for s in input().split(" ")]for i in range(m)],key=lambda x:x[0])
    isf=[False for i in range(n)]
    inv=[False for i in range(n)]
    isf[0]=True
    for r in rel:
        if r[0]==1:
            isf[r[1]-1]=True
            inv[r[1]-1]=True
    for r in rel:
        if isf[r[0]-1]==True:inv[r[1]-1]=True
    print(inv.count(True))

Submission status: Wrong Answer

Solution code:
while True:
  n = int(input())
  if not n:
    break
  m = int(input())
  edge = [[0 for i in range(n + 1)] for j in range(n + 1)]
  for i in range(m):
    a,b = map(int,input().split())
    edge[a][b] = 1
    edge[b][a] = 1
  friends1 = [i for i in range(n + 1) if edge[1][i]]
  friends2 = []
  for friend in friends1:
    friend_lst = [i for i in range(n) if edge[friend][i]]
    friends2.extend(friend_lst)
  ans = set(friends1 + friends2)
  if 1 in ans:
    ans = ans - {1}

  print(len(ans))

Submission status: Wrong Answer

Solution code:
while 1:
    n = int(raw_input()); m = int(raw_input());
    if n==0 and m==0: break
    e = [[0 for i in range(501)] for j in range(501)]
    u = [0]*501; ans = 0;
    for i in range(m):
        f,t = map(int, raw_input().split())
        e[f][t] = e[t][f] = 1
    for i in range(2,n):
        if e[1][i]==1:
            if u[i]==0: ans+=1
            for j in range(i+1,n):
                if e[i][j]==1 and u[j]==0:
                    ans+=1; u[j] = 1;
    print str(ans)

Submission status: Wrong Answer

Solution code:
while 1:
    n = int(raw_input()); m = int(raw_input());
    if n==0 and m==0: break
    e = [[0 for i in range(501)] for j in range(501)]
    u = [0]*501; ans = 0;
    for i in range(m):
        f,t = map(int, raw_input().split())
        e[f][t] = e[t][f] = 1
    for i in range(2,n):
        if e[1][i]==1:
            if u[i]==0: ans+=1
            u[i] = 1
            for j in range(2,n):
                if e[i][j]==1 and u[j]==0:
                    ans+=1; u[j] = 1;
    print str(ans)

Submission status: Wrong Answer

Solution code:
l = []
t = []
ans = [False] * 501
while True:
  l = []
  t = []
  ans = [False] * 501
  N = input()
  M = input()
  if N == 0 and M == 0:
    break
  for i in range(M):
    a, b = map(int, raw_input().split())
    l.append([a, b])
    if a == 1:
      t.append(b)
    if b == 1:
      t.append(a)
  for i in t:
    ans[i] = True
    for j in range(M):
      if l[j][0] == i:
        ans[l[j][1]] = True
      if l[j][1] == i:
        ans[l[j][0]] = True
  res = 0
  for i in range(len(ans)):
    if ans[i] and i != 1:
      print "tes: %d" % (i) 
      res += 1

  print res

Submission status: Wrong Answer

Solution code:
while True:
	a,b = [input() for i in xrange(2)]
	if a == 0:
		exit()
	l = [[] for i in xrange(a+1)]
	for i in xrange(b):
		a,b = map(int,raw_input().split())
		l[a].append(b)
	f = []
	f.extend(l[1])
	for i in l[1]:
		f.extend(l[i])
	print(len(set(f)))

Submission status: Wrong Answer

Solution code:
import sys
import itertools
readline = sys.stdin.readline

while True:
    N = int(readline())
    M = int(readline())
    if N == 0 and M == 0:
        break
    pairs = set()
    for _ in xrange(M):
        t = tuple(int(x) for x in readline().split())
        pairs.add(t)
    friends = set()
    for i in xrange(2, N + 1):
        if (1, i) in pairs:
            friends.add(i)
        else:
            for j in xrange(2, i):
                if (1, j) in pairs and (j, i) in pairs:
                    friends.add(i)
    print len(friends)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
readline = sys.stdin.readline

while True:
    N = int(readline())
    M = int(readline())
    if N == 0 and M == 0:
        break
    pairs = set()
    for _ in xrange(M):
        t = tuple(int(x) for x in readline().split())
        pairs.add(t)
    friends = set()
    for i in xrange(2, N + 1):
        if (1, i) in pairs:
            friends.add(i)
        else:
            for j in xrange(2, N + 1):
                if (1, j) in pairs and (j, i) in pairs:
                    friends.add(i)
    print len(friends)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
readline = sys.stdin.readline

while True:
    N = int(readline())
    M = int(readline())
    if N == 0 and M == 0:
        break
    pairs = set()
    for _ in xrange(M):
        t = tuple(int(x) for x in readline().split())
        pairs.add(t)
    cnt = 0
    for i in xrange(2, N + 1):
        if (1, i) in pairs:
            cnt += 1
        else:
            for j in xrange(2, N + 1):
                if (1, j) in pairs and (j, i) in pairs:
                    cnt += 1
    print cnt

Submission status: Wrong Answer

Solution code:
import sys
import itertools
readline = sys.stdin.readline

while True:
    N = int(readline())
    M = int(readline())
    if N == 0 and M == 0:
        break
    pairs = set()
    for _ in xrange(M):
        a, b = tuple(int(x) for x in readline().split())
        pairs.add((a, b))
        pairs.add((b, a))
    cnt = 0
    for i in xrange(2, N + 1):
        if (1, i) in pairs:
            cnt += 1
        else:
            for j in xrange(2, N + 1):
                if (1, j) in pairs and (j, i) in pairs:
                    cnt += 1
    print cnt

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n == 0: break
    f = [[] for i in range(n)]
    for i in range(input()):
        a, b = map(int,  raw_input().split())
        a -= 1; b -= 1
        f[a].append(b)
        f[b].append(a)
    ans = set(f[0])
    for i in f[0]:
        ans |= set(f[i])
    if 0 in ans: ans.remove(0)
    print len(ans)

Submission status: Accepted

Solution code:
from collections import deque
def bfs(s):
    visited[s]=1
    q=deque([s])
    while len(q)!=0:
        p=q.popleft()
        for i in fr[p]:
            if visited[i]==0:
                visited[i]=1
                q.append(i)
                d[i]=d[p]+1

while 1:
    n=input()
    m=int(raw_input())
    if n==m==0:break
    fr=[[]*n for _ in xrange(n)]
    d=[0]*n
    visited=[0]*n
    for i in  xrange(m):
        a,b=map(int,raw_input().split())
        a-=1
        b-=1
        fr[a].append(b)
        fr[b].append(a)
    bfs(0)
    cnt=0
    for i in xrange(n):
        if 1<=d[i]<=2:
            cnt+=1
    print(cnt)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    m = int(input())
    if n==0: break
    tab = [[] for _ in range(n)]
    for _ in range(m):
        a,b = map(int,input().split())
        a -= 1
        b -= 1
        tab[a].append(b)
        tab[b].append(a)
    ans = set(tab[0])
    foff = set()
    for i in ans:
        foff = foff | set(tab[i])
    print(len((ans|foff)-{0}))

Submission status: Accepted

Solution code:
while True:
    n, m = int(input()), int(input())
    if m == 0:
        break
    d = {k+1:[] for k,_ in enumerate(range(m))}
    for _ in range(m):
        a, b = map(int, input().split())
        d[a].append(b)
        d[b].append(a)
    ans = d[1].copy()
    for k in d[1]:
        ans += d[k]
    print(len(set(ans))-1)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    m = int(input())
    if m == 0:
        break
    dataset = [input().split() for _ in range(m)]
    list_ = {i+1: [] for i in range(n)}
    for data in dataset:
        idx_x, idx_y = map(int, data)
        list_[idx_x].append(idx_y)
        list_[idx_y].append(idx_x)

    ans = list_[1].copy()
    for i in list_[1]:
        ans += list_[i]
    print(len(set(ans))-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0545

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        n = int(input())
        m = int(input())
        if n == 0 and m == 0:
            break

        G = [[] for _ in range(n+1)]
        for _ in range(m):
            a, b = map(int, input().split())
            G[a].append(b)
            G[b].append(a)

        invitees = set()
        for f in G[1]:
            invitees.add(f)     #  ??´??\?????????
            for ff in G[f]:
                invitees.add(ff) #  ???????????????

        invitees.remove(1)      #  ????????????????????????????????????
        # print(invitees)
        print(len(invitees))


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
while True:
    n=int(input())
    if n==0:break
    m=int(input())
    rel=sorted([[int(s)for s in input().split(" ")]for i in range(m)],key=lambda x:x[0])
    isf=[False for i in range(n)]
    inv=[False for i in range(n)]
    isf[0]=True
    for r in rel:
        if r[0]==1:isf[r[1]-1]=True
    for r in rel:
        if isf[r[0]-1]==True:inv[r[1]-1]=True
        elif isf[r[1]-1]==True:inv[r[0]-1]=True
    print(inv.count(True))

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if not n:
    break
  m = int(input())
  edge = [[0 for i in range(n + 1)] for j in range(n + 1)]
  for i in range(m):
    a,b = map(int,input().split())
    edge[a][b] = 1
    edge[b][a] = 1
  friends1 = {i for i in range(n + 1) if edge[1][i]}
  friends2 = set()
  for friend in friends1:
    friends_lst = {i for i in range(n + 1) if edge[friend][i]}
    friends2 = friends2 | friends_lst
#  print(friends1 | friends2)
  ans = len(friends1 | friends2)
  if ans:
    ans -= 1
  print(ans)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if not n:
    break
  m = int(input())
  edge = [[0 for i in range(n + 1)] for j in range(n + 1)]
  for i in range(m):
    a,b = map(int,input().split())
    edge[a][b] = 1
    edge[b][a] = 1
  friends1 = [i for i in range(n + 1) if edge[1][i]]
  friends2 = []
  for friend in friends1:
    friends_lst = [i for i in range(n + 1) if edge[friend][i]]
    friends2.extend(friends_lst)
  ans = len(set(friends1 + friends2))
  if ans:
    ans -= 1
  print(ans)

Submission status: Accepted

Solution code:
def solve():
  while True:
    n = int(input())
    if not n:
      break
    m = int(input())
    edge = [[0 for i in range(n + 1)] for j in range(n + 1)]
    for i in range(m):
      a,b = map(int,input().split())
      edge[a][b] = 1
      edge[b][a] = 1
    friends1 = [i for i in range(n + 1) if edge[1][i]]
    friends2 = []
    for friend in friends1:
      friends_lst = [i for i in range(n + 1) if edge[friend][i]]
      friends2.extend(friends_lst)
    ans = len(set(friends1 + friends2))
    if ans:
      ans -= 1
    print(ans)
solve()

Submission status: Accepted

Solution code:
for e in iter(input,'0'):
 R=[set()for _ in[0]*-~int(e)]
 for _ in[0]*int(input()):
  a,b=map(int,input().split())
  R[a]|={b}
  if a-1:R[b]|={a}
 for m in set(R[1]):
  R[1]|=R[m]
 print(len(R[1]))

Submission status: Accepted

Solution code:
for e in iter(input,'0'):
 R=[[]for _ in[0]*-~int(e)]
 for _ in[0]*int(input()):
  a,b=map(int,input().split())
  R[a]+=[b];R[b]+=[a]
 for m in R[1][:]:R[1]+=R[m]
 print(len({*R[1]}-{1}))

Submission status: Accepted

Solution code:
import sys
r=sys.stdin.readline
for e in iter(r,'0\n'):
 R=[[]for _ in[0]*-~int(e)]
 for _ in[0]*int(r()):
  a,b=map(int,r().split())
  R[a]+=[b];R[b]+=[a]
 for m in R[1][:]:R[1]+=R[m]
 print(len({*R[1]}-{1}))

Submission status: Accepted

Solution code:
def s():
 import sys
 r=sys.stdin.readline
 for e in iter(r,'0\n'):
  R=[[]for _ in[0]*-~int(e)]
  for _ in[0]*int(r()):
   a,b=map(int,r().split())
   R[a]+=[b];R[b]+=[a]
  for m in R[1][:]:R[1]+=R[m]
  print(len({*R[1]}-{1}))
if'__main__'==__name__:s()

Submission status: Accepted

Solution code:
def s():
 import sys
 r=sys.stdin.readline
 for e in iter(r,'0\n'):
  R=[[]for _ in[0]*-~int(e)]
  for _ in[0]*int(r()):
   a,b=map(int,r().split())
   R[a]+=[b];R[b]+=[a]
  for m in R[1][:]:R[1]+=R[m]
  print(len({*R[1]}-{1}))
s()

Submission status: Accepted

Solution code:
# AOJ 0545: Party
# Python3 2018.7.1 bal4u

import sys
from sys import stdin
input = stdin.readline

while True:
	n, m = int(input()), int(input())
	if n == 0: break
	n += 1
	f, tbl = [0]*n, []
	for i in range(m):
		a, b = map(int, input().split())
		if a == 1 or b == 1: f[a] = f[b] = 1
		else: tbl.append((a, b))
	for a, b in tbl:
		if f[a] == 1 and f[b] == 1: continue
		if f[a] == 1: f[b] = 2
		elif f[b] == 1: f[a] = 2
	ans = 0
	for i in range(2, n):
		if f[i]: ans += 1
	print(ans)

Submission status: Accepted

Solution code:
while True:
    n=raw_input()
    if n in "0":break
    try:
        m=input()
    except SyntaxError:
        break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Accepted

Solution code:
while 1:
    n = int(raw_input()); m = int(raw_input());
    if n==0 and m==0: break
    e = [[0 for i in range(501)] for j in range(501)]
    u = [0]*501; ans = 0;
    for i in range(m):
        f,t = map(int, raw_input().split())
        e[f][t] = e[t][f] = 1
    for i in range(2,n+1):
        if e[1][i]==1:
            if u[i]==0: ans+=1
            u[i] = 1
            for j in range(2,n+1):
                if e[i][j]==1 and u[j]==0:
                    ans+=1; u[j] = 1;
    print str(ans)

Submission status: Accepted

Solution code:
l = []
t = []
ans = [False] * 501
while True:
  l = []
  t = []
  ans = [False] * 501
  N = input()
  M = input()
  if N == 0 and M == 0:
    break
  for i in range(M):
    a, b = map(int, raw_input().split())
    l.append([a, b])
    if a == 1:
      t.append(b)
    if b == 1:
      t.append(a)
  for i in t:
    ans[i] = True
    for j in range(M):
      if l[j][0] == i:
        ans[l[j][1]] = True
      if l[j][1] == i:
        ans[l[j][0]] = True
  res = 0
  for i in range(len(ans)):
    if ans[i] and i != 1:
      res += 1

  print res

Submission status: Accepted

Solution code:
while 1:
    n = int(raw_input())
    m = int(raw_input())
    dic = {}

    if not n and not m:
       break
    
    for i in range(n):
        dic[i+1] = []
    
    for i in range(m):
        a, b = [int(j) for j in raw_input().split(" ")]
        dic[a].append(b)
        dic[b].append(a)
    
    invite = list(dic[1])
    for i in dic[1]:
        invite += dic[i]
    
    d = set(invite)
    print len(d) - (1 in d)

Submission status: Accepted

Solution code:
import sys
import itertools
readline = sys.stdin.readline

while True:
    N = int(readline())
    M = int(readline())
    if N == 0 and M == 0:
        break
    pairs = set()
    for _ in xrange(M):
        a, b = tuple(int(x) for x in readline().split())
        pairs.add((a, b))
        pairs.add((b, a))
    cnt = 0
    for i in xrange(2, N + 1):
        if (1, i) in pairs:
            cnt += 1
        else:
            for j in xrange(2, N + 1):
                if (1, j) in pairs and (j, i) in pairs:
                    cnt += 1
                    break
    print cnt

Submission status: Accepted

Solution code:
while True:
	n = int(input())
	m = int(input())
	if n==0 and m==0: break
	A = [list(map(int, input().split())) for _ in range(m)]
	A.sort()
	B, C = set(), set()  #友達リスト
	C = set()  #友達の友達リスト
	for i in range(len(A)):
		if A[i][0]==1: B.add(A[i][1])
	for i in range(len(A)):
		if A[i][0]==1: continue
		if A[i][0] in B: C.add(A[i][1])
		if A[i][1] in B: C.add(A[i][0])
#	print((B|C))
	print(len(B|C))

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    m = int(input())
    if n == m == 0:
        break
    a = []
    for i in range(m):
        x, y = map(int,input().split())
        a.append([x, y])
    a.sort(key = lambda x: x[0])
    f1 = []
    f2 = []
    for i in range(m):
        if a[i][0] == 1:
            f1.append(a[i][1])
        elif a[i][0] in f1 and not (a[i][1] in f1) and not (a[i][1] in f2):
            f2.append(a[i][1])
        elif a[i][1] in f1 and not (a[i][0] in f1) and not (a[i][0] in f2):
            f2.append(a[i][0])
    print(len(f1) + len(f2))

Submission status: Accepted

Solution code:
def main():
    while True:
        n = int(input())
        m = int(input())
        if n == 0:
            break

        lof = [list(map(int, input().split())) for _ in range(m)]
        lof.sort(key=lambda x: x[0])

        friend = [False] * (n + 1)
        friend_of_friend = [False] * 501 * (n + 1)
        no = 0
        while True:
            a, b = lof[no]
            if a == 1:
                friend[b] = True
                no += 1
            else:
                break

        for i in range(no, m):
            a, b = lof[i]
            if friend[a] and not friend[b]:
                if friend_of_friend[b] == False:
                    friend_of_friend[b] = True
                    no += 1
            elif friend[b] and not friend[a]:
                if friend_of_friend[a] == False:
                    friend_of_friend[a] = True
                    no += 1

        print(no)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while True:
    n=int(input())
    m=int(input())
    if n==0:
        break
    a=[None for _ in range(m)]
    b=[None for _ in range(m)]
    friend = set()
    friend1=set()
    friend2=set()
    for i in range(m):
        a[i],b[i] = list(map(int, input().split()))
        if a[i] ==1:
            friend.add(b[i])
    for i in range(m):
        if a[i] in friend:
            friend1.add(b[i])
        if b[i] in friend:
            friend2.add(a[i])
    ans=friend|friend1|friend2
    ans.discard(1)
    print(len(ans))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if not n:
        break
    m = int(input())
    li = [[] for _ in range(n + 1)]
    for _ in range(m):
        a, b = [int(i) for i in input().split()]
        li[a].append(b)
        li[b].append(a)
    ans = set()
    for i in li[1]:
        ans.add(i)
        for j in li[i]:
            ans.add(j)
    if 1 in ans:
        ans.remove(1)
    print(len(ans))

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    m=int(input())
    friend=[[0 for i in range(1)] for j in range(n+1)]
    if n==m==0:break
    for i in range(m):
        a,b=map(int,input().split())
        friend[a].append(b)
        friend[b].append(a)
    friend_1st=friend[1]
    friend_2nd=friend_1st[:]
    for i in friend_1st:
        friend_2nd.extend(friend[i])
    ans=list(set(friend_2nd))
    if 0 in ans:ans.remove(0)
    if 1 in ans:ans.remove(1)
    print(len(ans))

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    m = int(input())
    if n == 0:
        break

    data = []
    friend = []
    for _ in range(m):
        a, b = map(int, input().split())
        if a == 1:
            friend.append(b)
        else:
            data.append([a, b])

    friendfriend = []
    for d in data:
        if d[0] in friend:
            friendfriend.append(d[1])
        elif d[1] in friend:
            friendfriend.append(d[0])

    friend.extend(friendfriend)
    friend = list(set(friend))
    print(len(friend))

Submission status: Runtime Error

Solution code:
while True:
    n=int(input())
    if n==0:break
    m=int(input())
    rel=sorted([[int(s)for s in input().split(" ")]for i in range(m)],key=lambda x:x[0])
    isf=[False for i in range(n)]
    inv=[False for i in range(n)]
    isf[0]=True
    for r in rel:
        if r[0]==1:
            isf[r[1]]=True
            inv[r[1]]=True
        elif isf[r[0]]==True:inv[r[1]]=True
    print(inv.count(True))

Submission status: Runtime Error

Solution code:
import sys
r=sys.stdin.readline
for e in iter(r,'0'):
 R=[[]for _ in[0]*-~int(e)]
 for _ in[0]*int(r()):
  a,b=map(int,r().split())
  R[a]+=[b];R[b]+=[a]
 for m in R[1][:]:R[1]+=R[m]
 print(len({*R[1]}-{1}))

Submission status: Runtime Error

Solution code:
while True:
  fri = int(input())
  count  = int(input())
  if fri == 0 and count ==0: break
  a,b,c,d = [],[],[],[]
  for i in range(count):
    t = map(int,raw_input().split())
    if t[0] == 1:
      if not(t[1] in b):
        b.append(t[1])
        continue
    if t[0] in b:
      if not(t[1] in b):
        c.append(t[1])
        continue
    else: continue
  g = int(len(b))+int(len(c))
  print g

Submission status: Runtime Error

Solution code:
while True:
    n,m=input(),input()
    if n==m==0:break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    n=input()
    m=input()
    if n==m==0:break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    try:
        n=input()
        m=input()
    except SyntaxError:
        pass
    if n==m==0:break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    n,m=map(int,raw_input().split())
    if n==m==0:break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    n=int(raw_input())
    m=int(raw_input())
    if n==m==0:break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    n=input()
    m=input()
    if n==0:break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    n=raw_input()
    m=input()
    if n in "0":break
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
while True:
    n=raw_input()
    if n in "0":break
    m=input()
    S,L=set(),[]
    for i in range(m):
        a,b=map(int,raw_input().split())
        if a==1:
            S.add(b)
        elif b==1:
            S.add(a)
        else:
            L.append((a,b))
    for i in S:
        for s in L:
            if i in s:
                S=S.union(s)
    print len(S)

Submission status: Runtime Error

Solution code:
l = []
t = []
ans = [False] * 500
while True:
  l = []
  t = []
  ans = [False] * 500
  N = input()
  M = input()
  if N == 0 and M == 0:
    break
  for i in range(M):
    a, b = map(int, raw_input().split())
    l.append([a, b])
    if a == 1:
      t.append(b)
    if b == 1:
      t.append(a)
  for i in t:
    ans[i] = True
    for j in range(M):
      if l[j][0] == i:
        ans[l[j][1]] = True
      if l[j][1] == i:
        ans[l[j][0]] = True
  res = 0
  for i in range(len(ans)):
    if ans[i] and i != 1:
      print "tes: %d" % (i) 
      res += 1

  print res