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

12-dhlee777 #51

Merged
merged 2 commits into from
May 22, 2024
Merged

12-dhlee777 #51

merged 2 commits into from
May 22, 2024

Conversation

dhlee777
Copy link
Contributor

πŸ”— 문제 링크

μ—°κ²°μš”μ†Œμ˜κ°œμˆ˜

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

2μ‹œκ°„

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

정점과 간선듀이 주어지고 κ·Έλž˜ν”„λ‚΄μ—μ„œ λͺ‡κ°œμ˜ 집합이 μ‘΄μž¬ν•˜λŠ”μ§€ κ΅¬ν•˜λŠ” λ¬Έμ œμ΄λ‹€. dfs,union-find 두가지 방법 λͺ¨λ‘ μ΄μš©ν•΄μ„œ ν’€ 수 μžˆλ‹€κ³  μƒκ°ν•˜μ˜€κ³  첫번째둜 union-findλ₯Ό μ΄μš©ν•˜μ—¬ ν’€μ–΄λ³΄μ•˜λ‹€.

union-find 풀이

1.κ°„μ„ μ˜ 양끝점을 μž…λ ₯받은 λ’€ unionnν•¨μˆ˜λ₯Ό 톡해 두 정점이 μ†ν•œ 집합끼리 ν•©μΉœλ‹€.
2.for루프문을 톡해 λͺ¨λ“ μ •μ λ“€μ„ νƒμƒ‰ν•˜λ©° κ°μ •μ μ˜ 루트 정점을 κ΅¬ν•œλ’€ set에 μ‚½μž…ν•œλ‹€.
3.set의 μ‚¬μ΄μ¦ˆκ°€ κ΅¬ν•˜κ³ μžν•˜λŠ” μ—°κ²°μš”μ†Œμ˜ κ°œμˆ˜κ°€ λœλ‹€.

set을 μ‚¬μš©ν•΄μ€€ μ΄μœ λŠ” κ°μ •μ λ“€μ˜ λ£¨νŠΈμ •μ μ΄ 쀑볡될 수 μžˆλŠ”λ° set 은 μ€‘λ³΅μ›μ†Œλ₯Ό 받지 μ•ŠκΈ° λ•Œλ¬Έμ— set의 크기둜 μ˜μ—­μ˜ 개수λ₯Ό ꡬ할 수 μžˆλ‹€.

dfs 풀이

#include<iostream>
#include<vector>
using namespace std;
vector<int>vec[1001];                   //κ°„μ„ μ˜ 정보λ₯Ό λ‚˜νƒ€λ‚΄κΈ°μœ„ν•œ 이차원벑터
int visited[1001];                         //λ°©λ¬Έμ—¬λΆ€μ €μž₯μœ„ν•œ λ°°μ—΄
int coun=0;
void dfs(int a) {
	for (int i = 0; i < vec[a].size(); i++) {
		if (!visited[vec[a][i]]) {
			visited[vec[a][i]] = 1;
			dfs(vec[a][i]);
		}
	}
}
int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int v, e,v1,v2;
	cin >> v >> e;
	for (int i = 0; i < e; i++) {
		cin >> v1 >> v2;
		vec[v1].push_back(v2);
		vec[v2].push_back(v1);
	}
	for (int j = 1; j <= v; j++) {
		if (!visited[j]) {
			visited[j] = 1;
			dfs(j);
			coun++;
		}
	}
	cout << coun;

}

1.벑터에 κ°„μ„ μ˜ 정보듀을 μž…λ ₯λ°›λŠ”λ‹€.
2. 1λΆ€ν„° λͺ¨λ“ μ •μ λ“€μ„ νƒμƒ‰ν•˜λ©° λ°©λ¬Έν•˜μ§€μ•Šμ•˜μ„κ²½μš°(visited[]=0) κ·Έμ •μ μ—μ„œ dfsλ₯Ό ν•΄μ€€λ‹€.
3. dfsλ₯Ό μ™„λ£Œν›„ μ—°κ²°μš”μ†Œμ˜ 개수λ₯Ό μ„ΈκΈ°μœ„ν•΄ coun++λ₯Ό ν•΄μ€€λ‹€.
image

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

dfs 풀이보닀 union-find둜 ν’€μ—ˆμ„λ•Œκ°€ λ©”λͺ¨λ¦¬κ°€ ν›¨μ”¬μ κ²Œ μ°¨μ§€ν•˜μ˜€κ³  μ‹œκ°„λ„ μ’€ λœκ±Έλ Έλ‹€.

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

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

