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

9-fnzksxl #37

Merged
merged 1 commit into from
Mar 7, 2024
Merged

9-fnzksxl #37

merged 1 commit into from
Mar 7, 2024

Conversation

fnzksxl
Copy link
Member

@fnzksxl fnzksxl commented Feb 23, 2024

πŸ”— 문제 링크

μˆœμœ„

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

μ•½ 1μ‹œκ°„.?

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

μ²˜μŒμ— 문제 쀑

ν•˜μ§€λ§Œ λͺ‡λͺ‡ κ²½κΈ° κ²°κ³Όλ₯Ό λΆ„μ‹€ν•˜μ—¬ μ •ν™•ν•˜κ²Œ μˆœμœ„λ₯Ό 맀길 수 μ—†μŠ΅λ‹ˆλ‹€.
이 뢀뢄을 λ„˜κ²¨ μ§šμ–΄ μ½μ–΄μ„œ(사싀상 μ•ˆ 읽고 λ„˜μ–΄κ°„)
μ œκ°€ 생각해내지 λͺ»ν•œ κ·Έλ¦¬λ””ν•œ λ°©λ²•μ΄λ‚˜ μˆ˜ν•™μ  지식이 μžˆλŠ” 쀄 μ•Œμ•˜μŠ΅λ‹ˆλ‹€.. ν•˜μ•„

μ²˜μŒμ— 아이디어가 잘 μ•ˆ λ– μ˜¬λΌμ„œ ν•œ 번 κ·Έλƒ₯ 그림판으둜 흐름을 λ¬΄μž‘μ • λ”°λΌκ°€λ΄€μŠ΅λ‹ˆλ‹€. λ§ˆμš°μŠ€λΌμ„œ 글씨가 λ”λŸ½κ΅°μš”

image

μ•„ν•˜! κΉ¨λ‹¬μ•˜μŠ΅λ‹ˆλ‹€. 이 녀석.. λ‚˜λ¨Έμ§€ λͺ¨λ“  λ…Έλ“œλ“€κ³Ό μ—°κ²°λ˜μ–΄ μžˆλ‹€λ©΄ μˆœμœ„λ₯Ό μ•Œ 수 μžˆκ΅°μš”?

이어진 간선듀을 유ν–₯간선이라 κ°„μ£Όν–ˆμ„ λ•Œ 이어진 λ…Έλ“œ
이 간선듀을 λ’€μ§‘μ—ˆμ„ λ•Œ 이어진 λ…Έλ“œ

μœ„ 두 λ…Έλ“œλ“€μ˜ 합집합이 본인을 μ œμ™Έν•œ λ…Έλ“œλ“€μ΄λΌλ©΄(사싀상 n-1개의 λ…Έλ“œλΌλ©΄) μˆœμœ„λ₯Ό μ•Œ 수 μžˆλŠ” λ…Έλ“œμž…λ‹ˆλ‹€.

μ—¬κΈ°κΉŒμ§€ μ•Œμ•„λ‚΄κ³  μ•„, ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ μ•Œκ³ λ¦¬μ¦˜μ„ μ“°λŠ” λ¬Έμ œμΈκ°€?κΉŒμ§€ μœ μΆ”ν•΄λƒˆλŠ”λ°..
μ˜€λžœλ§Œμ— λ“€μ–΄λ³΄λŠ” 이름듀이라 μ–΄λ–»κ²Œ κ΅¬ν˜„ν•˜λŠ”μ§€ κΉŒλ¨Ήμ–΄λ²„λ Έμ§€ λ­μ—μš”.
κ·Έλž˜μ„œ 제 λ§ˆμŒλŒ€λ‘œ 일단 κ΅¬ν˜„ν•΄λ΄€μŠ΅λ‹ˆλ‹€. μ½”λ“œκ°€ λ”λŸ¬μ›Œλ³΄μ΄λŠ” μ΄μœ λ„ λ‹€ 그것 λ•Œλ¬Έμ΄μ—μš”.

