-
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
34-pknujsp #135
34-pknujsp #135
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.
from collections import defaultdict, deque
import sys
total_test_case = int(sys.stdin.readline())
A = 0
B = 1
for _ in range(total_test_case):
vertex_count, edge_count = map(int,sys.stdin.readline().split())
graph_information = defaultdict(set)
answer = True
for _ in range(edge_count):
start, destination = map(int,sys.stdin.readline().split())
graph_information[start].add(destination)
graph_information[destination].add(start)
a_set, b_set = set(), set()
for vertex in graph_information:
if vertex in a_set or vertex in b_set: continue
deq = deque()
if len(a_set) == 0 and len(b_set) == 0:
deq.append([vertex, A])
a_set.add(vertex)
if len(graph_information[vertex] - a_set) == len(graph_information[vertex]):
deq.append([vertex, A])
a_set.add(vertex)
elif len(graph_information[vertex] - b_set) == len(graph_information[vertex]):
deq.append([vertex, B])
a_set.add(vertex)
while deq:
now, flag = deq.popleft()
for neighbor_vertex in graph_information[now]:
if flag == A and neighbor_vertex in b_set: continue
if flag == B and neighbor_vertex in a_set: continue
if flag == A and neighbor_vertex in a_set:
answer = False
break
if flag == B and neighbor_vertex in b_set:
answer = False
break
if flag == A:
b_set.add(neighbor_vertex)
deq.append([neighbor_vertex, B])
elif flag == B:
a_set.add(neighbor_vertex)
deq.append([neighbor_vertex, A])
# print(a_set, b_set)
if answer: print("YES")
else: 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.
μ΄λΆ κ·Έλν: μΈμ ν λ
ΈλλΌλ¦¬ μλ‘ λ€λ₯Έ μ§ν©μ λ€μ΄κ° μ μλ κ·Έλν
μ μ λ€μ λ κ°μ§ μμΌλ‘ μΉ ν λ, μΈμ ν μ μ μ΄ κ°μ μμ΄λΌλ©΄ μ΄λΆ κ·Έλνκ° μλ κ²!
(μΆμ²: https://code-angie.tistory.com/21)
DFS μννμ¬ νμ¬λ
Έλκ° 1μ΄λ©΄ μΈμ λ
Έλλ -1, μΈμ λ
Έλμ μΈμ λ
Έλλ 1 .. λ°λ³΅νμ¬
μμ μΉ νμ λ νμ¬λ
Έλμ μΈμ λ
Έλκ° κ°μ μμ΄λΌλ©΄ μ΄λΆ κ·Έλνκ° μλλ€
κ·Έλν λ¬Έμ μ½μ§ μλ€μπ
κΈ°λ³Έ μ
ν
λΆν° μ΄ν΄κ° μ μκ°μ
PR보면μ λ¬Έμ μ μ΄λ»κ² μ κ·Όνλμ§ μ΅νμ΅λλ€
κ·Έλν λ¬Έμ λ λμ΄λλ³λ‘ νμ΄λ΄μΌκ² μ΄μ
μ’μ νμ΄ κ°μ¬ν©λλ€!!
π λ¬Έμ λ§ν¬
μ΄λΆ κ·Έλν
βοΈ μμλ μκ°
1μκ° μ΄μ
μ κ·Όμ μμ ν μλͺ»νμ¬ λ§μ΄ μμ
β¨ μλ μ½λ
κ°μ μ§ν© λ΄μ λ ΈλλΌλ¦¬ μλ‘ μ°κ²°λμ§ μλλ‘ κ·Έλνμ μ μ λ€μ λ μ§ν©μΌλ‘ λλ μ μλμ§ νλ³νλ λ¬Έμ
μ λμ¨ νμΈλλ‘ νμ΄λ₯Ό μμνλ€κ° μλ κ±Έ κΉ¨λ«κΈ° κΉμ§ μκ°μ λ§μ΄ μ΄ λ¬Έμ μ λλ€
μΌλΆ μΌμ΄μ€λ§ λ³΄κ³ κ·Έλν λ΄μ μ¬μ΄ν΄ μ¬λΆκ° μ€μνλ€κ³ μκ°νλλ° μλμμ΅λλ€
1. 무방ν₯ κ·Έλνλ₯Ό μμ±
2. μ μ μ κ·Έλ£ΉννκΈ° μν΄ λ¦¬μ€νΈ μμ±
κ° μ μ μ 1 λλ -1μ κ°μ λΆμ¬νμ¬ κ·Έλ£Ήμ ꡬλΆ
3. λͺ¨λ λ Έλ μν
μμ
(1, 2), (2, 3), (3, 4), (4, 2)
π μλ‘κ² μκ²λ λ΄μ©