알고리즘 문제/AlgoSpot
GALLERY 감시 카메라 설치
parkit
2018. 9. 9. 11:24
728x90
반응형
https://algospot.com/judge/problem/read/GALLERY
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; const int WATCHED = 0; const int UNWATCHED = 1; const int INSTALLED = 2; bool visit[1001] = { false, }; int C = 0, H = 0, G = 0; int install = 0; vector<vector<int> > v; int DFS(int start) { visit[start] = true; bool child[3] = { false, false, false }; for (int next : v[start]) { if (!visit[next]) { child[DFS(next)] = true; } } // 감시되고 있지 않다면, 감시 카메라 갯수 1 증가 및 '설치됨' return if (child[UNWATCHED]) { ++install; return INSTALLED; } // 이미 설치가 되었다면, 이미 '감시 당함'이므로, return if (child[INSTALLED]) { return WATCHED; } // 위의 2개가 아니라면 return UNWATCHED; } int main(void) { int start_vertex = 0, end_vertex = 0; scanf("%d", &C); for (int tc = 1; tc <= C; tc++) { scanf("%d %d", &G, &H); // G : 갤러리의 수, H : 복도의 수 v.resize(G); while (H--) { scanf("%d %d", &start_vertex, &end_vertex); v[start_vertex].push_back(end_vertex); v[end_vertex].push_back(start_vertex); } for (int i = 0; i < v.size(); i++) { sort(v[i].begin(), v[i].end()); } for (int i = 0; i < G; i++) { if (!visit[i] && DFS(i) == UNWATCHED) { ++install; } } printf("답 : %d\n", install); install = 0; memset(visit, false, sizeof(visit)); for (int i = 0; i<G; i++) { if(!v[i].empty()) v[i].clear(); } } return 0; } | cs |
728x90
반응형