-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from AlgoLeadMe/4-dhlee777
4-dhlee777
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
|
||
//*์ฒซ๋ฒ์งธ ํ์ด(bfs ์ด์ฉ) | ||
#include<iostream> | ||
#include<vector> | ||
#include<queue> | ||
using namespace std; | ||
vector<int>com_edge[101]; //์ปดํจํฐ๊ฐ์ ๊ฐ์ ์ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ์ด์ฐจ์๋ฒกํฐ | ||
int visited[101]; //๊ฐ์ผ๋ ์ปดํจํฐ์ธ์ง ํ์ธํ๊ธฐ์ํ ๋ฐฐ์ด | ||
queue<int> q; | ||
int coun = 0; | ||
void bfs(int a) { //bfs ๋ฅผ ์ํ ํจ์ | ||
q.push(a); | ||
visited[a] = 1; | ||
while (!q.empty()) { | ||
int com = q.front(); | ||
q.pop(); | ||
for (int i = 0; i < com_edge[com].size(); i++) { | ||
int com_linked = com_edge[com][i]; | ||
if (!visited[com_linked]) | ||
{ | ||
q.push(com_linked); | ||
coun++; //๋ฐฉ๋ฌธ๋์ง์์ ์ฐ๊ฒฐ๋ ์ปดํจํฐ์ผ๊ฒฝ์ฐ ์นด์ดํธ | ||
visited[com_linked] = 1; | ||
} | ||
} | ||
} | ||
|
||
} | ||
int main(void) { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
int com_num, edge, a, b; | ||
cin >> com_num >> edge; | ||
for (int i = 0; i < edge; i++) { | ||
cin >> a >> b; | ||
com_edge[a].push_back(b); | ||
com_edge[b].push_back(a); | ||
} | ||
bfs(1); | ||
cout << coun; | ||
return 0; | ||
} | ||
|
||
|
||
|
||
//* ๋๋ฒ์งธ ํ์ด (union-find ์ด์ฉ) | ||
#include<iostream> | ||
using namespace std; | ||
int parent [101]; //์ด๋ค์ปดํจํฐ์ ๋ถ๋ชจ์ปดํจํฐ๋ฅผ ์ ์ฅํ๋ ๋ฐฐ์ด | ||
int find_parent(int a) { //๋ฃจํธ ์ปดํจํฐ๋ฅผ ์ฐพ๋ ํจ์ | ||
if (a == parent[a]) return a; | ||
else return parent[a] = find_parent(parent[a]); | ||
|
||
} | ||
void add(int a, int b) { //๋๊ฐ์ ์ปดํจํฐ๊ฐ ์ํ ๊ฐ๊ฐ์ ์งํฉ์ ํฉ์น๋ ํจ์ | ||
a = find_parent(a); | ||
b = find_parent(b); | ||
if (a > b) parent[a] = b; | ||
else parent[b] = a; | ||
} | ||
int main(void) { | ||
ios_base::sync_with_stdio(false); | ||
cin.tie(NULL); | ||
int com_num, edge,a,b; | ||
cin >> com_num >> edge; | ||
for (int i = 1; i <= com_num; i++) { //์ ์ผ์ฒ์ ์์ ์ ๋ถ๋ชจ๋ ์์ ์ด๊ธฐ์ ์ด๊ธฐํํด์ค๋ค. | ||
parent[i] = i; | ||
} | ||
for (int i = 0; i < edge; i++) { | ||
cin >> a >> b; | ||
add(a, b); | ||
} | ||
int coun = 0; | ||
for (int i = 1; i <= com_num; i++) { // ์ด๋ค์ปดํจํฐ์ ๋ฃจํธ์ปดํจํฐ๊ฐ 1์ด๋ผ๋ฉด 1๊ณผ ์ฐ๊ฒฐ๋ ๊ฐ์ ์งํฉ์ด๋ฏ๋ก ์ซ์๋ฅผ ์นด์ดํธํ๋ค. | ||
if (find_parent(i) == 1) coun++; | ||
} | ||
|
||
cout << coun-1; //1๋ฒ ์ปดํจํฐ์์ํด ๊ฐ์ผ๋ ์ปดํจํฐ์ ์๋ฅผ ์ธ๋๊ฒ์ด๋ฏ๋ก 1๋ฒ์ปดํจํฐ๋ฅผ ์ ์ธํด์ค๋ค. | ||
return 0; | ||
} | ||
|