union-find 같은 κ²½μš°λŠ” λ³„λ„μ˜ λ©”λͺ¨λ¦¬ 없이 κ΅¬ν˜„λ˜μ–΄μ„œ 그런 것 κ°™λ„€μš”.. 저도 DFS둜 ν’€μ—ˆμ—ˆλŠ”λ° μ•žμœΌλ‘œλŠ” 이런 κ²½μš°λŠ” union-findλ₯Ό μ‚¬μš©ν•΄μ„œ ν’€μ–΄μ•Όκ² λ„€μš”!
ν•¨μˆ˜ 이름 unionn 은 μΌλΆ€λŸ¬ κ·Έλ ‡κ²Œ μ“°μ‹ κ±΄κ°€μš”?

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

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

정점이 κ²ΉμΉ˜μ§€ μ•Šκ²Œ set둜 μ„€μ •ν•΄μ„œ ν‘Έμ‹ κ²Œ 인상 κΉŠμŠ΅λ‹ˆλ‹€. 이쯀되면 μœ λ‹ˆμ˜¨ νŒŒμΈλ“œ ν•œ 번 파보고 μ‹Άλ„€μš”.

@9kyo-hwang
Copy link

union-find 같은 κ²½μš°λŠ” λ³„λ„μ˜ λ©”λͺ¨λ¦¬ 없이 κ΅¬ν˜„λ˜μ–΄μ„œ 그런 것 κ°™λ„€μš”.. 저도 DFS둜 ν’€μ—ˆμ—ˆλŠ”λ° μ•žμœΌλ‘œλŠ” 이런 κ²½μš°λŠ” union-findλ₯Ό μ‚¬μš©ν•΄μ„œ ν’€μ–΄μ•Όκ² λ„€μš”!

ν•¨μˆ˜ 이름 unionn 은 μΌλΆ€λŸ¬ κ·Έλ ‡κ²Œ μ“°μ‹ κ±΄κ°€μš”?

이미 곡용체λ₯Ό μ˜λ―ΈλΌλŠ” union ν‚€μ›Œλ“œκ°€ μ •μ˜λ˜μ–΄ μžˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ 보톡 C++에선 uniteλž€ μ΄λ¦„μœΌλ‘œ ν•¨μˆ˜λͺ…을 μ§“λ”λΌκ΅¬μš” :)

@dhlee777
Copy link
Contributor Author

union-find 같은 κ²½μš°λŠ” λ³„λ„μ˜ λ©”λͺ¨λ¦¬ 없이 κ΅¬ν˜„λ˜μ–΄μ„œ 그런 것 κ°™λ„€μš”.. 저도 DFS둜 ν’€μ—ˆμ—ˆλŠ”λ° μ•žμœΌλ‘œλŠ” 이런 κ²½μš°λŠ” union-findλ₯Ό μ‚¬μš©ν•΄μ„œ ν’€μ–΄μ•Όκ² λ„€μš”! ν•¨μˆ˜ 이름 unionn 은 μΌλΆ€λŸ¬ κ·Έλ ‡κ²Œ μ“°μ‹ κ±΄κ°€μš”?

밑에 κ΅ν™©λ‹˜μ΄ λ§μ”€ν•΄μ£Όμ‹ λŒ€λ‘œ 이미 unionν‚€μ›Œλ“œκ°€ μ •μ˜λ˜μ–΄μžˆμ–΄μ„œ unionnμ΄λΌλŠ” ν‘œν˜„μ„ μΌμ§€λ§Œ λ‹€μŒλΆ€ν„°λŠ” uniteλΌλŠ” ν•¨μˆ˜λͺ…을 μ¨λ΄μ•Όκ² λ„€μš”:)

@InSange
Copy link
Collaborator

InSange commented May 21, 2024

union-find 같은 κ²½μš°λŠ” λ³„λ„μ˜ λ©”λͺ¨λ¦¬ 없이 κ΅¬ν˜„λ˜μ–΄μ„œ 그런 것 κ°™λ„€μš”.. 저도 DFS둜 ν’€μ—ˆμ—ˆλŠ”λ° μ•žμœΌλ‘œλŠ” 이런 κ²½μš°λŠ” union-findλ₯Ό μ‚¬μš©ν•΄μ„œ ν’€μ–΄μ•Όκ² λ„€μš”! ν•¨μˆ˜ 이름 unionn 은 μΌλΆ€λŸ¬ κ·Έλ ‡κ²Œ μ“°μ‹ κ±΄κ°€μš”?

