-
Notifications
You must be signed in to change notification settings - Fork 1
18-fnhid #73
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
Conversation
|
μ λ μ΄ λ¬Έμ λ₯Ό 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 |
dohyeondol1
left a comment
There was a problem hiding this 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;
}
hadongun
left a comment
There was a problem hiding this 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')
π λ¬Έμ λ§ν¬
μ¬λ¬λΆμ λ€λ¦¬κ° λμ΄ λλ¦¬κ² μ΅λλ€!
βοΈ μμλ μκ°
30λΆ
β¨ μλ μ½λ
μ΄λ² μ°¨μλ κ°λ³κ² ν μ μλ λ¬Έμ λ₯Ό κ°μ Έμμ΅λλ€.
Nκ°μ μ μ κ³Ό N-2κ°μ κ°μ μΌλ‘ μ΄λ£¨μ΄μ§ κ·Έλνκ° μμ΅λλ€.
μ΄ κ·Έλνμμ N-1κ°μ μ μ λ N-2κ°μ κ°μ μΌλ‘ λͺ¨λ μ΄μ΄μ Έ μκ³ ,
1κ°μ μ μ μ λ€λ₯Έ λͺ¨λ μ μ κ³Ό λΆλ¦¬λμ΄ μμ΅λλ€.
μ λ ₯μΌλ‘ κ·Έλνμ N-2κ°μ κ°μ μ λ³΄κ° μ£Όμ΄μ§λλ°,
μ΄ μ 보λ₯Ό ν λλ‘ λ 립λ μ μ λ₯Ό μ°Ύμμ λ€λ₯Έ μ μ μ€ νλμ μ°κ²°ν΄μΌ ν©λλ€.
(μ°κ²°ν λ λ Έλλ₯Ό μΆλ ₯νλ©΄ λ©λλ€. μ€νμ μ μ§ λ¬Έμ λΌ μ¬λ¬ λ°©λ²μ΄ μμ κ²½μ° ν λ°©λ²λ§ μΆλ ₯ν΄λ μ λ΅ μ²λ¦¬ λ©λλ€.)
λ§μ λ°©λ²μΌλ‘ ν΄κ²°ν μ μκ² μ§λ§, 1λ² μ μ μμ dfsλ₯Ό μμνμ¬ λ°©λ¬Έν μ μ λ€μ λͺ¨λ 체ν¬νμ¬ μ£Όμκ³ ,
λ°©λ¬Ένμ§ μμ μ μ μ νμΈνμ¬ μΆλ ₯νλ λ°©μμΌλ‘ λ¬Έμ λ₯Ό ν΄κ²°νμμ΅λλ€.
μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©