-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6-Me-in-U #20
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μμ μ λ ¬μ DFSλ₯Ό μ΄μ©ν΄μλ μνν μ μμ£ . λ³λλ‘ μ§μ μ°¨μλ₯Ό κΈ°λ‘ν νμ μμ΄, κΈ°λ³Έμ μΈ DFS μννλ― νλ ν¨μ λ§μ§λ§μ λ°©λ¬Έν λ Έλλ₯Ό λ£λ μμ λ³νλ§ μ£Όλ©΄ λκΈ° λλ¬Έμ μ½λκ° BFS λ°©μμ λΉν΄ μ¬ννλ€κ³ μκ°λλ€μ :)
import java.io.*;
import java.util.*;
public class Main {
static int N, M;
static List<Integer>[] adjacencyList;
static List<Integer> orderOfVisit;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
adjacencyList = new ArrayList[N + 1];
for (int i = 0; i <= N; i++) {
adjacencyList[i] = new ArrayList<>();
}
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
adjacencyList[A].add(B);
}
topologySort();
Collections.reverse(orderOfVisit);
for (int node : orderOfVisit) {
bw.write(node + " ");
}
bw.flush();
bw.close();
br.close();
}
static void DFS(boolean[] visited, int srcNode) {
visited[srcNode] = true;
for (int dstNode : adjacencyList[srcNode]) {
if (!visited[dstNode]) {
DFS(visited, dstNode);
}
}
orderOfVisit.add(srcNode);
}
static void topologySort() {
boolean[] visited = new boolean[N + 1];
orderOfVisit = new ArrayList<>();
for (int node = 1; node <= N; node++) {
if (!visited[node]) {
DFS(visited, node);
}
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split())
in_degree = [0]*(N+1)
graph = [[] for _ in range(N+1)]
q = deque()
answer = []
for i in range(M):
a, b = map(int, input().split())
graph[a].append(b)
in_degree[b] += 1
for i in range(1, N+1):
if in_degree[i] == 0:
q.append(i)
while q:
tmp = q.popleft()
answer.append(tmp)
for i in graph[tmp]:
in_degree[i] -= 1
if in_degree[i] == 0:
q.append(i)
for i in answer:
print(i, end=' ')
μμμ λ ¬μ λν΄ μ²μ μκ² λμλ€μ. μ λ bfsλ‘ νμ΄λ΄€μ΅λλ€. κ³ μνμ ¨μ΅λλ€!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ΄λ²κΈ°νμ μμμ λ ¬μ λν΄μ μκ²λκ³ κ³΅λΆν΄λ³΄κ² λμλ€μ!
μκ³ νμ ¨μ΅λλ€~
from collections import deque
def sort(N,comparisons):
graph=[[] for _ in range(N+1)]
in_degree=[0]*(N+1)
for A,B in comparisons:
graph[A].append(B)
in_degree[B]+=1
queue=deque()
for i in range(1,N+1):
if in_degree[i]==0:
queue.append(i)
result=[]
while queue:
current=queue.popleft()
result.append(current)
for neighbor in graph[current]:
in_degree[neighbor]-=1
if in_degree[neighbor]==0:
queue.append(neighbor)
return result
N,M=map(int,input().split())
comparisons=[]
for _ in range(M):
a,b=map(int,input().split())
comparisons.append((a,b))
sorted_students=sort(N,comparisons)
print(' '.join(map(str,sorted_students)))
π λ¬Έμ λ§ν¬
2252λ² μ€ μΈμ°κΈ°
βοΈ μμλ μκ°
1μκ°
β¨ μλ μ½λ
μμ μ λ ¬(topological sorting)μ μ ν₯ κ·Έλνμ κΌμ§μ λ€(vertex)μ λ³μ λ°©ν₯μ κ±°μ€λ₯΄μ§ μλλ‘ λμ΄νλ κ²μ μλ―Ένλ€.
μμ) λ§μ½ νΉμ μκ°κ³Όλͺ©μ μ μκ³Όλͺ©μ΄ μλ€λ©΄ κ·Έ μ μ κ³Όλͺ©λΆν° μκ°ν΄μΌ νλ―λ‘, νΉμ κ³Όλͺ©λ€μ μκ°ν΄μΌ ν λ μμ μ λ ¬μ ν΅ν΄ μ¬λ°λ₯Έ μκ° μμλ₯Ό μ°ΎμλΌ μ μλ€.
1.
Input: G = (V, E)
μ΄κΈ° μν
κ·Έλν G
1 -> 3
2 -> 3
3 ->
inDegree[]
{0,0,2}
νμ μ°¨μκ° 0μΈ μ μ μΆκ°
λ°λ³΅ Queue(1,2)
{
νμμ 1μ κΊΌλ΄κ³ "1" μΆλ ₯
1κ³Ό μ°κ²°λ μ μ 3μ μ°¨μλ₯Ό 1κ°μ => inDegree[]{0,0,1}
}
λ°λ³΅ Queue(2)
{
νμμ 2μ κΊΌλ΄κ³ "2" μΆλ ₯
2μ μ°κ²°λ μ μ 3μ μ°¨μλ₯Ό 1κ°μ => inDegree[]{0,0,0}
μ€μ΄λ μ°¨μκ° 0μ΄ λμμμΌλ‘ νμ 0μΆκ°
}
λ°λ³΅ Queue(3)
{
νμμ 3μ κΊΌλ΄κ³ "3" μΆλ ₯
}
μ’ λ£.
μΆλ ₯ κ²°κ³Ό 1,2,3
νμ λ£λ μμμ λ°λΌμ 2,1,3μ΄ λ μλ μλ€.
λ¬Όλ‘ μ΄κ²λ μ λ΅μ΄λ€.