8000 Update 5567.cpp · qus0in/basic-algo-lecture@e37f2f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e37f2f9

Browse files
Update 5567.cpp
1 parent fc6d9ec commit e37f2f9

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

0x18/solutions/5567.cpp

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
1-
// Authored by : yongjunleeme
2-
// Co-authored by : BaaaaaaaaaaarkingDog
3-
// http://boj.kr/65c65de75cb34088b63463ec29b4e92a
1+
// Authored by : BaaaaaaaaaaarkingDog
2+
// Co-authored by : -
3+
// http://boj.kr/4de63bcfae1f4c529037d0b03050a65d
44
#include <bits/stdc++.h>
55
using namespace std;
66

77
int n, m, a, b;
88
vector <int> adj[505];
9-
int vis[505];
10-
int ans = 0;
11-
12-
void dfs(){
13-
stack<int> s;
14-
s.push(1);
15-
while(!s.empty()){
16-
int cur = s.top(); s.pop();
17-
vis[cur] = 1;
18-
for(auto nxt : adj[cur]){
19-
if(vis[nxt]) continue;
20-
vis[nxt] = 1;
21-
ans++;
22-
if(cur == 1) s.push(nxt); // 상근이의 이웃만 스택에 추가됨
23-
}
24-
}
25-
}
9+
int dist[505];
2610

2711
int main(void){
2812
ios::sync_with_stdio(0);
@@ -35,6 +19,34 @@ int main(void){
3519
adj[b].push_back(a);
3620
}
3721

38-
dfs();
39-
cout << ans;
22+
int ans = 0;
23+
queue<int> q;
24+
q.push(1);
25+
dist[1] = 1;
26+
while(!q.empty()){
27+
int cur = q.front();
28+
q.pop();
29+
if(dist[cur] > 2) // 친구의 친구는 거기서 bfs를 더 하지 않고 stop
30+
continue;
31+
for(auto nxt : adj[cur]){
32+
if(dist[nxt] > 0)
33+
continue;
34+
dist[nxt] = dist[cur] + 1;
35+
q.push(nxt);
36+
ans++;
37+
}
38+
}
39+
cout << ans << '\n';
4040
}
41+
42+
/*
43+
DFS로 풀이를 해도 괜찮지만 그 경우에는 같은 사람이 친구이자 친구의 친구인 경우의 처리에 주의.
44+
ex)
45+
1-2
46+
1-3
47+
2-3
48+
3-4
49+
3-5
50+
에서는 3이 상근이의 친구이자 친구의 친구임. 이 때 처리를 잘못하면
51+
자칫 4, 5를 포함시키지 않을 수도 있음.
52+
*/

0 commit comments

Comments
 (0)
0