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

11725: 트리의 부모 찾기 #21

Open
minsoo0715 opened this issue Sep 6, 2023 · 0 comments
Open

11725: 트리의 부모 찾기 #21

minsoo0715 opened this issue Sep 6, 2023 · 0 comments
Assignees
Labels
#그래프 그래프 관련 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들

Comments

@minsoo0715
Copy link
Owner

minsoo0715 commented Sep 6, 2023

11725: 트리의 부모 찾기

소스 코드

아이디어

하위 노드들이 루트 노드 밑에 어떻게 생길지 모르기 때문에, 루트 노드로부터 노드 탐색이 필요하다.
각 노드들의 연결된 노드를 배열에 기록해놓고, 방문한 노드를 제외하고 탐색을 한다.
이때 방문하기전에 현재 노드를 정답으로 기록한다.

구현

연결된 노드가 기록된 배열을 순회하고, 방문하지 않은 노드라면 하위 노드에 대한 부모 노드를 저장 후, 재귀 호출을 통해 모든 노드를 방문한다.

void solve(int num = 1) {
    visited[num] = true;

    for(int i : v[num]) {
        if(!visited[i]) {
            ans[i] = num;
            solve(i);
        }
    }
}
@minsoo0715 minsoo0715 added @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들 #그래프 그래프 관련 문제 labels Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#그래프 그래프 관련 문제 @백준 백준(https://www.acmicpc.net) 문제 C/C++ c/c++로 해결한 문제들
Projects
None yet
Development

No branches or pull requests

1 participant