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

72-tgyuuAn #238

Merged
merged 2 commits into from
Sep 2, 2024
Merged

72-tgyuuAn #238

merged 2 commits into from
Sep 2, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Aug 25, 2024

πŸ”— 문제 링크

λ“±μ‚° λ§ˆλ‹ˆμ•„

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

2μ‹œκ°„?

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

image




문제 κ°œμš”

총 N개의 μ˜€λ‘λ§‰μ΄ μžˆμ„λ•Œ, N개의 μ˜€λ‘λ§‰μ„ μ„œλ‘œμ„œλ‘œ λ°©λ¬Έν•œλ‹€κ³  ν•  λ•Œ λ“€λ¦¬λŠ” μ˜€μ†”κΈΈμ˜ 개수

ex) 3개의 μ˜€λ‘λ§‰μ΄ μžˆμ„κ²½μš°, 1 -> 2, 1 -> 3, 2 -> 3 μœΌλ‘œκ°€λŠ” 길의 개수λ₯Ό κ΅¬ν•˜λŠ”λ°,

이 λ•Œ λ°˜λ“œμ‹œ μ‚° 정상 (1번 μ˜€λ‘λ§‰) 을 μ§€λ‚˜μ•Ό ν•˜λ©°,

1 -> 2μ—μ„œ 갈 λ•Œ μ€‘λ³΅λ˜λŠ” 길은 μ„Έμ•Œλ¦¬μ§€ μ•ŠμŒ.

근데, 1 -> 2μ—μ„œ 갈 λ•Œ κ°”λ˜ κΈΈμ΄μ§€λ§Œ 1 -> 3μ—μ„œ 갈 λ•ŒλŠ” 세도 λœλ‹€.

이 λ•Œ, N의 μ΅œλŒ€ κ°œμˆ˜λŠ” 30만개 μ΄λ―€λ‘œ,

μ˜€λ‘λ§‰ i, jλ₯Ό κ³ λ₯΄λŠ” λͺ¨λ“  경우의 μˆ˜λŠ” 30만 * 29만 99999999 λ₯Ό ν•˜λ©΄ μ‹œκ°„μ˜€λ°”κ°€ λ‚˜κΈ° λ•Œλ¬Έμ— λ‹€λ₯Έ 방법을 선택해야 ν•œλ‹€.




ν•œ 참을 μƒκ°ν•˜λ‹€κ°€ λ– μ˜€λ₯Έ 것이 λ°”λ‘œ λ…Έλ“œ(μ˜€λ‘λ§‰)에 μ§‘μ€‘ν•˜λŠ” 것이 μ•„λ‹Œ, κ°„μ„ (μ˜€μ†”κΈΈ)에 μ§‘μ€‘ν•˜λŠ” 것.




사싀 이 κΉŒμ§€λŠ” λ– μ˜¬λ ΈλŠ”λ°, κ·Έ ν›„λ‘œ '간선을 가지고 ν‘ΈλŠ” 것은 λΆ„λͺ…ν•œλ°, μ–΄λ–€ κ·œμΉ™μ΄ μžˆμ§€?'

λ₯Ό 1μ‹œκ°„ λ‚΄λ‚΄ κ³ λ―Όν•˜λ‹€κ°€ κ²°κ΅­ 레퍼런슀λ₯Ό 보고 λ§μ•˜λ‹€.




image

μœ„ λ ˆνΌλŸ°μŠ€μ— μ„€λͺ…이 μ•„μ£Ό 잘 λ‚˜μ™€μžˆλŠ”λ°,

핡심은 λ°”λ‘œ μœ„ κ·Έλ¦Όμ—μ„œ λΉ¨κ°„ 간선을 μ§€λ‚˜κ°€λŠ” 경둜의 개수λ₯Ό κ΅¬ν•˜μžμ΄λ‹€.

이것은 κ°„μ„ μ˜ μ•„λž˜μͺ½κ³Ό κ·Έ λ‚˜λ¨Έμ§€μ˜ μ •μ μ˜ 개수둜 ꡬ할 수 μžˆλŠ”λ°,

μ•„λž˜ 3가지 경우의 μˆ˜κ°€ μžˆλ‹€.


1. κ°„μ„ μ˜ μ•„λž˜μͺ½
5, 8, 9 μ •μ λΌλ¦¬λŠ” 각자 μ„œλ‘œ λ‹€λ₯Έ μ–΄λ–€ 것을 골라도 무쑰건 λΉ¨κ°„ 간선을 μ§€λ‚˜κ°€κ²Œ λœλ‹€.


2. κ·Έ μ™Έ
5, 8, 9λ₯Ό μ œμ™Έν•œ μ •μ λΌλ¦¬λŠ” μ„œλ‘œ λ‹€λ₯Έ μ–΄λ–€ 것을 골라도 λΉ¨κ°„ 간선을 μ§€λ‚˜κ°€μ§€ μ•ŠλŠ”λ‹€.


3. κ°„μ„ μ˜ μ•„λž˜μͺ½ + κ·Έ μ™Έ
κ°„μ„ μ˜ μ•„λž˜μͺ½ 정점 3개 쀑 ν•˜λ‚˜μ™€, κ·Έ μ™Έ 정점듀 쀑 ν•˜λ‚˜λ₯Ό κ³ λ₯΄λ©΄ 무쑰건 λΉ¨κ°„ 간선을 μ§€λ‚˜κ°€κ²Œ λœλ‹€.

이 λ•Œ, μš°λ¦¬λŠ” 각 간선에 λŒ€ν•΄μ„œ 1번과 3λ²ˆμ„ 더해주면 λœλ‹€.







1λ²ˆμ€ κ°„μ„  μ•„λž˜μ˜ λ…Έλ“œ κ°―μˆ˜λ“€ 쀑에 2개λ₯Ό κ³ λ₯΄λ©΄ 되고,

