๐ก Info
๋ด์ฉ
N๋ช ์ ํ์๋ค์ ํค ์์๋๋ก ์ค์ ์ธ์ฐ๋ ค๊ณ ํ๋ค. ๊ฐ ํ์์ ํค๋ฅผ ์ง์ ์ฌ์ ์ ๋ ฌํ๋ฉด ๊ฐ๋จํ๊ฒ ์ง๋ง, ๋ง๋ ํ ๋ฐฉ๋ฒ์ด ์์ด์ ๋ ํ์์ ํค๋ฅผ ๋น๊ตํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๊ธฐ๋ก ํ์๋ค. ๊ทธ๋๋ง๋ ๋ชจ๋ ํ์๋ค์ ๋ค ๋น๊ตํด ๋ณธ ๊ฒ์ด ์๋๊ณ , ์ผ๋ถ ํ์๋ค์ ํค๋ง์ ๋น๊ตํด ๋ณด์๋ค.
์ผ๋ถ ํ์๋ค์ ํค๋ฅผ ๋น๊ตํ ๊ฒฐ๊ณผ๊ฐ ์ฃผ์ด์ก์ ๋, ์ค์ ์ธ์ฐ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
๐ฅ์ ๋ ฅ ์กฐ๊ฑด
3 2
1 3
2 3
4 2
4 2
3 1
๐ค์ถ๋ ฅ ์กฐ๊ฑด
1 2 3
4 2 3 1
์ค์ ํ์ด ์๊ฐ : 14๋ถ
import java.util.*;
public class Main {
static int N;
static int M;
static int[][] arr;
static boolean[] visited;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
sc.nextLine();
arr = new int[N + 1][N + 1];
for (int i = 0; i < M; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
arr[from][to] = 1;
arr[to][from] = 1;
}
visited = new boolean[N + 1];
dfs(1); // ์ ์ ์ด 1๋ถํฐ ์์ํจ
System.out.println();
}
public static void dfs(int V) {
visited[V] = true;
System.out.print(V + " ");
for (int i = 1; i <= N; i++) {
if (arr[V][i] == 1 && !visited[i])
dfs(i);
}
}
}
//before
public static void dfs(int V) {
visited[V] = true;
System.out.print(V + " ");
for (int i = 1; i <= N; i++) {
if (arr[V][i] == 1 && !visited[i])
dfs(i);
}
}
//after
//before
...
static int[][] arr;
static int[] indegree;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
sc.nextLine();
arr = new int[N+1][N+1];
indegree = new int[N+1];
for(int i=0; i<M; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
arr[from][to] = 1;
indegree[to]++;
}
bfs();
}
public static void bfs(){
Queue<Integer> queue = new LinkedList<>();
List<Integer> result = new ArrayList<>();
// ์ง์
์ฐจ์๊ฐ 0์ธ ๋
ธ๋๋ฅผ ํ์ ์ถ๊ฐ
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) {
queue.add(i);
}
}
while (!queue.isEmpty()) {
int current = queue.poll();
result.add(current); // ์ฒ๋ฆฌ๋ ์ ์ ์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ถ๊ฐ
for (int i = 1; i <= N; i++) {
if (arr[current][i] == 1) {
indegree[i]--; // ํด๋น ์ ์ ์์ ๋๊ฐ๋ ๊ฐ์ ์ ์ ๊ฑฐ
if (indegree[i] == 0) {
queue.add(i);
}
}
}
}
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
for (int node : result) {
System.out.print(node + " ");
}
}
}
//after
...
static List<Integer>[] List;
static int[] indegree;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
//sc.nextLine(); -> ์ด๊ธฐํ๋ ์ญ์ !!
//arr = new int[N+1][N+1];
List = new ArrayList[N+1];
indegree = new int[N+1];
for (int i = 1; i <= N; i++) {
List[i] = new ArrayList<>();
}
for(int i=0; i<M; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
//arr[from][to] = 1;
List[from].add(to);
indegree[to]++;
}
bfs();
}
public static void bfs(){
//Queue<Integer> queue = new LinkedList<>();
//List<Integer> result = new ArrayList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>();
// ์ง์
์ฐจ์๊ฐ 0์ธ ๋
ธ๋๋ฅผ ํ์ ์ถ๊ฐ
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) {
pq.add(i);
}
}
while (!pq.isEmpty()) {
int n = pq.poll();
System.out.print(n + " ");
for (int i : List[n]) {
indegree[i]--; // ํด๋น ์ ์ ์์ ๋๊ฐ๋ ๊ฐ์ ์ ์ ๊ฑฐ
if (indegree[i] == 0) {
pq.add(i);
}
}
}
}
}
์ค์ ํ์ด ์๊ฐ : 1์๊ฐ 6๋ถ(์ฒซ ํ์ด ์๊ฐ ํฌํจ)
import java.util.*;
public class Main {
static int N;
static int M;
//static int[][] arr;
static List<Integer>[] List;
static int[] indegree;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
//sc.nextLine();
//arr = new int[N+1][N+1];
List = new ArrayList[N+1];
indegree = new int[N+1];
for (int i = 1; i <= N; i++) {
List[i] = new ArrayList<>();
}
for(int i=0; i<M; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
//arr[from][to] = 1;
List[from].add(to);
indegree[to]++;
}
bfs();
}
public static void bfs(){
//Queue<Integer> queue = new LinkedList<>();
//List<Integer> result = new ArrayList<>();
PriorityQueue<Integer> pq = new PriorityQueue<>();
// ์ง์
์ฐจ์๊ฐ 0์ธ ๋
ธ๋๋ฅผ ํ์ ์ถ๊ฐ
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) {
pq.add(i);
}
}
while (!pq.isEmpty()) {
int n = pq.poll();
System.out.print(n + " ");
for (int i : List[n]) {
indegree[i]--; // ํด๋น ์ ์ ์์ ๋๊ฐ๋ ๊ฐ์ ์ ์ ๊ฑฐ
if (indegree[i] == 0) {
pq.add(i);
}
}
}
}
}