κ°„λ‹¨ν•˜κ²Œ μ œκ°€ κ΅¬ν˜„ν•œ 방식을 μ„€λͺ…ν•΄λ³΄κ² μŠ΅λ‹ˆλ‹€.

  1. 데이터λ₯Ό ν† λŒ€λ‘œ κ·Έλž˜ν”„λ₯Ό λ§Œλ“ λ‹€.
  2. 간선을 λ’€μ§‘μ–΄μ„œ κ·Έλž˜ν”„λ₯Ό ν•˜λ‚˜ 더 λ§Œλ“ λ‹€.
  3. μ²˜μŒλΆ€ν„° λκΉŒμ§€ λ…Έλ“œλ₯Ό μˆœνšŒν•˜λ©΄μ„œ
  • 뒀집지 μ•Šμ€ κ·Έλž˜ν”„μ—μ„œ λ‚΄κ°€ 갈 수 μžˆλŠ” λ…Έλ“œλ₯Ό κΈ°μ–΅
  • 뒀집은 κ·Έλž˜ν”„μ—μ„œ λ‚΄κ°€ 갈 수 μžˆλŠ” λ…Έλ“œλ₯Ό κΈ°μ–΅
  1. κΈ°μ–΅ν•˜κ³  μžˆλŠ” λ…Έλ“œμ˜ ν•© == n-1 이라면 개수 +1
from collections import deque

def func(n, graph, degree):
    for i in range(1, n+1):
        q = deque()
        q.append(i)
        
        while q:
            node = q.popleft()
            
            for next_node in graph[node]:
                _before = len(degree[i])
                degree[i].add(next_node)
                
                if _before == len(degree[i]):
                    continue
                
                q.append(next_node)
                
def solution(n, results):
    answer = 0
    graph = [[] for _ in range(n+1)]
    reversed_graph = [[] for _ in range(n+1)]
    in_degree = [set() for _ in range(n+1)]
    out_degree = [set() for _ in range(n+1)]
    
    for win, defeat in results:
        graph[win].append(defeat)
        reversed_graph[defeat].append(win)
    
    func(n, graph, out_degree)
    func(n, reversed_graph, in_degree)
    
    
    for i in range(1, n+1):
        if len(out_degree[i])+len(in_degree[i]) == n-1:
            answer += 1
    
    return answer

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

문제λ₯Ό 꼼꼼히 읽자.

@9kyo-hwang
Copy link

문제 μ„€λͺ…λ§Œ 보면 λ„μ €νžˆ κ·Έλž˜ν”„ 문제둜 μ•ˆλ³΄μ΄λŠ”λ°, μ•Œκ³ λ³΄λ‹ˆ ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ λ¬Έμ œλž€ 말이죠... 저도 이거 μ²˜μŒν’€ λ•Œ κ·Έλž˜ν”„ μΉ΄ν…Œκ³ λ¦¬λΌκ³  ν‘œκΈ° μ•ˆν•΄μ€¬μœΌλ©΄ κ°ˆν”Όλ„ λͺ»μž‘μ•˜μ„ 것 κ°™μ•„μš”.

μ „ 정석적인 ν”Œλ‘œμ΄λ“œ-μ›Œμ…œ ν’€μ΄λ‘œ μ ‘κ·Όν–ˆμŠ΅λ‹ˆλ‹€. λŒ€μ‹  μ—¬κΈ°μ„œλŠ” μ΅œλ‹¨ 거리λ₯Ό κ΅¬ν•˜λŠ” 것이 μ•„λ‹Œ, "οΏ½iκ°€ kλ₯Ό 이겼고 kκ°€ jλ₯Ό 이겼으면 iλŠ” jλ₯Ό 이긴 κ±°λ‹€!"와 같이 관계 λ₯Ό ν™•μž₯ν•΄κ°€λŠ” κ°œλ…μ΄λ‹ˆ 이 λΆ€λΆ„λ§Œ 살짝 λ‹€λ₯΄κ² λ„€μš”.