3λ²ˆμ€ κ°„μ„  μ•„λž˜ λ…Έλ“œ 갯수 * (λͺ¨λ“  λ…Έλ“œ 갯수 - κ°„μ„  μ•„λž˜ λ…Έλ“œ 갯수)λ₯Ό ν•΄μ£Όλ©΄ λœλ‹€.







정말... λ– μ˜¬λ¦¬κΈ° 쉽지 μ•Šμ€ 문제..

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

@tgyuuAn tgyuuAn added tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. μž‘μ„± 쀑 ⏱️ labels Aug 25, 2024
@tgyuuAn tgyuuAn requested review from alstjr7437 and H0ngJu August 25, 2024 05:49
@tgyuuAn tgyuuAn self-assigned this Aug 25, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review August 25, 2024 06:02
@alstjr7437 alstjr7437 requested review from H0ngJu and removed request for H0ngJu and alstjr7437 September 1, 2024 05:47
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

정점이 2개인데, N이 300000이라 dp? 이뢄탐색? 인가 ν•˜κ³  λ“€μ–΄κ°”λŠ”λ°
κ°„μ„ μœΌλ‘œ μœ„, μ•„λž˜ μ„œλΈŒνŠΈλ¦¬λ‘œ κ΅¬λΆ„ν•΄μ„œ μ ‘κ·Όν•΄μ•Ό ν•˜λŠ”κ΅°μš” .. πŸ˜΅β€πŸ’«

레퍼런슀 μ°Έμ‘°ν•΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€!
근데 μ €λŠ” 61점이 λ‚˜μ˜€λ„€μš” ..? νƒœκ·œλ‹˜ 100점 λ°›μœΌμ…¨λ‚˜μ˜ˆ

νƒœκ·œλ‹˜ μ½”λ“œλ„ λ‹€μ‹œ ν™•μΈν–ˆλŠ”λ° 거의 λ˜‘κ°™μ€λ° 뭐가 λ¬Έμ œμΌκΉŒμš”.?.?.?.?.??

import sys

sys.setrecursionlimit(10 ** 6)

def dfs(start, info, visited):
    cnt = 1
    global total
    visited[start] = True

    for v in info[start]:
        if not visited[v]:
            sub  = dfs(v, info, visited) # μ„œλΈŒνŠΈλ¦¬ λ…Έλ“œ 수
            total += sub * (sub - 1) // 2 # μ„œλΈŒνŠΈλ¦¬ λ‚΄
            total += sub * (N-sub) # μ„œλΈŒνŠΈλ¦¬ + μ™Έ
            cnt += sub

    return cnt

N = int(input())
info = [[] for _ in range(N+1)]
visited = [False for _ in range(N+1)]
total = 0

for i in range(N-1):
    a, b = map(int, input().split())
    info[a].append(b)
    info[b].append(a)

dfs(1, info, visited)
print(total)

image

이게 μ–΄λ–»κ²Œ μ΄ˆλ“±λ¬Έμ œ

@tgyuuAn
Copy link
Member Author

tgyuuAn commented Sep 2, 2024

정점이 2개인데, N이 300000이라 dp? 이뢄탐색? 인가 ν•˜κ³  λ“€μ–΄κ°”λŠ”λ° κ°„μ„ μœΌλ‘œ μœ„, μ•„λž˜ μ„œλΈŒνŠΈλ¦¬λ‘œ κ΅¬λΆ„ν•΄μ„œ μ ‘κ·Όν•΄μ•Ό ν•˜λŠ”κ΅°μš” .. πŸ˜΅β€πŸ’«

레퍼런슀 μ°Έμ‘°ν•΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€! 근데 μ €λŠ” 61점이 λ‚˜μ˜€λ„€μš” ..? νƒœκ·œλ‹˜ 100점 λ°›μœΌμ…¨λ‚˜μ˜ˆ

νƒœκ·œλ‹˜ μ½”λ“œλ„ λ‹€μ‹œ ν™•μΈν–ˆλŠ”λ° 거의 λ˜‘κ°™μ€λ° 뭐가 λ¬Έμ œμΌκΉŒμš”.?.?.?.?.??

import sys

sys.setrecursionlimit(10 ** 6)

def dfs(start, info, visited):
    cnt = 1
    global total
    visited[start] = True

    for v in info[start]:
        if not visited[v]:
            sub  = dfs(v, info, visited) # μ„œλΈŒνŠΈλ¦¬ λ…Έλ“œ 수
            total += sub * (sub - 1) // 2 # μ„œλΈŒνŠΈλ¦¬ λ‚΄
            total += sub * (N-sub) # μ„œλΈŒνŠΈλ¦¬ + μ™Έ
            cnt += sub

    return cnt

N = int(input())
info = [[] for _ in range(N+1)]
visited = [False for _ in range(N+1)]
total = 0

for i in range(N-1):
    a, b = map(int, input().split())
    info[a].append(b)
    info[b].append(a)

dfs(1, info, visited)
print(total)

image

이게 μ–΄λ–»κ²Œ μ΄ˆλ“±λ¬Έμ œ

μž¬κ·€ 횟수λ₯Ό 쑰금 λŠ˜λ €λ³΄μ‹€λž˜μœ  ??

sys.setrecursionlimit

@tgyuuAn tgyuuAn merged commit 39a3ee2 into main Sep 2, 2024
@tgyuuAn tgyuuAn deleted the 72-tgyuuAn branch September 2, 2024 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ν•œ μ€„λ‘œλŠ” μ†Œκ°œν•  수 μ—†λŠ” λ‚¨μž. 리뷰 μ™„λ£Œ βœ”οΈ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants