일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 싸이월드
- node
- unity stencil buffer
- html
- react native
- c++ 정보은닉
- react native typescript navigation
- react native typescript
- stencil buffer
- react-native
- 스탠실 버퍼 시작
- javascript
- Expo
- 스탠실 버퍼 튜토리얼
- 리액트 네이티브 맥
- C++
- react native typescript navigate
- react native ios 기기 연결
- cyworld
- c++ using
- react native mac
- 리액트 네이티브 설치 오류
- react
- react native accessible
- react native 타입스크립트
- CSS
- 스탠실 버퍼 사용
- GitHub
- node.js
- 벡터와 리스트의 차이
Archives
- Today
- Total
혀니의 이거저거 뿌시기
[C++] 백준 2606 <바이러스> 본문
728x90
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하인 양의 정수이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍
www.acmicpc.net
내 블로그에 있던 bfs 정리글을 고대로 참조해서 풀었는데..
맞게한거같은데 자꾸 오류가 나길래 무엇이 원인인지 했더니만
우선 프로그래머스와는 다르게 백준은 cin도 내가 지정을 해줘야하기 때문에
배열크기나 원소의 초기값을 제대로 설정해줘야하는듯..
int의 초기값은 0이어서 괜찮지만 bool의 초기값은 true이기 때문에 {false,}를 이용해서 초기값을 설정해주어야 한다.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main()
{
int answer = 0;
int comCount; int lineCount;
cin >> comCount;
bool isVisited[comCount + 1] = {false,};
vector<int> graph[comCount + 1];
cin >> lineCount;
for(int i = 0; i < lineCount; i++)
{
int a,b;
cin >> a >> b;
graph[a].push_back(b);
graph[b].push_back(a);
}
queue<int> q;
q.push(1);
while(!q.empty())
{
int x = q.front();
q.pop();
for(int i = 0; i < graph[x].size() ; i++)
{
int back = graph[x][i];
if(!isVisited[back]){
q.push(back);
isVisited[back] = true;
if(back > 1)
{
answer++;
}
}
}
}
cout << answer << endl;
return 0;
}
또한, 단순히 깊이를 구하는것이 아닌, 1과 연결된것들을 구해야 하므로 양방향으로 넣어줘야 한다.
728x90
'알고리즘 공부(C++)' 카테고리의 다른 글
[C++]프로그래머스 입국심사 (0) | 2023.08.21 |
---|---|
[C++] 백준 1012 유기농 배추 (DFS) (0) | 2023.08.21 |
[C++]프로그래머스 개인정보 수집 유효기간 (0) | 2023.08.20 |
[C++]프로그래머스 [3차]n진수 게임 (0) | 2023.08.12 |
[C++]프로그래머스 징검다리 건너기(이진탐색) (0) | 2023.08.03 |