알고리즘/BaekJoon
[c++] 2606번 : 바이러스
__jhp_+
2022. 6. 28. 11:36
https://www.acmicpc.net/problem/2606
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍의 수가 주어
www.acmicpc.net
#include <iostream>
#include <stack>
using namespace std;
#define MAX_COMPUTER 101
int CountOfCompWithVirus(bool network[][MAX_COMPUTER], int computers);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
bool network[MAX_COMPUTER][MAX_COMPUTER] = { false, };
int computers, networks, from, to, count;
cin >> computers >> networks;
for (int i = 1; i <= networks; ++i)
{
cin >> from >> to;
network[from][to] = true;
network[to][from] = true;
}
count = CountOfCompWithVirus(network, computers);
cout << count << '\n';
return 0;
}
int CountOfCompWithVirus(bool network[][MAX_COMPUTER], int computers) {
stack<int> st;
bool compWithVirus[MAX_COMPUTER] = { false, };
int count = 0, start= 1;
st.push(start);
compWithVirus[start] = true;
while (st.empty() == false)
{
int from = st.top();
st.pop();
for (int to = start; to <= computers; ++to)
{
if (compWithVirus[to] == false && network[from][to] == true)
{
st.push(from);
st.push(to);
compWithVirus[to] = true;
++count;
break;
}
}
}
return count;
}