Skip to content
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

Merged
merged 3 commits into from
Mar 3, 2024
Merged

34-pknujsp #135

merged 3 commits into from
Mar 3, 2024

Conversation

pknujsp
Copy link
Collaborator

@pknujsp pknujsp commented Feb 12, 2024

πŸ”— 문제 링크

이뢄 κ·Έλž˜ν”„

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

1μ‹œκ°„ 이상
접근을 μ™„μ „νžˆ 잘λͺ»ν•˜μ—¬ 많이 μ†Œμš”

✨ μˆ˜λ„ μ½”λ“œ

같은 집합 λ‚΄μ˜ λ…Έλ“œλΌλ¦¬ μ„œλ‘œ μ—°κ²°λ˜μ§€ μ•Šλ„λ‘ κ·Έλž˜ν”„μ˜ 정점듀을 두 μ§‘ν•©μœΌλ‘œ λ‚˜λˆŒ 수 μžˆλŠ”μ§€ νŒλ³„ν•˜λŠ” 문제

μœ λ‹ˆμ˜¨ νŒŒμΈλ“œλ‘œ 풀이λ₯Ό μ‹œμž‘ν–ˆλ‹€κ°€ μ•„λ‹Œ κ±Έ κΉ¨λ‹«κΈ° κΉŒμ§€ μ‹œκ°„μ„ 많이 μ“΄ λ¬Έμ œμž…λ‹ˆλ‹€
일뢀 μΌ€μ΄μŠ€λ§Œ 보고 κ·Έλž˜ν”„ 내에 사이클 μ—¬λΆ€κ°€ μ€‘μš”ν•˜λ‹€κ³  μƒκ°ν–ˆλŠ”λ° μ•„λ‹ˆμ—ˆμŠ΅λ‹ˆλ‹€

  • 이 λ¬Έμ œλŠ” 정점듀을 두 그룹으둜 λ‚˜λˆ„λŠ” 것이 λͺ©ν‘œμ΄κ³ 
  • 이 κ³Όμ •μ—μ„œ μΈμ ‘ν•œ 정점이 각각 λ‹€λ₯Έ 그룹에 속해야 ν•˜λŠ” 쑰건을 λ§Œμ‘±ν•΄μ•Ό ν•©λ‹ˆλ‹€
  • μœ λ‹ˆμ˜¨ νŒŒμΈλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 좔가적인 쑰건을 κ³ λ €ν•΄μ•Ό ν•˜λ©΄μ„œ
  • μΈμ ‘ν•œ 정점이 λ‹€λ₯Έ 그룹에 μ†ν•˜λŠ”μ§€ νŒλ‹¨ν•˜λŠ” 둜직이 λ³΅μž‘ν•΄μ§‘λ‹ˆλ‹€
  • λ˜ν•œ 사이클 νŒλ‹¨μ΄ 핡심 둜직이 μ•„λ‹™λ‹ˆλ‹€

1. 무방ν–₯ κ·Έλž˜ν”„λ₯Ό 생성

2. 정점을 κ·Έλ£Ήν™”ν•˜κΈ° μœ„ν•΄ 리슀트 생성

각 정점에 1 λ˜λŠ” -1의 값을 λΆ€μ—¬ν•˜μ—¬ 그룹을 ꡬ뢄

3. λͺ¨λ“  λ…Έλ“œ 순회

  • ν•΄λ‹Ή λ…Έλ“œμ˜ idκ°€ 0인 경우 μΈμ ‘ν•œ λ…Έλ“œλ“€μ— λŒ€ν•΄μ„œ BFS μˆ˜ν–‰
  • 탐색 κ³Όμ •μ—μ„œ μΈμ ‘ν•œ λ…Έλ“œμ˜ ID둜 직전 λ…Έλ“œμ˜ ID에 음수λ₯Ό μ·¨ν•œ κ°’μœΌλ‘œ μ„€μ •
  • 같은 κ·Έλ£Ή λ‚΄μ˜ λ…Έλ“œ κ°„μ—λŠ” μΈμ ‘ν•˜μ§€ μ•Šμ•„μ•Ό ν•˜λ―€λ‘œ, μ„œλ‘œ λ‹€λ₯Έ 값을 λΆ€μ—¬ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€

μ˜ˆμ‹œ

  • μ •μ μ˜ 개수 : 4
  • κ°„μ„ μ˜ 개수 : 4
  • κ°„μ„  : (1, 2), (2, 3), (3, 4), (4, 2)
단계 ν˜„μž¬ 정점 인접 정점 μƒνƒœ Β 
1 1 2 1: 1, 2: -1 1번 μ •μ μ—μ„œ μ‹œμž‘ν•˜μ—¬ 2번 정점을 -1둜 μ„€μ •
2 2 1, 3 1: 1, 2: -1, 3: 1 2번 μ •μ μ˜ 인접 정점 쀑 3번 정점을 1둜 μ„€μ •
3 3 2, 4 1: 1, 2: -1, 3: 1, 4: 1 3번 μ •μ μ˜ 인접 정점 쀑 4번 정점을 -1둜 μ„€μ •
4 4 2, 3 1: 1, 2: -1, 3: 1, 4: -1 4번 정점은 이미 idκ°€ λΆ€μ—¬λ˜μ–΄ μžˆμ§€ μ•ŠμŒ
def bfs(start):
    q = deque([start])
    groups[start] = 1

    while q:
        curr = q.popleft()
        visited[curr] = True

        for adj in graph[curr]:
            if visited[adj]:
                continue

            if not groups[adj]:
                groups[adj] = -groups[curr]
                q.append(adj)
            elif groups[adj] == groups[curr]:
                return False
    return True


for _ in range(int(stdin.readline())):
    V, E = map(int, stdin.readline().split())
    graph = [[] for i in range(V + 1)]

    for _ in range(E):
        a, b = map(int, stdin.readline().split())
        graph[a].append(b)
        graph[b].append(a)

    groups = [0] * (V + 1)
    visited = [False] * (V + 1)
    result = None

    for i in range(1, V + 1):
        if groups[i] == 0 and not bfs(i):
            result = 'NO'
            break

    print('YES' if not result else result)

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 15, 2024

image

흠..

Copy link
Member

@tgyuuAn tgyuuAn left a 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")

λ­”κ°€ λ‘œμ§μ€ λΉ„μŠ·ν•œλ° μ œκ°€ 더 μ§€μ €λΆ„ν•˜κ²Œ ν‘Ό 것 같은 λŠλ‚Œμ΄ λ“œλ„€μš” ....

μ½”λ“œ 해석해보고 μ˜€κ² μŠ΅λ‹ˆλ‹€..

Copy link
Member

@yuna83 yuna83 left a 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λ³΄λ©΄μ„œ λ¬Έμ œμ— μ–΄λ–»κ²Œ μ ‘κ·Όν•˜λŠ”μ§€ μ΅ν˜”μŠ΅λ‹ˆλ‹€
κ·Έλž˜ν”„ λ¬Έμ œλ„ λ‚œμ΄λ„λ³„λ‘œ ν’€μ–΄λ΄μ•Όκ² μ–΄μš”
쒋은 풀이 κ°μ‚¬ν•©λ‹ˆλ‹€!!

@pknujsp pknujsp merged commit 5b540db into main Mar 3, 2024
1 check passed
@pknujsp pknujsp deleted the 34-pknujsp branch March 3, 2024 07:36
@pknujsp pknujsp restored the 34-pknujsp branch March 3, 2024 07:38
@pknujsp pknujsp mentioned this pull request Mar 3, 2024
@tgyuuAn tgyuuAn deleted the 34-pknujsp branch May 10, 2024 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants