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

4-dhlee777 #17

Merged
merged 2 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}