Study/코딩 테스트

2021.6.30 [백준] 결혼식

NOredstone 2021. 6. 30. 14:26

구현

 

 

Q

 

https://www.acmicpc.net/problem/5567

 

5567번: 결혼식

예제 1의 경우 2와 3은 상근이의 친구이다. 또, 3과 4는 친구이기 때문에, 4는 상근이의 친구의 친구이다. 5와 6은 친구도 아니고, 친구의 친구도 아니다. 따라서 2, 3, 4 3명의 친구를 결혼식에 초대

www.acmicpc.net

 

A

 

구현이므로 문제에 잘 읽고 해당하는 코드만 짜면 되었다. 리스트를 여러 개로 나누는 것이 편하게 풀 수 있는 방법이었다.

 

나의 코드

 

n = int(input())
m = int(input())
friend = []
near = []
array = [0]*m
for i in range(m):
    a, b = map(int, input().split())
    if a >= b:
        array[i] = [a, b]
    else:
        array[i] = [b, a]
array.sort()
for i in range(m):
    a, b = array[i]
    if a == 1 and b not in friend:
        friend.append(b)
    elif b == 1 and a not in friend:
        friend.append(a)
    elif a in friend and b not in friend and b not in near and b != 1:
        near.append(b)
    elif b in friend and a not in friend and a not in near and a != 1:
        near.append(a)
print(len(friend)+len(near))