#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// 2606 ๋ฐ์ด๋ฌ์ค
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int iComputerCount, iPairCount = 0;
int counter = 0;
cin >> iComputerCount >> iPairCount;
vector<vector<int>> vecNetworks(iPairCount, vector<int>(2, 0));// row-PairCount, col-2
vector<int> vecVirus(iComputerCount, 0);
for (int i = 0; i < iPairCount; ++i)
{
int a, b;
cin >> a >> b;
vecNetworks[i][0] = a;
vecNetworks[i][1] = b;
}
// 1๋ฒ ์ปดํจํฐ ๋ฐ์ด๋ฌ์ค
queue<int> q;
q.push(1);
vecVirus[0] = 1;
// ๋ฐ์ด๋ฌ์ค ํ์ฅ
while (!q.empty())
{
int iCurCom = q.front();
q.pop();
// ์ฐ๊ฒฐ๋์ด ์๊ณ ์์ง ๊ฐ์ผ๋์ง ์์ ์ปดํจํฐ๋ค์ q์ ๋ฃ๋๋ค.
for (int i = 0; i < iPairCount; ++i)
{
if (vecNetworks[i][0] == iCurCom && vecVirus[vecNetworks[i][1] - 1] != 1)
{
q.push(vecNetworks[i][1]);
vecVirus[vecNetworks[i][1] - 1] = 1;
counter++;
}
else if(vecNetworks[i][1] == iCurCom && vecVirus[vecNetworks[i][0] - 1] != 1)
{
q.push(vecNetworks[i][0]);
vecVirus[vecNetworks[i][0] - 1] = 1;
counter++;
}
}
}
cout << counter;
return 0;
}
์ด์ฐจ์ vector๋ฅผ ๋ค๋ฃจ๋๊ฒ ์ต์ํ์ง ์์์ ์ด์ฐจ์ vector๋ก ํ์ด๋ณด์์ต๋๋ค.
์ฐธ๊ณ
https://velog.io/@yoonjong/C-2%EC%B0%A8%EC%9B%90-vector-%EC%B4%88%EA%B8%B0%ED%99%94
[C++] 2์ฐจ์ vector ์ด๊ธฐํ
2์ฐจ์ ๋ฒกํฐ์ ํ์ฉ ๊ด๋ จํด ๊ฐ๋จํ๊ฒ ์ ๋ฆฌํด๋ณด์๋ค.
velog.io
https://wh00300.tistory.com/116
C++ ์ด์ฐจ์ ๋ฒกํฐ ์ฌ์ฉ
vector ์ด์ฐจ์ ๋ฐฐ์ด์ column, ์ฆ ์ด์นธ์ ์์ ๋กญ๊ฒ ์ฌ์ฉํ๊ณ ์ถ์ด ์์๋ณด๋ค๊ฐ ์ด์ค๋ฒกํฐ๋ฅผ ์๊ฒ ๋์์ต๋๋ค. ๋ง์ฝ ์ฒ์์๋ 2*2์ ์ด์ฐจ์ ๋ฐฐ์ด์ ์ฌ์ฉํ๋ค๊ฐ ์ด ์ํ๋ฅผ ์ ์งํ๋ฉด์ 3*2์ ๋ฐฐ์ด์ด ํ์
wh00300.tistory.com
'๐ฅ๏ธ Study Note > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
programmers / level.2 / ์ด์ง ๋ณํ ๋ฐ๋ณตํ๊ธฐ(C++) (0) | 2023.01.11 |
---|---|
programmers / level.2 / ์ฌ๋ฐ๋ฅธ ๊ดํธ(C++) (0) | 2023.01.11 |
programmers / level.2 / ์ต์๊ฐ ๋ง๋ค๊ธฐ(C++) (0) | 2023.01.11 |
programmers / level.2 / ์ฐ์ต๋ฌธ์ : JadenCase ๋ฌธ์์ด ๋ง๋ค๊ธฐ(C++) (0) | 2023.01.08 |
programmers / level.2 / ์ฐ์ต๋ฌธ์ : ์ต๋๊ฐ๊ณผ ์ต์๊ฐ (c++) (0) | 2023.01.06 |