From b0cb552fd67eab0c28dd3c97d3340ca61e929433 Mon Sep 17 00:00:00 2001 From: jooon <98799034+SeongJoonhee@users.noreply.github.com> Date: Wed, 17 May 2023 00:08:05 +0900 Subject: [PATCH 1/4] =?UTF-8?q?5567=5F=EA=B2=B0=ED=98=BC=EC=8B=9D.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7_\352\262\260\355\230\274\354\213\235.py" | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 "\354\235\264\355\213\260\354\244\200\355\235\254/5567_\352\262\260\355\230\274\354\213\235.py" diff --git "a/\354\235\264\355\213\260\354\244\200\355\235\254/5567_\352\262\260\355\230\274\354\213\235.py" "b/\354\235\264\355\213\260\354\244\200\355\235\254/5567_\352\262\260\355\230\274\354\213\235.py" new file mode 100644 index 0000000..da3d05d --- /dev/null +++ "b/\354\235\264\355\213\260\354\244\200\355\235\254/5567_\352\262\260\355\230\274\354\213\235.py" @@ -0,0 +1,56 @@ +#2023-05-11-Week5-수업 +#5567_결혼식 + +''' +입력 값 +6 +5 +1 2 +1 3 +3 4 +2 3 +4 5 +''' + +#bfs + +import sys +from collections import deque + +''' +이렇게 작성했더니 런타임에러 +input = sys.stdin.readline() +N = int(input()) +M = int(input()) +''' + +N = int(sys.stdin.readline()) +M = int(sys.stdin.readline()) + +graph = [[False] * (N + 1) for i in range(N + 1)] #False 대신 0 가능 #1번 인덱스부터 사용 + +for i in range(M): + a, b = map(int, sys.stdin.readline().split()) + graph[a].append(b) # a 인덱스에 b 넣기 + graph[b].append(a) # b 인덱스에 a 넣기 + + #1번 인덱스 -> 본인 -> 본인의 친구 + +visited = [0] * (N + 1) # false 대신 0 이어야 함 -> 덧셈 연산 수행을 위해 int형 + +def bfs(vertex): + queue = deque([vertex]) + visited[vertex] = 1 + while queue: #queue==1일 때 반복 + vertex = queue.popleft() #deque의 popleft()은 list의 pop(0)와 같지만 O(1)임 + for i in graph[vertex]: + if visited[i] == 0: + queue.append(i) + visited[i] = visited[vertex] + 1 +bfs(1) +result = 0 #초대하는 동기의 수 +for i in range(2,N+1): + if (visited[i] != 0) and (visited[i] < 4) : # 본인/친구/친구의 친구 -> visited 수 범위 1~3 + #if 0 < visited[i] <=3 : 이게 더 간결함 + result += 1 #visited 범위에 해당하면 누적합 계산 +print(result) From 79f8d373201a9a69debb9e3562ebafac710583fe Mon Sep 17 00:00:00 2001 From: jooon <98799034+SeongJoonhee@users.noreply.github.com> Date: Wed, 17 May 2023 01:28:35 +0900 Subject: [PATCH 2/4] =?UTF-8?q?#21=20:=202606=5F=EB=B0=94=EC=9D=B4?= =?UTF-8?q?=EB=9F=AC=EC=8A=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...24\354\235\264\353\237\254\354\212\244.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 "\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" diff --git "a/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" "b/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" new file mode 100644 index 0000000..228fcc9 --- /dev/null +++ "b/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" @@ -0,0 +1,37 @@ +#2023-05-11-Week5-과제 +#2606_바이러스 + +''' +입력 + +7 총 컴퓨터 수 +6 컴퓨터 간 연결된 선 개수 +1 2 1번컴퓨터와 2번컴퓨터 연결 +2 3 +... +4 7 +''' + +#bfs 너비 우선 탐색 +import sys +from collections import deque +vertex = int(sys.stdin.readline()) # 컴퓨터 개수 +edge = int(sys.stdin.readline()) # 연결선 개수 + +graph = [[NULL] for i in range(vertex+1)] #그래프 +visited = [0]*(vertex+1) #1번 인덱스부터 사용 + +for i in range(edge): # 그래프 생성 + a,b = map(int,stdin.split()) + graph[a]+=[b] # a 에 b 연결 + graph[b]+=[a] # b 에 a 연결 + +visited[1] = 1 # 1번 컴퓨터부터 시작이니 방문 표시 +queue = deque([1]) +while queue : + result = queue.popleft() + for i in graph[result]: + if visited[i]==0: + queue.append(i) + visited[i]=1 +print(sum(visited)-1) From 0d64782f7a6dc1b84f3d28a9da5a08510767aef5 Mon Sep 17 00:00:00 2001 From: jooon <98799034+SeongJoonhee@users.noreply.github.com> Date: Thu, 18 May 2023 00:49:31 +0900 Subject: [PATCH 3/4] =?UTF-8?q?#21=20:=202606=5F=EB=B0=94=EC=9D=B4?= =?UTF-8?q?=EB=9F=AC=EC=8A=A4=5FBFS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\235\264\353\237\254\354\212\244_BFS.py" | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) rename "\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" => "\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_BFS.py" (53%) diff --git "a/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" "b/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_BFS.py" similarity index 53% rename from "\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" rename to "\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_BFS.py" index 228fcc9..eec5668 100644 --- "a/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244.py" +++ "b/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_BFS.py" @@ -13,6 +13,7 @@ ''' #bfs 너비 우선 탐색 +''' import sys from collections import deque vertex = int(sys.stdin.readline()) # 컴퓨터 개수 @@ -35,3 +36,39 @@ queue.append(i) visited[i]=1 print(sum(visited)-1) +''' + +import sys +from collections import deque + +vertex = int(sys.stdin.readline()) # 컴퓨터 개수 +edge = int(sys.stdin.readline()) # 연결선 개수 + +graph = [[] for i in range(vertex + 1)] # 그래프 +visited = [0] * (vertex + 1) # 1번 인덱스부터 사용 + +for i in range(edge): # 그래프 생성 + a, b = map(int, sys.stdin.readline().split()) + graph[a].append(b) # a 에 b 연결 + graph[b].append(a) # b 에 a 연결 + + +def bfs(start): + visited[start] = 1 + queue = deque([start]) + + while queue: + node = queue.popleft() + for i in graph[node]: + if visited[i] == 0: + queue.append(i) + visited[i] = 1 + +bfs(1) # 1번 컴퓨터부터 시작 +print(sum(visited) - 1) + + + + + + From ffdac8d78959f9c1e929241a33c6d97208488eee Mon Sep 17 00:00:00 2001 From: jooon <98799034+SeongJoonhee@users.noreply.github.com> Date: Thu, 18 May 2023 00:51:26 +0900 Subject: [PATCH 4/4] =?UTF-8?q?#21=20:=202606=5F=EB=B0=94=EC=9D=B4?= =?UTF-8?q?=EB=9F=AC=EC=8A=A4=5FDFS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\235\264\353\237\254\354\212\244_DFS.py" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_DFS.py" diff --git "a/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_DFS.py" "b/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_DFS.py" new file mode 100644 index 0000000..cce231e --- /dev/null +++ "b/\354\235\264\355\213\260\354\244\200\355\235\254/2606_\353\260\224\354\235\264\353\237\254\354\212\244_DFS.py" @@ -0,0 +1,38 @@ +#2023-05-11-Week5-과제 +#2606_바이러스 + +''' +입력 + +7 총 컴퓨터 수 +6 컴퓨터 간 연결된 선 개수 +1 2 1번컴퓨터와 2번컴퓨터 연결 +2 3 +... +4 7 +''' + +#dfs +import sys + +vertex = int(sys.stdin.readline()) # 컴퓨터 개수 +edge = int(sys.stdin.readline()) # 연결선 개수 + +graph = [[] for _ in range(vertex + 1)] # 그래프 +visited = [0] * (vertex + 1) # 1번 인덱스부터 사용 + +for i in range(edge): # 그래프 생성 + a, b = map(int, sys.stdin.readline().split()) + graph[a].append(b) # a 에 b 연결 + graph[b].append(a) # b 에 a 연결 + + +def dfs(node): + visited[node] = 1 + for i in graph[node]: + if visited[i] == 0: + dfs(i) + + +dfs(1) # 1번 컴퓨터부터 시작 +print(sum(visited) - 1)