2252 : ์ค ์ธ์ฐ๊ธฐ#
์๊ฐ์ ํ |
๋ฉ๋ชจ๋ฆฌ์ ํ |
์ ๋ต๋น์จ |
---|---|---|
2์ด |
128MB |
56.930% |
๋ฌธ์ #
N๋ช ์ ํ์๋ค์ ํค ์์๋๋ก ์ค์ ์ธ์ฐ๋ ค๊ณ ํ๋ค. ๊ฐ ํ์์ ํค๋ฅผ ์ง์ ์ฌ์ ์ ๋ ฌํ๋ฉด ๊ฐ๋จํ๊ฒ ์ง๋ง, ๋ง๋ ํ ๋ฐฉ๋ฒ์ด ์์ด์ ๋ ํ์์ ํค๋ฅผ ๋น๊ตํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๊ธฐ๋ก ํ์๋ค. ๊ทธ๋๋ง๋ ๋ชจ๋ ํ์๋ค์ ๋ค ๋น๊ตํด ๋ณธ ๊ฒ์ด ์๋๊ณ , ์ผ๋ถ ํ์๋ค์ ํค๋ง์ ๋น๊ตํด ๋ณด์๋ค.
์ผ๋ถ ํ์๋ค์ ํค๋ฅผ ๋น๊ตํ ๊ฒฐ๊ณผ๊ฐ ์ฃผ์ด์ก์ ๋, ์ค์ ์ธ์ฐ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
input#
์ฒซ์งธ ์ค์ N(1 โค N โค 32,000), M(1 โค M โค 100,000)์ด ์ฃผ์ด์ง๋ค.
M์ ํค๋ฅผ ๋น๊ตํ ํ์์ด๋ค. ๋ค์ M๊ฐ์ ์ค์๋ ํค๋ฅผ ๋น๊ตํ ๋ ํ์์ ๋ฒํธ A, B๊ฐ ์ฃผ์ด์ง๋ค.
์ด๋ ํ์ A๊ฐ ํ์ B์ ์์ ์์ผ ํ๋ค๋ ์๋ฏธ์ด๋ค.
ํ์๋ค์ ๋ฒํธ๋ 1๋ฒ๋ถํฐ N๋ฒ์ด๋ค.
output#
์ฒซ์งธ ์ค์ ํ์๋ค์ ์์์๋ถํฐ ์ค์ ์ธ์ด ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค. ๋ต์ด ์ฌ๋ฌ ๊ฐ์ง์ธ ๊ฒฝ์ฐ์๋ ์๋ฌด๊ฑฐ๋ ์ถ๋ ฅํ๋ค.
think#
์์์ ๋ ฌ์ ์ด์ฉํด์ผ ํ๋ ๊ฒ์ด๋ผ ๋ชป ๋ฐ์ ์คฌ๋ค. ํค๊ฐ ํฐ ํ์์ ์์ ํ์์ผ๋ก ์ ์ด ๊ฐ์ง ์๋ ์ผ์ ํ ๋ฐฉํฅ์ฑ(? ์์)๋ฅผ ๊ฐ์ง์ผ๋ก ์ํํ์ง ์๋ ๊ทธ๋ํ๋ผ๊ณ ํ ์ ์๋ค. ์ด๋ค์ ๋ํด์ ์ ๋ ฌ์ ํด์ผํ๋ค. ๊ทธ๋ผ ๋ต์ด ์ฌ๋ฌ๊ฐ์ง ์บ ๊ฒฝ์ฐ๋ ์๋ฌด๊ฑฐ๋ ํ๋ ์ถ๋ ฅํ๋ ๊ฒ ๋ณด๋ ์ญ์ ๊ฐ์ ๋ฑ๊ธ์ ์์์ ๋ ธ๋๊ฐ ์๋ ๊ฒ์์ ํ๋ฆผ์ด๊ฐ ์๋ค.
โ from su
python#
import sys, os
problem_num = "2252"
path = os.getcwd() + f"\\txt\\{problem_num}" + ".txt"
sys.stdin = open(path, "r")
input = sys.stdin.readline
from collections import deque
node, edge = map(int, input().split())
indegrees = [0] * (node + 1)
graph = [[] for i in range(node + 1)]
for _ in range(edge):
in_, out_ = map(int, input().split())
graph[in_].append(out_)
indegrees[out_] += 1
def topology_sort():
result = []
q = deque()
for i in range(1, node + 1):
if indegrees[i] == 0:
q.append(i)
while q:
now = q.popleft()
result.append(now)
for i in graph[now]:
# ํด๋น ์์์ ์ฐ๊ฒฐ๋ ๋
ธ๋๋ค์ ์ง์
์ฐจ์ -1
indegrees[i] -= 1
if indegrees[i] == 0:
q.append(i)
for i in result:
print(i, end=" ")
topology_sort()
3 4 1 2
javascript#
1 const problem_num = "2252";
2 const path = `${__dirname}\\txt\\${problem_num}.txt`;
3 // const path = "/dev/stdin";
4
5 const input = require("fs").readFileSync(path, 'utf8').toString().trim().split('\n');
6 let currentLine = 0;
7
8 function readLine() {
9 return input[currentLine++].split(' ').map(Number);
10 }
11
12 const [node, edge] = readLine();
13 let indegrees = Array(node + 1).fill(0);
14 let graph = Array.from({ length: node + 1 }, () => []);
15
16 for (let i = 0; i < edge; i++) {
17 const [in_, out_] = readLine();
18 graph[in_].push(out_);
19 indegrees[out_] += 1;
20 }
21
22 function topology_sort(node, indegrees) {
23 const result = [];
24 const q = []; // deque style
25 for (let i = 1; i < node + 1; i++) {
26 if (indegrees[i] == 0) {
27 q.push(i)
28 }
29 }
30 while (q.length) {
31 const now = q.shift();
32 result.push(now);
33 for (let i = 0; i < graph[now].length; i++) {
34 const nextNode = graph[now][i];
35 indegrees[nextNode] -= 1;
36 if (indegrees[nextNode] == 0) {
37 q.push(nextNode);
38 }
39 }
40 }
41 return result;
42 }
43
44 result = topology_sort(node, indegrees);
45 console.log(result.join(' '));
array ์์ฑ๋ฐฉ์์ด ํ์ด์ฌ์ด๋ ์ข ๋ค๋ฅด๋ค
๋ฆฌ์คํธ for ์ํ๋ฅผ ํ ๋๋ ๋ฐ์ const๋ก const temp = list[i] ์ด๋ฐ์์ผ๋ก ๋ง๋ค์ด์ ์ฌ์ฉํด ์ค์ผํ๋ค.
javascript array๋ ๊ธฐ๋ณธ์ ์ผ๋ก deque์ฒ๋ผ ์ฌ์ฉํ ์ ์๋ค.
Java#
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int node = Integer.parseInt(st.nextToken());
int edge = Integer.parseInt(st.nextToken());
// ์ง์
์ฐจ์ ๋ฐฐ์ด ์ด๊ธฐํ
int[] indegrees = new int[node + 1];
// ๊ทธ๋ํ ์ด๊ธฐํ(list of ArrayList)
ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
for (int i = 0; i <= node; i++) {
graph.add(new ArrayList<Integer>());
}
// input graph indegrees initialize
for (int i = 0; i < edge; i++) {
st = new StringTokenizer(br.readLine());
int in_ = Integer.parseInt(st.nextToken());
int out_ = Integer.parseInt(st.nextToken());
graph.get(in_).add(out_); // [i] ๊ฐ ์๋๋ผ .get method๋ก index์์ ๊ฒ ๊ฐ์ ธ์ด
indegrees[out_]++;
}
ArrayList<Integer> result = new ArrayList<>();
Queue<Integer> q = new LinkedList<>();
for (int i = 1; i <= node; i++) {
if (indegrees[i] == 0) {
q.add(i);
}
}
while (!q.isEmpty()) {
int now = q.poll();
result.add(now);
for (int i = 0; i < graph.get(now).size(); i++) {
int nextNode = graph.get(now).get(i);
indegrees[nextNode]--;
if (indegrees[nextNode] == 0) {
q.add(nextNode);
}
}
}
for (int i : result) {
System.out.println(i + " ");
}
}
}