Skip to content

Conversation

@Fnhid
Copy link
Collaborator

@Fnhid Fnhid commented Jul 5, 2025

πŸ”— 문제 링크

μ—¬λŸ¬λΆ„μ˜ 닀리가 λ˜μ–΄ λ“œλ¦¬κ² μŠ΅λ‹ˆλ‹€!

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

30λΆ„

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

이번 μ°¨μ‹œλŠ” κ°€λ³κ²Œ ν’€ 수 μžˆλŠ” 문제λ₯Ό κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€.

image

N개의 정점과 N-2개의 κ°„μ„ μœΌλ‘œ 이루어진 κ·Έλž˜ν”„κ°€ μžˆμŠ΅λ‹ˆλ‹€.

이 κ·Έλž˜ν”„μ—μ„œ N-1개의 μ •μ λŠ” N-2개의 κ°„μ„ μœΌλ‘œ λͺ¨λ‘ 이어져 있고,

1개의 정점은 λ‹€λ₯Έ λͺ¨λ“  정점과 λΆ„λ¦¬λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€.

μž…λ ₯으둜 κ·Έλž˜ν”„μ˜ N-2개의 κ°„μ„  정보가 μ£Όμ–΄μ§€λŠ”λ°,

이 정보λ₯Ό ν† λŒ€λ‘œ λ…λ¦½λœ 정점λ₯Ό μ°Ύμ•„μ„œ λ‹€λ₯Έ 정점 쀑 ν•˜λ‚˜μ™€ μ—°κ²°ν•΄μ•Ό ν•©λ‹ˆλ‹€.

(μ—°κ²°ν•  두 λ…Έλ“œλ₯Ό 좜λ ₯ν•˜λ©΄ λ©λ‹ˆλ‹€. μŠ€νŽ˜μ…œ μ €μ§€ 문제라 μ—¬λŸ¬ 방법이 μžˆμ„ 경우 ν•œ λ°©λ²•λ§Œ 좜λ ₯해도 μ •λ‹΅ 처리 λ©λ‹ˆλ‹€.)

λ§Žμ€ λ°©λ²•μœΌλ‘œ ν•΄κ²°ν•  수 μžˆκ² μ§€λ§Œ, 1번 μ •μ μ—μ„œ dfsλ₯Ό μ‹œμž‘ν•˜μ—¬ λ°©λ¬Έν•œ 정점듀을 λͺ¨λ‘ μ²΄ν¬ν•˜μ—¬ μ£Όμ—ˆκ³ ,

λ°©λ¬Έν•˜μ§€ μ•Šμ€ 정점을 ν™•μΈν•˜μ—¬ 좜λ ₯ν•˜λŠ” λ°©μ‹μœΌλ‘œ 문제λ₯Ό ν•΄κ²°ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

image

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

// 깊이 μš°μ„  탐색 ν•¨μˆ˜
ν•¨μˆ˜ dfs(κ·Έλž˜ν”„ G, λ°©λ¬Έ_μ—¬λΆ€_λ°°μ—΄ visited):
    μŠ€νƒ s 생성
    s에 1을 μΆ”κ°€ (탐색 μ‹œμž‘ λ…Έλ“œ)
    visited[1] = μ°Έ

    sκ°€ λΉ„μ–΄μžˆμ§€ μ•Šμ€ λ™μ•ˆ 반볡:
        v = s의 맨 μœ„ μ›μ†Œ 확인
        sμ—μ„œ μ›μ†Œ 제거

        G[v]의 각 이웃 λ…Έλ“œ neigh에 λŒ€ν•΄ 반볡:
            λ§Œμ•½ visited[neigh]κ°€ 거짓이라면:
                visited[neigh] = μ°Έ
                s에 neighλ₯Ό μΆ”κ°€

// 메인 μ‹€ν–‰ 둜직
μ£Όμš” 둜직 μ‹œμž‘:
    μ •μˆ˜ n μž…λ ₯λ°›κΈ°

    크기 (n + 1)의 κ·Έλž˜ν”„ G 생성
    크기 (n + 1)의 λΆˆλ¦¬μ–Έ λ°°μ—΄ visitedλ₯Ό λͺ¨λ‘ κ±°μ§“μœΌλ‘œ μ΄ˆκΈ°ν™”

    // κ·Έλž˜ν”„ 정보 μž…λ ₯λ°›κΈ° (정점은 n-2개)
    i = 0λΆ€ν„° n-3κΉŒμ§€ 반볡:
        μ •μˆ˜ a, b μž…λ ₯λ°›κΈ°
        G[a]에 bλ₯Ό μΆ”κ°€
        G[b]에 aλ₯Ό μΆ”κ°€

    // 1번 λ…Έλ“œμ™€ μ—°κ²°λœ λͺ¨λ“  λ…Έλ“œλ₯Ό λ°©λ¬Έ 처리
    dfs(G, visited) 호좜

    // λ°©λ¬Έν•˜μ§€ μ•Šμ€ λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ˜ λ…Έλ“œ μ°ΎκΈ°
    고립된_λ…Έλ“œ = -1
    i = 2λΆ€ν„° nκΉŒμ§€ 반볡:
        λ§Œμ•½ visited[i]κ°€ 거짓이라면:
            고립된_λ…Έλ“œ = i
            반볡 쀑단

    // κ²°κ³Ό 좜λ ₯
    1κ³Ό 고립된_λ…Έλ“œλ₯Ό 좜λ ₯

