-
Notifications
You must be signed in to change notification settings - Fork 1
32-g0rnn #128
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
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.
λ¬Έμ λ₯Ό λ³΄κ³ μμμ λ ¬μ΄ λ μ¬λμ΅λλ€. (μ¬μ€ 1λ¬μ μ νμλ λ¬Έμ λκ΅°μ 볡μ΅μΌμ νμ΄λ΄€μ΅λλ€.)
μμμ λ ¬μ μ΄λ¦λ§ λ³΄κ³ λ¬΄μΈκ° μ λ ¬μ νλ € μκ°νλ€λ©΄, μλ§ λ§€μ° μ΄λ €μΈ κ±°μμ.
μμμ λ ¬μ ν΅μ¬μ κ· νΈλμ΄ λ§μνλλ‘ μ°μ μμνμ μ§μ
κ°μ μ΄ 0μΈ λ
Έλλ€μ μ§μ΄λ£κ³ ,
κ·Έ νλ₯Ό μννλ©° κ³μν΄μ μ§μ
κ°μ μ νλ μ€μ΄κ³ , μ§μ
κ°μ μ΄ 0 μΈ λ
Έλλ₯Ό μ½μ
νλ κ²μ΄μ£ .
μμ μ μ κ° κΈ°λ‘ν pdfλ₯Ό μ΄ν΄λ³΄λ κΆμ€ν κ΅μλκ»μ λ§μνμ μ΄ λ°©μμ λ¨μ μ
- μ§μ κ°μ μ΄ 0μΈ λ Έλλ₯Ό μ΄λ»κ² μ°Ύμ κ²μΈκ°? (μ‘΄μ¬νμ§ μλλ€λ©΄?)
- μ§μΆ κ°μ μ μ΄λ»κ² μ κ±°ν κ²μΈκ°?
λΌκ³ ν©λλ€.
κ·Όλ° μλ§ μ΄ λ°©μμ "μ½λ© ν
μ€νΈ" μμλ κ³ λ €νμ§ μμλ λ λ¬Έμ κ°λ€μ.
κ΅μλμ λ²‘ν° νλκ° μ£Όμ΄μ‘μλ μμμ λ ¬μ λ°ννλ "μκ³ λ¦¬μ¦" κ·Έ μ체λ₯Ό κ΄μ μΌλ‘ 보μ κ² κ°μ΅λλ€.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> tSort(vector<vector<int>>& graph, vector<int> indegree, int N) {
priority_queue<int, vector<int>, greater<>> pq;
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) pq.push(i);
}
vector<int> result;
while (!pq.empty()) {
int cur = pq.top();
pq.pop();
result.push_back(cur);
for (int next : graph[cur]) {
if (--indegree[next] == 0) pq.push(next);
}
}
return result;
}
int main() {
int N, M;
cin >> N >> M;
vector<int> indegree(N + 1, 0);
vector<vector<int>> graph(N + 1);
for (int i = 0; i < M; i++) {
int A, B;
cin >> A >> B;
graph[A].push_back(B);
indegree[B]++;
}
vector<int> answer = tSort(graph, indegree, N);
for (int i : answer) cout << i << " ";
return 0;
}
kokeunho
left a comment
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.
μμμ λ ¬μ΄μκ΅°μ...
μ°μ μμνλ₯Ό μ¬μ©ν΄μΌν κ±°λΌκ³ μκ°νκΈ°λ νλλ°
κ·Έ λ€λ‘λ νμ΄λ²μ΄ λ μ€λ₯΄μ§ μμμ νμ΄ μ°Έκ³ νμ΅λλ€.
μκΎΈ μ€λ΅μ΄ λ μ λ΄€λλ μ΅κ΄μ± Scanner μ¬μ©μΌλ‘ μ
λ ₯μ μκ°μ΄ μ΄κ³Όλ κ²μ΄μμ΅λλ€..
μμΌλ‘ κ·Έλ₯ BufferReader μ¨μΌκ² μ΅λλ€.
μμ μ λ ¬ μκ³ λ¦¬μ¦ μμ
μμ λ€μμλλ° κ΅¬νμ κΈ°μ΅μ΄ μλ¬λ€μ
λ€μ νλ² λ³΅μ΅ ν μ μμλ λ¬Έμ μμ΅λλ€.
μκ³ νμ
¨μ΅λλ€~
+λ¨μ μ μκ°μ΄ μλλλ° μμμ λ ¬ κ²°κ³Όκ° μ¬λ¬κ°κ° λμ¬ μ μμ§ μλμ?
Details
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N, M;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
List<Integer>[] graph = new ArrayList[N+1];
for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>();
int[] indegree = new int[N+1];
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int A = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
graph[A].add(B);
indegree[B]++;
}
br.close();
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int i = 1; i <= N; i++) {
if (indegree[i] == 0) {
pq.offer(i);
}
}
StringBuilder sb = new StringBuilder();
while (!pq.isEmpty()) {
int current = pq.poll();
sb.append(current).append(" ");
for (int next : graph[current]) {
indegree[next]--;
if (indegree[next] == 0) {
pq.offer(next);
}
}
}
System.out.print(sb.toString().trim());
}
}
9kyo-hwang
left a comment
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λ‘λ ν΄κ²°μ΄ μλμ£ ... κ·Έλμ μ°μ μμ νλ₯Ό μ¬μ©ν λ°©μμΌλ‘ λ°μ ν μ μκ² λμ£ .
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M; cin >> N >> M;
vector<vector<int>> graph(N + 1);
vector<int> indegrees(N + 1, 0);
while(M--) {
int A, B; cin >> A >> B;
graph[A].emplace_back(B);
indegrees[B]++;
}
priority_queue<int, vector<int>, greater<>> pq;
for(int i = 1; i <= N; ++i) {
if(indegrees[i] == 0) {
pq.emplace(i);
}
}
vector<int> answer;
while(N--) {
if(pq.empty()) {
break;
}
int u = pq.top();
pq.pop();
answer.emplace_back(u);
for(int v : graph[u]) {
if(--indegrees[v] == 0) {
pq.emplace(v);
}
}
}
for(int i : answer) {
cout << i << " ";
}
return 0;
}23λ λ 1νκΈ°μΈκ° κ΅λ΄ μ½ν μμ μμμ λ ¬ λμ¨ κ±° λλ°°κΈ° κΉ¨μ Έμ νμ°½ μμμ λ ¬ λ§μ΄ νμμλλ° νλμ μνλ€κ³ λ κ°λ¬Όκ°λ¬Ό...
λ§μ΅λλ€. μμ μ λ ¬ μ체λ μ¬λ¬κ°μ§κ° λμ¬ μ μμΌλ λ¬Έμ 쑰건 3μ "κ°λ₯νλ©΄ μ¬μ΄ λ¬Έμ λΆν° νμ΄μΌνλ€." κ° μμΌλ―λ‘ λμ¬ μ μλ λ¬Έμ μμλ 1κ°μ§μ λλ€. |
π λ¬Έμ λ§ν¬
λ¬Έμ μ§
βοΈ μμλ μκ°
30m
β¨ μλ μ½λ
μλ‘μ΄ μ νμ λ¬Έμ λ₯Ό μ°Ύκ³ μΆμλ° μκ°λ³΄λ€ μ½μ§κ° μλ€μ. μμ μ νλ² λ€λ€λ³Έμ μλ μ νμ λλ€.
λ°©ν₯μ±μ΄ μκ³ μ¬μ΄ν΄μ΄ μλ κ·Έλνμμ μ μ λ€μ μμλλ‘ λμ΄νλ μμμ λ ¬ λ¬Έμ μ λλ€. μ κ° μ¬μ©ν λ°©λ²μ μΉΈ μκ³ λ¦¬μ¦μΌλ‘ μ§μ μ°¨μκ° 0μΈ μμλ€μ κ³μ νμ λ£κ³ , νμ λ£μ λ μ§μ μ°¨μλ₯Ό μ€μ¬μ€λλ€. μ΄λ κ² νλ©΄ μμμ λ ¬μ λ§λ€ μ μμ΅λλ€.
μκ° λ³΅μ‘λλ O(V+E) μ¦, λ Έλμ κ°μ + μ£μ§μ κ°μμ λλ€.
κΆμ€ν κ΅μλμ μμ λ μ΄ λ°©λ²μ λ¨μ μ΄ μμλκ±° κ°μλ°.. νΉμ κΈ°μ΅λμλ λΆμλμ?
π μλ‘κ² μκ²λ λ΄μ©