ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 바이러스
    백준알고리즘 2019. 7. 13. 00:30
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<iostream>
    #include<utility>
    
    using namespace std;
    int a[1001][1001];
    bool d[1001][1001];
    bool alp[1000];
    int result = -1;
    int n, t;
    
    int main() {
    	scanf("%d", &n);
    	scanf("%d", &t);
    	int x, y;
    	for (int i = 0; i < t; i++) {
    		scanf("%d %d", &x, &y);
    		a[x][y] = 1;
    		a[y][x] = 1;
    	}
    	queue<pair<int, int>> q;
    	alp[1] = true;
    	for (int i = 1; i <= n; i++) {
    		if (a[1][i] == 1 && d[1][i] == false) {
    			q.push(make_pair(1, i));
    			d[1][i] = true;
    			d[i][1] = true;
    			alp[i] = true;
    
    		}
    	}
    	while (!q.empty()) {
    		x = q.front().second; y = q.front().first;
    		alp[x] = true;
    		q.pop();
    		for (int i = 1; i <= n; i++) {
    			if (a[x][i] == 1 && d[x][i] == false)
    			{
    				q.push(make_pair(x, i));
    				d[x][i] = true;
    				d[i][x] = true;
    				alp[i] = true;
    			}
    		}
    	}
    	for (int i = 1; i <= n; i++)
    	{
    		if (alp[i] == true)
    			result += 1;
    	}
    	printf("%d\n", result);
    	return 0;
    
    
    }

     

    BOJ 문제, 바이러스 BFS 로 인접 행렬로 풀이 -> 인접했을때 사람 = true로 바꾸고 true 개수 새기.. 

    1은 항상 포함이므로 result 초기값 -1

    '백준알고리즘' 카테고리의 다른 글

    욕심쟁이 판다  (0) 2019.07.13
    동물원  (0) 2019.07.13
    정수 삼각형  (0) 2019.07.13
    RGB거리  (0) 2019.07.13
    빙산  (0) 2019.07.12

    댓글

Designed by Tistory.