-
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
50-9kyo-hwang #181
50-9kyo-hwang #181
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.
Nκ³Ό Mμ΄ λ무 ν¬λ€λ³΄λ ,O(NM) O(N**2)... μ΄μμΌλ‘λ ν΄κ²°ν μ μμ΄λ³΄μμ΅λλ€..
κ·Έλμ N +M μΌλ‘ μνλ₯Ό ν λ² νκ³ μ νμ μ λ¨κ²¨,
κ° λ
Έλκ° μ΄λκΉμ§ μ κ·Όμ΄ κ°λ₯νμ§λ₯Ό κΈ°λ‘ν΄μ£Όλ©΄ μ΄λ¨κΉλ μμ΄λμ΄λ₯Ό λ μ¬λ Έμ΄μ.
κ·Έλ°λ°.. 루νκ° μμ κ²½μ°μ λν΄μ μ κ³Όμ μ λΆκ°νλκ΅°μ π’(20λΆ νμ§)
κ²°κ΅μ κ΅ν©λκ»μ μ°Έκ³ ν΄μ£Όμ κ°μ°κ²°κ·Έλν κ΄λ ¨ λ΄μ© μ½κ³ κ²¨μ° νμλ€μ γ
λ무 μ’μ λ¬Έμ ..! κ³ μνμ ¨μ΅λλ€π
public class Main {
static boolean[] visited;
static Stack<Integer> stack = new Stack<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int v = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
visited = new boolean[v + 1];
List<Integer>[] graph = new ArrayList[v + 1];
List<Integer>[] reverseGraph = new ArrayList[v + 1];
for(int i = 1; i <= v; i++) {
graph[i] = new ArrayList<>();
reverseGraph[i] = new ArrayList<>();
}
for(int i = 0; i < e; i++) {
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
graph[from].add(to);
reverseGraph[to].add(from);
}
for(int i = 1; i <= v; i++) {
if(!visited[i]) {
dfs(i, graph, false);
} //λ°©λ¬Έ νμ§ μμλ€λ©΄ dfsλ‘ λ°©λ¬Έ
}
visited = new boolean[v + 1];
dfs(stack.pop(), reverseGraph, true);
String answer = "Yes";
while(!stack.isEmpty()) {
// μ€νμ΄ λΉμ΄μμλκΉμ§ κΊΌλ΄λλ°, λ°©λ¬Ένμ§ μμλ€λ©΄ λͺ¨λ μ°κ²°λμ΄ μμ§ μμΌλ―λ‘ No
if(!visited[stack.pop()]) {
answer = "No";
break;
}
}
System.out.print(answer);
}
public static void dfs(int depth, List<Integer>[] graph, boolean isReverseGraph) {
visited[depth] = true;
for(int a : graph[depth]) {
if(!visited[a]) {
dfs(a, graph, isReverseGraph);
}
}
// μ λ°©ν₯ κ·ΈλνμΌ κ²½μ°μλ§ stackμ μ μ₯
if(!isReverseGraph) {
stack.add(depth);
}
}
}
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.
SCC..! μμ² μμΈνκ² μ¨μ£Όμ μ€λͺ
λλΆμ μ½κ² μ΄ν΄νκ³ κ°λλ€!
μμΌλ‘ μμμ μ΄ μ£Όμ΄μ§μ§ μκ³ μ΄λ»κ²λ λͺ¨λ λ
Έλλ₯Ό λ°©λ¬Έν μ μλμ§λ₯Ό 묻λ λ¬Έμ κ° λμ€λ©΄, κ°μ°κ²° κ·Έλνλ₯Ό λ μ¬λ¦¬λ©΄ λκ² κ΅°μ!
λ¬Έμ λ₯Ό λμ ν΄λ³΄μλλ° μκ°μ΄κ³Όκ° μμ²λκ² λ¬μ΅λλ€..
κ·Έλμ μ½μ¬λΌμ£Ό μκ³ λ¦¬μ¦μΌλ‘ λ€μ νμ΄λ³΄λ €λ€ μκ°μ΄ μ€λ 걸릴 κ² κ°μ μ°μ 리뷰 κΈνκ² λ¨κΉλλ€!!
μκ°μ΄κ³Όλ¬μ§λ§ μ°μ μ¬λ¦¬λλ‘ νκ² μ΅λλ€π€―
μκ°μ΄κ³Ό λ μ½λ...
from collections import deque
import sys
sys.setrecursionlimit(10**6)
input = open(0).readline
nation_num, flight_num = map(int, input().split())
forward_graph = [[] for _ in range(nation_num + 1)]
reverse_graph = [[] for _ in range(nation_num + 1)]
for i in range(flight_num):
n, m = map(int, input().split())
forward_graph[n].append(m)
reverse_graph[m].append(n)
def DFS(q, visited, graph):
while q:
x = q.popleft()
if not visited[x]:
visited[x] = 1
for node in graph[x]:
q.append(node)
DFS(q, visited, graph)
for k in range(1, nation_num+1):
q_f, q_r = deque([k]), deque([k])
visited_f = [1] + [0 for _ in range(nation_num)]
visited_r = [1] + [0 for _ in range(nation_num)]
DFS(q_f, visited_f, forward_graph)
DFS(q_r, visited_r, reverse_graph)
if sum(visited_f) == nation_num+1 and sum(visited_r) == nation_num+1:
print("Yes")
break
elif k == nation_num and(sum(visited_f) != nation_num or sum(visited_r) != nation_num):
print("No")
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λ² μ§μ μμ μμν΄μ, λͺ¨λ κ²½λ‘μ λν΄ λ€μ 1λ² μ§μ μΌλ‘ λμμ¬ μ μλ€.
- μ κ³Όμ μμ λͺ¨λ λ Έλλ₯Ό μ§λ¬λ€.
μμ 2κ°μ§κ° λλ€λ©΄, Yesλ₯Ό μΆλ ₯νλ€! λΌκ³ νκ³ μΆμμ§λ§...
μ½λ μ§κΈ°μ μ€ν¨νκ³ , μ΄μ°μ μ° μΌλ¨ μ μΆνλλ°, μκ° μ΄κ³ΌλΌλ κ²°κ³Όκ°...γ
γ
μ΄κ² μ μκ° μ΄κ³Ό λμ§
SCCλΌλ... μμ¦ νλ©΄ ν μλ‘ μμ§ λͺ» λ€μ΄λ³Έ μκ³ λ¦¬μ¦λ μμ² λ§μ΄ λ¨μλ€λκ±Έ κΉ¨λ«μ΅λλ€...
γ ...ν μκ°λ§ λ μ΄ λ¬Έμ μ ν¬μν΄λ³Όκ²μ
π λ¬Έμ λ§ν¬
26146 μ¦ν₯ μ¬ν (Easy)
βοΈ μμλ μκ°
30λΆ
β¨ μλ μ½λ
1. λ¬Έμ
Nκ°μ λ Έλμ Mκ°μ μ£μ§λ‘ μ΄λ£¨μ΄μ§ λ°©ν₯ κ·Έλνκ° μ‘΄μ¬νλ€.
μ΄λ€ λ Έλμμ μΆλ°νλ μ§, λ€λ₯Έ λͺ¨λ λ Έλλ‘ κ° μ μλμ§ κ²μ¬νλΌ.
쑰건
μμ 1
λ¬΄μ¨ μμμ μ μ ννλ λͺ¨λ μ μ μ λ°©λ¬Έν μ μλ κ²½λ‘λ₯Ό μ°Ύμ μ μμΌλ―λ‘, λ΅μ Yesκ° λλ€. μλλ κ°λ₯ν κ²½λ‘ μ€ νλμ΄λ€.
μμ 2
1λ² μ μ μμ μΆλ°νλ©΄ λͺ¨λ μ μ μ λ°©λ¬Έν μ μλ κ²½λ‘κ° μ‘΄μ¬νμ§λ§, 2λ² μ μ μμ μΆλ°νλ©΄ λͺ¨λ μ μ μ λ°©λ¬Έν μ μλ κ²½λ‘κ° μ‘΄μ¬νμ§ μμΌλ―λ‘, λ΅μ Noκ° λλ€.
2. νμ΄
μ λλ€μμ "κ°μ°κ²° κ·Έλν"μ κ΄ν PRμ μμ±νλλ°, μ΄λ₯Ό λ³΄κ³ μμ μ μκ³ λ¦¬μ¦ κΈ°λ§κ³ μ¬ μνμ λμλ λ΄μ©μ΄ λ μ¬λλ€.
μ μ΄λ―Έμ§μμ μ ν μ€λͺ λλ‘, κ°μ°κ²° κ·ΈλνμΈμ§ νμΈνλ λ°©λ²μ μλμ κ°λ€.
μ΄λ₯Ό ν λλ‘ μ½λλ₯Ό μμ±νλ€.
μ λ ₯μΌλ‘ μ£μ§λ€μ΄ μ£Όμ΄μ§ λ, μ λ°©ν₯ κ·Έλν$G$ μ μλ°©ν₯ κ·Έλν $G^C$ λ₯Ό ꡬμΆνλ€.
DFS ν¨μλ₯Ό λ κ·Έλνμ λν΄ νΈμΆν΄ λͺ¨λ Trueκ° λ°νλλ€λ©΄ "Yes"λ₯Ό μΆλ ₯νκ³ , νλλΌλ Falseκ° λ°νλλ€λ©΄ "No"λ₯Ό μΆλ ₯νλ€.
DFS ν¨μλ λ°©λ¬Έ 체ν¬λ₯Ό νμΈνλ Visited 리μ€νΈλ₯Ό μμ±νλ€, μ€μ§μ μΌλ‘ DFSλ₯Ό μννλ DFSHelper ν¨μλ₯Ό νΈμΆνλ μν μ λ΄λΉνλ€.
DFSHelper ν¨μλ₯Ό μνν λ€ Visited 리μ€νΈλ₯Ό μνν΄ νλλΌλ λλ¬νμ§ λͺ»ν λ Έλκ° μ‘΄μ¬νλ return false, μλλΌλ©΄ return trueλ₯Ό λ°ννλ€.
DFSHelper ν¨μλ μ νμ μΈ DFS μν ν¨μμ΄λ€. "μμμ λ Έλ"μ λν΄ μΆλ°νλ©΄ λλ―λ‘ μ΅μ΄ μΆλ° λ Έλλ₯Ό 1λ²μΌλ‘ μ§μ νλ€.
μ΄ μκ³ λ¦¬μ¦μ 2λ²μ DFSλ§ μννλ©΄ λλ―λ‘ μκ° λ³΅μ‘λλ$O(2 * (N + M)) = O(N + M)$ μ΄λ€.
μ 체 μ½λ
π μλ‘κ² μκ²λ λ΄μ©
μ¬μ€ μ΄ λ΄μ©μ SCC(Strongly Connected Component, κ°ν μ°κ²° μμ) μ λν νΉμ μΌμ΄μ€μ΄λ€.
μΌλ°μ μΌλ‘ κ·Έλν λ΄ μ¬λ¬ κ°μ SCCλ₯Ό μΆμΆνλ νμμΈλ°, μ΄ λ¬Έμ λ μ 체 κ·Έλνμ λν΄ SCCκ° νλλ§ μ‘΄μ¬νλ κ²½μ°μ΄λ€.
SCCλ₯Ό μΆμΆνλ λνμ μΈ μκ³ λ¦¬μ¦ 2κ°μ§κ° "μ½μ¬λΌμ£Ό μκ³ λ¦¬μ¦"κ³Ό "νμ μκ³ λ¦¬μ¦"μΈλ°, μ¬κΈ°μ μ¬μ©λ μκ³ λ¦¬μ¦μ΄ "μ½μ¬λΌμ£Ό μκ³ λ¦¬μ¦"κ³Ό κ±°μ λμΌνλ€.
λ§μ½ μ΄ μκ³ λ¦¬μ¦μ μ¬μ©νμ§ μλλ€λ©΄ "λͺ¨λ λ Έλμ λν΄" ν λ²μ© BFS/DFSλ₯Ό μνν΄μΌ νλλ°, μ΄λ$O(N * (N + M))$ μκ°λ³΅μ‘λκ° κ±Έλ € μ΅μ
μ κ²½μ° 1400μ΅λ²μ μ°μ°μ΄ νμν΄ μκ°μ΄ μ΄κ³Όνλ€.