Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- trie
- 카카오 인턴
- jdbc
- 튜플
- 알고리즘
- bulk update
- 가사 검색
- 호텔 방 배정
- 보행자 천국
- Python
- Spring Boot
- Tistory
- pycon
- 트라이
- 티스토리
- Open API
- 크레인 인형뽑기 게임
- 징검다리 건너기
- 티스토리 open api
- 불량 사용자
- 프로그래머스
- 트라이 #trie #알고리즘
- CleanCode
Archives
- Today
- Total
택시짱의 개발 노트
[SW Expert] 5521. 상원이의 생일파티 본문
링크
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
상원이가 친구들에게 줄 수 있는 초대장의 개수를 구하는 문제이다.
상원이가 초대장을 줄 수있는 경우는 친한친구와 친한친구의 친한친구 까지 줄수 있다.
이때 저는 친한 친구의 친한 친구를 어떻게 찾아야 될까 고민하다 dfs나 bfs를 이용하여 친구의 친구를 찾으면 되겠다 생각했습니다
그래서 친한 친구들을 인접리스트로 만든 후
상원이부터 시작하여 친한친구의 친한 친구 까지 탐색을 하여 결과를 구했습니다.
#include<iostream> #include<vector> #include<algorithm> #include<queue> #include<string> #include<set> #include<map> #include<cstring> #include<functional> #include<cmath> #include<stack> #define SIZE 555 const int INF = 2000000000; using namespace std; typedef long long int ll; typedef pair<int, int> pii; bool visited[SIZE]; void init() { for (int i = 0; i < SIZE; i++) { visited[i] = false; } } int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T; cin >> T; for(int t =1 ;t <=T;t++) { init(); vector<int> arr[SIZE]; queue < pair<int,int>> q; int N, M; cin >> N >> M; for (int i = 0; i < M; i++) { int a, b; cin >> a >> b; arr[a].push_back(b); arr[b].push_back(a); } int res = 0; q.push({ 1,0 }); while (!q.empty()) { pair<int, int> h = q.front(); q.pop(); visited[h.first] = true; if (h.second >= 2) { continue; } for (auto next : arr[h.first]) { if (visited[next] != true) { visited[next] = true; pair<int, int> n; n.first = next; n.second = h.second + 1; q.push(n); res++; } } } cout << '#' << t << " " << res << "\n"; } }
반응형
'알고리즘' 카테고리의 다른 글
[프로그래머스] 가장 먼 노드 (0) | 2020.04.15 |
---|---|
[프로그래머스] 순위 (0) | 2020.04.14 |
[프로그래머스] 구명보트 (0) | 2020.04.07 |
[프로그래머스] 징검다리 건너기 (0) | 2020.04.01 |
[프로그래머스] 호텔 방 배정 (0) | 2020.04.01 |
Comments