visited뢀뢄을 boolν˜•μœΌλ‘œ μž‘μ•˜μ„ λ•Œ, 그리고 λ…Έλ“œμ˜ 크기가 μ΅œλŒ€μ— λͺ¨λ“  엣지듀이 ν•˜λ‚˜μ”© μ‘΄μž¬ν•˜μ˜€μ„ λ•Œ 차이가 많이 ν΄κΉŒμš”?

μ•„λ§ˆ ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€μ˜ 기쀀에 큰 영ν–₯을 받을 κ²ƒμœΌλ‘œ μƒκ°μ΄λ˜λŠ”λ°..

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

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

union_find의 경우 μ΅œμ •μƒμ˜ λΆ€λͺ¨ λ…Έλ“œλ₯Ό 기점으둜 μ§„ν–‰λ˜λ‹€λ³΄λ‹ˆ 단 λ°©ν–₯ 탐색인 반면 DFSλŠ” λͺ¨λ“  λ…Έλ“œλ“€μ— λŒ€ν•΄μ„œ μž¬κ·€κ°€ λΉˆλ²ˆν•˜κ²Œ 호좜되고 λΆ„κΈ°κ°€ λ§Žλ‹€λ³΄λ‹ˆ μ‹œκ°„λ³΅μž‘λ„ μΈ‘λ©΄μ—μ„œλ„ 차이가 μ–΄λŠμ •λ„ λ‚˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ 봀을 λ•Œ union_findκ°€ ν™•μ‹€νžˆ λ³΅μž‘λ„ μΈ‘λ©΄μ—μ„œλŠ” μ’‹μ•„λ³΄μ΄μ§€λ§Œ 각자 λ³„κ°œμ˜ νŠΉμ„±κ³Ό 기쀀을 κ°€μ§€λŠ” 걸둜 λ³΄μ•˜μ„λ•Œ ν•΄κ²°ν•˜μ§€ λͺ»ν•˜λŠ” 뢀뢄도 μ‘΄μž¬ν•˜μ§€ μ•Šμ„κΉŒμš”?

@yuyu0830
Copy link
Collaborator

union_find의 경우 μ΅œμ •μƒμ˜ λΆ€λͺ¨ λ…Έλ“œλ₯Ό 기점으둜 μ§„ν–‰λ˜λ‹€λ³΄λ‹ˆ 단 λ°©ν–₯ 탐색인 반면 DFSλŠ” λͺ¨λ“  λ…Έλ“œλ“€μ— λŒ€ν•΄μ„œ μž¬κ·€κ°€ λΉˆλ²ˆν•˜κ²Œ 호좜되고 λΆ„κΈ°κ°€ λ§Žλ‹€λ³΄λ‹ˆ μ‹œκ°„λ³΅μž‘λ„ μΈ‘λ©΄μ—μ„œλ„ 차이가 μ–΄λŠμ •λ„ λ‚˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ 봀을 λ•Œ union_findκ°€ ν™•μ‹€νžˆ λ³΅μž‘λ„ μΈ‘λ©΄μ—μ„œλŠ” μ’‹μ•„λ³΄μ΄μ§€λ§Œ 각자 λ³„κ°œμ˜ νŠΉμ„±κ³Ό 기쀀을 κ°€μ§€λŠ” 걸둜 λ³΄μ•˜μ„λ•Œ ν•΄κ²°ν•˜μ§€ λͺ»ν•˜λŠ” 뢀뢄도 μ‘΄μž¬ν•˜μ§€ μ•Šμ„κΉŒμš”?

ν™•μ‹€νžˆ μ‚¬μš© κ°€λŠ₯ν•œ 뢀뢄은 훨씬 μ œν•œμ μΌ 것 κ°™κΈ΄ ν•˜λ„€μš”... μ •ν™•νžˆ μ € 문제λ₯Ό ν•΄κ²°ν•  λ•Œ union_findκ°€ μœ μš©ν•  텐데 그런 κ²½μš°κ°€ μž¦μ§€λŠ” μ•Šμ„ 것 κ°™λ„€μš” γ…‹γ…‹

@dhlee777 dhlee777 merged commit b39df49 into main May 22, 2024
@dhlee777 dhlee777 deleted the 12-dhlee777 branch May 22, 2024 11:30
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