def solution(n, results):
    graph = [[float('inf')] * (n + 1) for _ in range(n + 1)]
    for i in range(1, n + 1):
        graph[i][i] = 0
        
    for A, B in results:
        graph[A][B] = 1  # AλŠ” Bλ₯Ό μ΄κ²ΌμœΌλ―€λ‘œ 1
        graph[B][A] = -1  # BλŠ” Aμ—κ²Œ μ‘ŒμœΌλ―€λ‘œ -1
        
    for k in range(1, n + 1):
        for i in range(1, n + 1):
            for j in range(1, n + 1):
                if graph[i][k] == 1 and graph[k][j] == 1:  # iκ°€ kλ₯Ό 이겼고, kκ°€ jλ₯Ό 이겼닀면
                    graph[i][j] = 1  # iλŠ” jλ₯Ό 이긴거닀.
                elif graph[i][k] == -1 and graph[k][j] == -1:  # λ°˜λŒ€ 경우
                    graph[i][j] = -1
                    
    answer = 0
    for i in range(1, n + 1):  # 맀 i번째 μ‚¬λžŒλ§ˆλ‹€
        if float('inf') not in graph[i][1:]:  # 관계λ₯Ό μ•Œ 수 μ—†λŠ”(inf) κ²½μš°κ°€ μ—†λ‹€λ©΄
            answer += 1  # 카운트 + 1
    return answer

ν—ˆν—ˆ dictionaryλ₯Ό μ¨μ„œ 훨씬 κ°„λ‹¨ν•˜κ²Œ ν‘Ό μ‚¬λžŒμ΄ μžˆλ„€μš”... updateλž€ ν•¨μˆ˜κ°€ μžˆμ–΄μ„œ μ–˜κ°€ λ‹€ ν•΄μ£Όλ„€...

from collections import defaultdict

def solution(n, results):
    win, lose = defaultdict(set), defaultdict(set)
    for A, B in results:
        win[A].add(B)
        lose[B].add(A)

    for i in range(1, n + 1):
        for winner in lose[i]:  # i번 μ„ μˆ˜μ—κ²Œ 이긴 μ„ μˆ˜λ“€
            win[winner].update(win[i])
        for loser in win[i]:  # i번 μ„ μˆ˜μ—κ²Œ 진 μ„ μˆ˜λ“€
            lose[loser].update(lose[i])

    answer = 0
    for i in range(1, n + 1):
        if len(win[i]) + len(lose[i]) == n - 1:
            answer += 1
        
    return answer

@fnzksxl
Copy link
Member Author

fnzksxl commented Feb 23, 2024

@9kyo-hwang μ €μ²˜λŸΌ set을 μ¨μ„œ ν‘Έμ‹  λΆ„μ΄λ„€μš© defaultdict둜 set μžλ£Œν˜•μ„ 빈 μƒνƒœλ‘œ μ΄ˆκΈ°ν™”ν•΄μ„œ μ“°μ‹  풀이 κ°™μŠ΅λ‹ˆλ‹€. 저도 update둜 ν•œ λ²ˆμ— μ—†μ• μ€„κΉŒλΌκ³  생각은 ν•΄λ΄€λŠ”λ° μ € 풀이 되게 κΉ”λ”ν•˜κ΅°μš”.. 그런데 μ‘Œμ„ λ•Œλ₯Ό -1둜 λ‹€λ₯΄κ²Œ ν‘œκΈ°ν•˜μ‹  μ΄μœ κ°€ μžˆμ„κΉŒμš”?

@9kyo-hwang
Copy link

@9kyo-hwang μ €μ²˜λŸΌ set을 μ¨μ„œ ν‘Έμ‹  λΆ„μ΄λ„€μš© defaultdict둜 set μžλ£Œν˜•μ„ 빈 μƒνƒœλ‘œ μ΄ˆκΈ°ν™”ν•΄μ„œ μ“°μ‹  풀이 κ°™μŠ΅λ‹ˆλ‹€. 저도 update둜 ν•œ λ²ˆμ— μ—†μ• μ€„κΉŒλΌκ³  생각은 ν•΄λ΄€λŠ”λ° μ € 풀이 되게 κΉ”λ”ν•˜κ΅°μš”.. 그런데 μ‘Œμ„ λ•Œλ₯Ό -1둜 λ‹€λ₯΄κ²Œ ν‘œκΈ°ν•˜μ‹  μ΄μœ κ°€ μžˆμ„κΉŒμš”?

