-
Notifications
You must be signed in to change notification settings - Fork 2
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
4-H0ngJu #158
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.
μ΄ λ¬Έμ μ½μ§ μμλ°..
골λ2 λ¬Έμ λ₯Ό λ°λ‘ λμ νμ ¨κ΅°μ..
μμ μ λ ¬μ λͺ¨λ₯΄μ ¨λλΌλ μ°μ μμ νλ₯Ό μΊμΉνμ κ²μ΄ λλ¨νμλ€μ.
μ΄ λ¬Έμ λ μ λ²μ PRλ‘ μ¬λ Έλ μ μ΄ μμ΄μ λ§ν¬λ‘ λμ ν κ²μ!
κ³ μνμ ¨μ΅λλ€~~~~~ππ
n, m = map(int, sys.stdin.readline().rstrip().split()) | ||
|
||
graph = [[] for _ in range(n+1)] | ||
inDegree = [0 for _ in range(n+1)] |
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.
μ¬μν κ±°κΈ΄ νλ°, νμ΄μ¬μ 곡μμ μΌλ‘ snake κΈ°λ²μ 컨벀μ μΌλ‘ μ±ννκ³ μμ΄μ!
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.
μμ£ ?
|
||
# μ λ ₯λ°μμ λ£κΈ° | ||
for _ in range(m): | ||
p1, p2 = map(int, sys.stdin.readline().rstrip().split()) |
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.
μ΄ λΆλΆλ λ―ΈμΈ νμ΄κΈ΄νλ°
def input(): return sys.stdin.readline().rstrip()
μΌλ‘ μ μΈν΄λμΌλ©΄
p1, p2 = map(int, input().split())
μΌλ‘ μ¬μ©ν μ μμ΄μ!
(dx, dy κΈ λ―ΈμΈ κΏνμ λλ€.)
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.
νν λ°μλ‘ μ΄λ² λ¬Έμ μ μ μ©νμ΅λλ€ππ
μμ μ μ΅μ νμ μ λͺ°λΌμ νμΌλ‘ μ νκ³ μ λ ¬, λ± λ±λ± μ¨κ° λ°©λ²μΌλ‘ μλν΄μ μ λ μμ νμ λ μ¬λ¦¬μ§λ λͺ»νμλλ°, λ μ¬λ €μ μ κ·Όνμ κ² λλ¨νμΈμ. 보λκΉ μ νμ΄μ κ±°μ κ°κ΅°μ. N, M = map(int, stdin.readline().split())
graph = [[] for _ in range(N + 1)]
indegrees = [0] * (N + 1)
for _ in range(M):
a, b = map(int, stdin.readline().split())
indegrees[b] += 1
graph[a].append(b)
heap = []
for p in range(1, N + 1):
if not indegrees[p]:
heappush(heap, p)
while heap:
p1 = heappop(heap)
print(p1, end=' ')
for p2 in graph[p1]:
if indegrees[p2] < 0:
continue
indegrees[p2] -= 1
if not indegrees[p2]:
heappush(heap, p2) |
import sys | ||
import heapq |
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 heapq import *
λͺ¨λ μν¬νΈ ν λ μμ²λΌ νλ©΄ μ’ λ νΈνκ² μ½λ©ν μ μλ΅λλ€
λ°λ‘ heapqλ₯Ό μ μ¨μ€λ λΌμ
heappush(heap, value)
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> sufficient(n + 1), necessary(n + 1);
vector<bool> isSolved(n + 1);
while (m--) {
int a, b;
cin >> a >> b;
sufficient[a].emplace_back(b);
necessary[b].emplace_back(a);
}
priority_queue<int, vector<int>, greater<>> pq;
for (int i = 1; i <= n; i++) {
if (!necessary[i].empty()) continue;
pq.emplace(i);
}
while (!pq.empty()) {
int cur = pq.top();
pq.pop();
isSolved[cur] = true;
cout << cur << ' ';
for (int i: sufficient[cur]) {
bool isAvailable = true;
for (int j: necessary[i]) {
if (isSolved[j]) continue;
isAvailable = false;
break;
}
if (!isAvailable) continue;
pq.emplace(i);
}
}
return 0;
} μμμ λ ¬μ μ¬μ©ν νμ΄λ νμ£Όλκ³Ό λΉμ·ν΄μ μμμ λ ¬μ λͺ¨λ₯΄λ μκΈ°μ νΌ μ½λλ₯Ό μ¬λ €λ΄€μ΄μ. μ°μ μμ ν λ°λ‘ 체ν¬ν κ±° κ΅Ώμ λλ€. |
π λ¬Έμ λ§ν¬
λ¬Έμ μ§
βοΈ μμλ μκ°
1μκ° 30λΆ
β¨ μλ μ½λ
μ΄ λ¬Έμ μμ μ°μ μμ νμ΄κΈ° λλ¬Έμ νμΌλ‘ μ κ·Όνμμ΅λλ€.
κ·Έλ°λ° μ λ ₯ λ°μ κ°μμ b,c,d,eλ³΄λ€ λ¨Όμ νμ΄μΌ νλ a λ¬Έμ κ° μλ κ²½μ°μ λν΄μ μ΄λ»κ² μ²λ¦¬λ₯Ό ν΄μΌ ν μ§ λͺ°λΌ κ³ λ―Όμ μ« νμ΅λλ€.
3λ²μ λ μ μΆνλλ° ν΅κ³Όκ° μλμ΄μ(
μ§κΈ μκ°ν΄λ³΄λ λ°λ‘κ° λ§λ€μ ..) κ²°κ΅ λ¬Έμ νλ¨μ μλ μκ³ λ¦¬μ¦ λΆλ₯λ₯Ό νμΈνκ³ μμμ λ ¬μ λν κ°λ μ΄ νμν κ² κ°μ μΆκ°λ‘ 곡λΆνκ³ , μ½λλ₯Ό μμ±νμμ΅λλ€.μμμ λ ¬μ΄λ 'μμκ° μ ν΄μ Έμλ μμ 'μ μ°¨λ‘λ‘ μνν΄μΌ ν λ, μμλ₯Ό κ²°μ νκΈ° μν μκ³ λ¦¬μ¦μ΄λ€. μ ννκ² μ΄ λ¬Έμ μ λ§μλ¨μ΄μ§λ μκ³ λ¦¬μ¦μ΄λΌκ³ λ³Ό μ μλ€.
μμ μ λ ¬μμ μ¬μ©λλ κ°λ 2κ°μ§κ° μλλ°, 'μ§μ μ°¨μ'μ 'μ§μΆ μ°¨μ' μ΄λ€.
μλμ μ¬μ§μ΄ μ§μ μ°¨μ/μ§μΆμ°¨μμ μμ΄λ€.
κΈ°λ³Έμ μΈ μμμ λ ¬ μκ³ λ¦¬μ¦μ μλμ½λλ μλμ κ°λ€.
λκ°μ΄ μ΄ λ¬Έμ μλ μμμ λ ¬μ μ μ©ν μ μλ€.
p1 - p2 λ‘ μ°κ²°λμ΄ μμΌλ―λ‘, p2μ κ°μ + 1.
π μλ‘κ² μκ²λ λ΄μ©