Skip to content

Commit

Permalink
Merge pull request #17 from AlgoLeadMe/4-dhlee777
Browse files Browse the repository at this point in the history
4-dhlee777
  • Loading branch information
dhlee777 authored Mar 27, 2024
2 parents 034adf7 + 7b8e1e9 commit a0eb419
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions dhlee777/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| 1์ฐจ์‹œ | 2024.03.12 | BFS | [์ˆจ๋ฐ”๊ผญ์งˆ](https://www.acmicpc.net/problem/1697) | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/4)|
| 2์ฐจ์‹œ | 2024.03.18 | DP | [์‰ฌ์šด๊ณ„๋‹จ์ˆ˜](https://www.acmicpc.net/problem/10844) | [#8](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/8)|
| 3์ฐจ์‹œ | 2024.03.23 | union-find | [์ง‘ํ•ฉ์˜ ํ‘œํ˜„](https://www.acmicpc.net/problem/1717) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/14)|
| 4์ฐจ์‹œ | 2024.03.25 | BFS | [๋ฐ”์ด๋Ÿฌ์Šค](https://www.acmicpc.net/problem/2606) | [#17](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/17)|



Expand Down
81 changes: 81 additions & 0 deletions dhlee777/bfs/๋ฐ”์ด๋Ÿฌ์Šค.cpp
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;
}

0 comments on commit a0eb419

Please sign in to comment.