끝

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

@froglike6
Copy link
Collaborator

μ €λŠ” 이 문제λ₯Ό Union Find둜 ν•΄κ²°ν•΄λ΄€μŠ΅λ‹ˆλ‹€. λ…Έλ“œλ“€μ„ λ‹€ λ¬Άμ—ˆμ„ λ•Œ, λ‹€ μ—°κ²°λœ λ…Έλ“œμ™€ κ·Έλ ‡μ§€ μ•Šμ€ λ…Έλ“œ 2개만 μžˆμœΌλ―€λ‘œ μ„œλ‘œμ†Œ 집합을 μ“°κΈ° νŽΈλ¦¬ν•˜λ‹€κ³  ν•˜λ„€μš”(μ°Έκ³ ν•œ κΈ€). λ§Œλ“€μ–΄μ§„ μ§‘ν•© 쀑 λΆ€λͺ¨κ°€ λ‹€λ₯Έ 집합을 μ—°κ²°μ‹œν‚€λŠ” λ°©μ‹μœΌλ‘œ 문제λ₯Ό ν•΄κ²°ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹€.

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

def find(x):
    while parent[x] != x:
        parent[x] = parent[parent[x]]
        x = parent[x]
    return x

def union(a, b):
    ra = find(a)
    rb = find(b)
    if ra != rb:
        parent[rb] = ra

N = int(input().strip())
parent = list(range(N + 1))

for _ in range(N - 2):
    u, v = map(int, input().split())
    union(u, v)

root = find(1)

for i in range(2, N + 1):
    if find(i) != root:
        print(1, i)
        break

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

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

μœ ν˜„λ‹˜μ˜ 풀이와 같은데, dfs κ΅¬ν˜„ λ°©μ‹λ§Œ λ‹€λ₯΄λ„€μš”.
μ €λŠ” μž¬κ·€λ₯Ό μ΄μš©ν•œ dfsλ₯Ό μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

μ—°κ²°λ˜μ§€ μ•Šμ€ 뢀뢄이 단 ν•œκ°œ μ‘΄μž¬ν•˜λŠ” κ²ƒμ΄λ―€λ‘œ, 1λΆ€ν„° dfsλ₯Ό μ‚¬μš©ν•˜μ—¬ λ°©λ¬Έ 처리λ₯Ό ν•΄μ£Όκ³ ,
이후 λ°©λ¬Έ μ²˜λ¦¬κ°€ λ˜μ§€ μ•Šμ€ λ…Έλ“œ μ•„λ¬΄κ±°λ‚˜ 좜λ ₯ν•˜λ„λ‘ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

#include <iostream>
#include <vector>
using namespace std;

const int MAX = 300001;

int N;
vector<int> graph[MAX];
bool visited[MAX];

void dfs(int current) {
    visited[current] = true;
    for (int next : graph[current]) {
        if (!visited[next]) {
            dfs(next);
        }
    }
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    cin >> N;
    for (int i = 0; i < N - 2; i++) {
        int u, v;
        cin >> u >> v;
        graph[u].emplace_back(v);
        graph[v].emplace_back(u);
    }

    dfs(1);

    int answer = -1;
    for(int i = 1; i <= N; i++)
        if(!visited[i]) answer = i;

    cout << 1 << ' ' << answer << '\n';
    return 0;
}

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

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

저도 μœ ν˜„λ‹˜κ³Ό λΉ„μŠ·ν•œ λ°©μ‹μœΌλ‘œ ν’€μ–΄λ΄€μŠ΅λ‹ˆλ‹€.! κ°€λ³κ²Œ ν’€ 수 μžˆλŠ” μ‹€λ ₯에 λ‹€κ°€μ„œλŠ” 것 κ°™κ΅°μš”ν˜Έ

μ €λŠ” bfs둜 νƒμƒ‰ν•΄λ³΄μ•˜μŠ΅λ‹ˆλ‹Ή

인접 리슀트λ₯Ό νƒμƒ‰ν•˜λ €λ‹ˆ 잘 기얡이 μ•ˆ λ‚˜μ„œ 쑰금 μ˜€λž˜κ±Έλ¦¬μ—ˆμˆ©λ‹ˆλ‹€

즐거운 μ•„μΉ¨

from collections import deque
import sys
input = sys.stdin.readline

n = int(input())
land = [[] for _ in range(n+1)]
for _ in range(n-2):
    u, v = map(int, input().split())
    land[u].append(v)
    land[v].append(u)

visited = [False]* (n+1)

def bfs(start):
    q = deque()
    q.append(start)
    visited[start] = True

    while q:
        ddang = q.popleft()
        for neighbor in land[ddang]:
            if not visited[neighbor]:
                visited[neighbor] = True
                q.append(neighbor)
for i in range(1, n+1):
    if not visited[i]:
        bfs(i)
        break
temp = 0
for i in range(1, n+1):
    if visited[i] == False:
        temp = i
        break
if temp != 1:
    print('1', temp)
else:
    print(temp, '2')

@Fnhid Fnhid merged commit 366e194 into main Jul 11, 2025
1 check passed
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.

5 participants