File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://www.acmicpc.net/problem/3584
2+
3+ #include < algorithm>
4+ #include < iostream>
5+ using namespace std ;
6+
7+ int parent[10001 ];
8+ bool visited[10001 ];
9+
10+ int main () {
11+ ios::sync_with_stdio (false );
12+ cin.tie (NULL ), cout.tie (NULL );
13+
14+ int T;
15+ cin >> T;
16+
17+ while (T--) {
18+ int N; // 노드 수
19+ cin >> N;
20+
21+ for (int i = 1 ; i <= N; i++){
22+ visited[i] = 0 ;
23+ parent[i] = i;
24+ }
25+
26+ for (int i = 0 ; i < N - 1 ; i++){
27+ int A, B; // A가 B의 부모
28+ cin >> A >> B;
29+ parent[B] = A;
30+ }
31+
32+ // LCA 최소 공통 조상 찾기
33+ int u,v;
34+ cin >> u >> v;
35+ visited[u] = 1 ;
36+
37+ // 루트 노드까지
38+ while (u != parent[u]){
39+ u = parent[u];
40+ visited[u] = 1 ;
41+ }
42+
43+ while (true ) {
44+ if (visited[v]){
45+ cout << v << " \n " ;
46+ break ;
47+ }
48+ v = parent[v];
49+ }
50+ }
51+ }
You can’t perform that action at this time.
0 commit comments