γ…“ 이긴 κ±°λž‘ 진 κ±°λ₯Ό ꡬ뢄할렀고 ν•œ 건데, μƒκ°ν•΄λ³΄λ‹ˆ 그럴 ν•„μš”κ°€ μ—†μ—ˆκ΅°μš”?
관계가 μžˆλ‹€λŠ” 사싀 μžμ²΄κ°€ μ€‘μš”ν•œ κ±°μ˜€λ„€μš”...

@fnzksxl
Copy link
Member Author

fnzksxl commented Feb 24, 2024

@9kyo-hwang κ΅ν™©λ‹˜μ΄λΌ μ–΄λ–€ 큰 뜻이 μˆ¨μ–΄μžˆλŠ” 쀄 μ•Œμ•˜μŠ΅λ‹ˆλ‹€ γ…Žγ…‹γ…‹γ…‹γ…‹γ…‹

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν”Œλ‘œμ΄λ“œ μ›Œμ…œ 기얡이 μ•ˆλ‚˜μ„œ ꡐ황이꺼 보고 ν’€μ—ˆλ„€μš”..γ…‹γ…‹
μƒκΈ°ν•΄μ„œ μ’‹μ•˜μŠ΅λ‹ˆλ‹€ κ΅Ώ..
이기고 이겼을 λ•Œ μ—…λŽƒ, 지고 μ‘Œμ„ λ•Œ μ—…λŽƒ 이걸 생각 λͺ»ν–ˆλ„€μš”

const val INFF = 100_000_000

class Solution {
    fun solution(n: Int, results: Array<IntArray>): Int {
        val graph = Array(n + 1) { i ->
            Array(n + 1) { j -> if (i == j) 0 else INFF }
        }

        results.forEach {
            graph[it[0]][it[1]] = 1
            graph[it[1]][it[0]] = -1
        }

        for (k in 1..n) {
            for (a in 1..n) {
                for (b in 1..n) {
                    if (graph[a][k] == 1 && graph[k][b] == 1) {
                        graph[a][b] = 1
                    } else if (graph[a][k] == -1 && graph[k][b] == -1) {
                        graph[a][b] = -1
                    }
                }
            }
        }

        val answer = mutableListOf<Int>()
        for (i in 1..n) {
            var isNotINFF = true
            for (j in 1..n) {
                if (graph[i][j] != INFF) {
                    continue
                }
                isNotINFF = false
                break
            }
            if (isNotINFF) {
                answer.add(i)
            }
        }
        return answer.count()
    }
}

@tgyuuAn
Copy link
Member

tgyuuAn commented Feb 25, 2024

AlgoLeadMe/AlgoLeadMe-1#88

이 λ¬Έμ œμ— λŒ€ν•΄μ„œ PR이 이전에 μ˜¬λΌμ™”μ—ˆλŠ”λ°,

μ €λŠ” ν”Œλ‘œμ΄λ“œ μ›Œμ…œμΈμ§€λ„ λͺ°λžμ–΄μš”..

μ €λŠ” μ•½κ°„ λ‹€λ₯΄κ²Œ ν’€μ–΄μ„œ 풀이 κ³΅μœ ν•΄λ΄…λ‹ˆλ‹€!!

@alstjr7437
Copy link
Member

alstjr7437 commented Feb 26, 2024

μ €λŠ” λ”°λ‘œ 이기고 μ§€λŠ” 관계λ₯Ό μƒκ°ν•΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€!!

  1. 이기고 진애듀 κ·Έλž˜ν”„λ₯Ό λ”°λ‘œ λ§Œλ“€μ–΄μ„œ μ €μž₯ν•΄λ‘”λ‹€.
  2. i(2) ν•œν…Œ 진애듀(5) -> i(2)λ₯Ό 이긴 μ• λ“€(1,3,4)μ—κ²Œλ„ 짐
  3. i(2) ν•œν…Œ 이긴애듀(1,3,4) -> i(2)ν•œν…Œ 진 μ• λ“€(5)μ—κ²Œλ„ 이김

μœ„μ™€ 같이 κ³„μ‚°ν•΄μ„œ 쀑볡을 μ œκ±°ν•˜κ³  두 κ·Έλž˜ν”„μ˜ 합이 n-1인만큼 λΆ€λΆ„κΉŒμ§€ μƒκ°ν–ˆλŠ”λ° κ΅¬ν˜„μ—μ„œ λ§‰ν˜€μ„œ 인터넷을 μ°Έκ³ ν–ˆμŠ΅λ‹ˆλ‹€ γ… 

