N, M, V = map(int, input().split())

matrix = [[0] * (N + 1) for _ in range(N+1)]
for _ in range(M):
    link = list(map(int, input().split()))
    matrix[link[0]][link[1]] = 1
    matrix[link[1]][link[0]] = 1
    
def dfs(current_node, row, foot_prints):
    foot_prints += [current_node]
    for search_node in range(len(row[current_node])):
        if row[current_node][search_node] and search_node not in foot_prints:
            foot_prints = dfs(search_node, row, foot_prints)
            
    return foot_prints

def bfs(start):
    queue = [start]
    foot_prints = [start]
    while queue:
        current_node = queue.pop(0)
        for search_node in range(len(matrix[current_node])):
            if matrix[current_node][search_node] and search_node not in foot_prints:
                foot_prints += [search_node]
                queue += [search_node]
                
    return foot_prints

print(*dfs(V, matrix, []))
print(*bfs(V))

이번 코드는 

https://this-programmer.com/entry/백준1260파이썬-DFS와-BFS

 

[백준/1260/파이썬3(python3)] DFS와 BFS

[백준/1260/파이썬] DFS와 BFS 문제 그래프를 DFS로 탐색한 결과와 BFS로 탐색한 결과를 출력하는 프로그램을 작성하시오. 단, 방문할 수 있는 정점이 여러 개인 경우에는 정점 번호가 작은 것을 먼저 방문하고,..

this-programmer.com

블로그를 참고하였습니다. 코드가 깔끔해서 보는사람 입장에선 이 코드를 포스팅하는게 낫다고 생각...

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 11866번, 1158번(python)  (0) 2019.06.20
백준 1966번(python)  (0) 2019.06.20
백준 10845번(python)  (0) 2019.06.17
백준 2504번(python)  (0) 2019.06.03
백준 9012번(python)  (0) 2019.06.02


 

class Queue:
    def __init__(self):
        self.list = []
        
    def push(self, x):
        self.list.append(x)
        
    def pop(self):
        if(self.empty()):
            return -1
        else:
            output = self.list[0]
            self.list = self.list[1:]

            return output

    def empty(self):
        return 1 if len(self.list) == 0 else 0
    
    def size(self):
        return len(self.list)
    
    def front(self):
        if(self.empty()):
            return -1
        else:
            return self.list[0]
        
    def back(self):
        if(self.empty()):
            return -1
        else:
            return self.list[-1]
        
Case = int(input())
queue = Queue()

while(Case > 0):
    Case -= 1
    input_split = input().split()
    
    order = input_split[0]
    
    if(order == 'push'):
        queue.push(input_split[1])
    elif(order == 'pop'):
        print(queue.pop())
    elif(order == 'size'):
        print(queue.size())
    elif(order == 'empty'):
        print(queue.empty())
    elif(order == 'front'):
        print(queue.front())
    elif(order == 'back'):
        print(queue.back())
    else:
        print('xx')

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 1966번(python)  (0) 2019.06.20
백준 1260번(python)  (0) 2019.06.18
백준 2504번(python)  (0) 2019.06.03
백준 9012번(python)  (0) 2019.06.02
백준 1874번(python)  (0) 2019.06.02


string = list(input())

class Stack:
    def __init__(self):
        self.len = 0
        self.list = []
        
    def push(self, x):
        self.list.append(x)
        self.len += 1
        
    def pop(self):
        if(self.size() == 0):
            return -1
        res = self.list[self.len - 1]
        del self.list[self.len - 1]
        self.len -= 1
        return res
    
    def size(self):
        return self.len
    
    def empty(self):
        return 1 if self.len == 0 else 0
    
    def top(self):
        return self.list[-1] if self.size() != 0 else -1
    
class customEx(BaseException): pass

stack = Stack()

if(string[0] == ')' or string[0] == ']'):
    print('0')
else:
    try:
        for str in string:
            if(str == '(' or str == '['):
                stack.push(str)
            elif(str == ')'):
                if(stack.top() == '('):
                    stack.pop()
                    stack.push(2)
                else:
                    res = 0
                    while(stack.top() != '('):
                        if(stack.top() != -1):
                            if(not isinstance(stack.top(), int)):
                                raise customEx
                            res += stack.top()
                            stack.pop()
                        else:
                            raise customEx
                    stack.pop()
                    res *= 2
                    stack.push(res)
            elif(str == ']'):
                if(stack.top() == '['):
                    stack.pop()
                    stack.push(3)
                else:
                    res = 0
                    while(stack.top() != '['):
                        if(stack.top() != -1):
                            if(not isinstance(stack.top(), int)):
                                raise customEx
                            res += stack.top()
                            stack.pop()
                        else:
                            raise customEx
                    stack.pop()
                    res *= 3
                    stack.push(res)          

        res = 0
        while(True):
            if(not stack.empty()):
                if(stack.top() != -1):
                    if(not isinstance(stack.top(), int)):
                        raise customEx
                    res += stack.top()
                    stack.pop()
            else:
                print(res)
                break
    except customEx:
        print(0)

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 1260번(python)  (0) 2019.06.18
백준 10845번(python)  (0) 2019.06.17
백준 9012번(python)  (0) 2019.06.02
백준 1874번(python)  (0) 2019.06.02
백준 10828번(python)  (0) 2019.06.02


Case = int(input())
for _ in range(Case):
    string = list(input())
    close_cnt = 0
        
    while(len(string) != 0):
        if(close_cnt < 0):
            break
        res = string.pop()

        if(res == '('):
            close_cnt -= 1
        elif(res == ')'):
            close_cnt += 1
            
    if(close_cnt == 0):
        print('YES')
    else:
        print('NO')

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 10845번(python)  (0) 2019.06.17
백준 2504번(python)  (0) 2019.06.03
백준 1874번(python)  (0) 2019.06.02
백준 10828번(python)  (0) 2019.06.02
백준 9020번(python)  (0) 2019.05.31


class Stack:
    def __init__(self):
        self.len = 0
        self.list = []
        
    def push(self, x):
        self.list.append(x)
        self.len += 1
        
    def pop(self):
        if(self.size() == 0):
            return -1
        self.len -= 1
        return self.list.pop()
    
    def size(self):
        return self.len
        
    def top(self):
        return self.list[-1] if self.size() else -1
    
    def empty(self):
        return 1 if len(self.list) == 0 else 0
        
Case = int(input())
num_list = [int(input()) for _ in range(Case)]
output_list = []

num_pointer = 0
stack = Stack()

for i in range(Case):
    stack.push(i + 1)
    output_list.append('+')
    
    while(num_pointer < Case and stack.top() == num_list[num_pointer]):
        stack.pop()
        output_list.append('-')
        num_pointer += 1
        
if not stack.empty():
    print("NO")
else:
    for i in output_list:
        print(i)

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

백준 2504번(python)  (0) 2019.06.03
백준 9012번(python)  (0) 2019.06.02
백준 10828번(python)  (0) 2019.06.02
백준 9020번(python)  (0) 2019.05.31
백준 4948번(python)  (0) 2019.05.31