File tree Expand file tree Collapse file tree 3 files changed +54
-0
lines changed
Expand file tree Collapse file tree 3 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ #include < string>
2+ #include < vector>
3+ #include < queue>
4+ #include < list>
5+ #include < algorithm>
6+ #include < limits>
7+ using namespace std ;
8+
9+ // 다익스트라 알고리즘보다 BFS가 훨씬 빠름.
10+ // 가중치가 1일 때는 BFS를 이용하자!
11+
12+ int solution91 (int n, vector<vector<int >> edge) {
13+ int answer = 0 ;
14+ int inf = 0 ;
15+ vector<list<int > > graph (n+1 , list<int >());
16+ vector<int > dist (n+1 , 0 );
17+ vector<bool > check (n+1 , false );
18+ queue<int > q;
19+
20+ for (auto i = edge.begin (); i != edge.end (); i++) { // 1-2, 2-1 두 개로 저장
21+ graph[(*i).front ()].push_back ((*i).back ());
22+ graph[(*i).back ()].push_back ((*i).front ());
23+ }
24+
25+ q.push (1 );
26+ check[1 ] = true ;
27+ dist[1 ] = 0 ;
28+
29+ while (!q.empty ()) {
30+ int start = q.front ();
31+ q.pop ();
32+ for (auto i : graph[start]) {
33+ if (check[i]==false ) {
34+ check[i] = true ;
35+ dist[i] = dist[start] + 1 ;
36+ q.push (i);
37+ }
38+ }
39+ }
40+
41+ sort (dist.begin (), dist.end ());
42+ inf = dist.back ();
43+
44+ for (int i = 1 ; i <= n; i++) {
45+ if (dist[i] == inf)
46+ answer++;
47+ }
48+ return answer;
49+ }
50+
Original file line number Diff line number Diff line change 9494 <ClCompile Include =" Lv2\Lv2_피보나치수.cpp" />
9595 <ClCompile Include =" Lv2\Lv2_행렬의곱셈.cpp" />
9696 <ClCompile Include =" Lv3\Lv3_2xn타일링.cpp" />
97+ <ClCompile Include =" Lv3\Lv3_가장먼노드.cpp" />
9798 <ClCompile Include =" Lv3\Lv3_네트워크.cpp" />
9899 <ClCompile Include =" Lv3\Lv3_단속카메라.cpp" />
99100 <ClCompile Include =" Lv3\Lv3_단어변환.cpp" />
Original file line number Diff line number Diff line change 294294 <ClCompile Include =" 실전모의고사2회\Q3.cpp" >
295295 <Filter >소스 파일</Filter >
296296 </ClCompile >
297+ <ClCompile Include =" Lv3\Lv3_가장먼노드.cpp" >
298+ <Filter >소스 파일</Filter >
299+ </ClCompile >
297300 </ItemGroup >
298301</Project >
You can’t perform that action at this time.
0 commit comments