μ κ³ λ³΄λ‹ˆ κ΅ν™©λ‹˜μ΄ μž‘μ„±ν•˜μ‹  λΆ€λΆ„μ΄λ„€μš”

def solution(n, results):
    answer = 0
    win_graph = {}    # 이긴 μ• λ“€
    lose_graph = {}   # 진 μ• λ“€
    
    for i in range(1, n+1):
        win_graph[i] = set()
        lose_graph[i] = set()

    for winner, loser in results:   # κ·Έλž˜ν”„ λ§Œλ“€κΈ°
        if winner not in win_graph:
            win_graph[loser] = set()
        if loser not in lose_graph:
            lose_graph[winner] = set()
        win_graph[loser].add(winner)
        lose_graph[winner].add(loser)

    for i in range(1, n+1):         
        for winner in win_graph[i]:  # iν•œν…Œ 진 μ• λ“€ -> iλ₯Ό 이긴 μ• λ“€ν•œν…Œλ„ 짐
            if i in lose_graph:
                lose_graph[winner].update(lose_graph[i])
        for loser in lose_graph[i]:  # iν•œν…Œ 이긴 μ• λ“€ -> iν•œν…Œ 진 μ• λ“€ν•œν…Œλ„ 이김
            if i in win_graph:
                win_graph[loser].update(win_graph[i])
    
    for i in range(1, n+1):
        if len(win_graph[i]) + len(lose_graph[i]) == n-1:  # iν•œν…Œ 이기고 진 μ• λ“€ ν•©μ³μ„œ n-1이면 μˆœμœ„κ°€ κ²°μ •λœ 것
            answer += 1

    return answer

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def bfs (_dict, player,n) :

    queue = [player]
    checker = [0 for _ in range(0,n+1)]
    count_node = 0
    while queue :
        current_player = queue.pop(0)
        
        for checking_player in _dict[current_player] :
            
            if checker[checking_player] == 0 :
                checker[checking_player] = 1
                queue.extend(_dict[current_player])
                count_node += 1
                
    return count_node

def solution(n, results):
    answer = 0
    
    win_dict = {index+1 : [] for index in range(len(results))}
    lose_dict = {index+1 : [] for index in range(len(results))}
    
    for result in results :
        win, lose = result[0], result[1]
        win_dict[win].append(lose)
        lose_dict[lose].append(win)
    
    for player in range(1,n+1) :
        if bfs(win_dict, player,n) + bfs(lose_dict, player,n) == n-1 :
            answer +=1
    print(answer)
    return answer

μ΄λ ‡κ²Œ ν–ˆλŠ”λ°

image

μ΄λ ‡κ²Œ λ– μ„œ ν™”κ°€ λ‚˜λ„€μš” 뭐가 ν‹€λ Έμ„κΉŒμš”?

@fnzksxl fnzksxl merged commit e855279 into main Mar 7, 2024
3 checks passed
@fnzksxl fnzksxl deleted the 9-fnzksxl branch March 7, 2024 06:21
@tgyuuAn
Copy link
Member

tgyuuAn commented Mar 7, 2024

@wkdghdwns199

μž¬κ·€ ν•¨μˆ˜κ°€ λ„ˆλ¬΄ κΉŠμ–΄μ Έμ„œ 그런 것 같은데,

μ½”λ“œ 맨 μœ„μ—

import sys

sys.setrecursionlimit(1000000)

λ„£μ–΄λ³΄μ‹œκ² μ–΄μš”?


μ œκ°€ 직접 ν•΄λ³΄λ‹ˆκΉŒ 아직 κ·Έλž˜λ„ μ—λŸ¬κ°€ λ‚˜λ„€μš”..

인덱슀 κ΄€λ ¨ μ—λŸ¬μΈκ°€...

@wkdghdwns199
Copy link
Collaborator

wkdghdwns199 commented Mar 13, 2024

@tgyuuAn μ•„ μ•„μ‰½λ„€μš”.. γ…œγ…œ ν•œ 번 λ‹€μ‹œ ν•΄λ΄μ•Όκ² μŠ΄λ‹€

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.

6 participants