Henu
개발냥발
Henu
전체 방문자
오늘
어제
  • 분류 전체보기 (411)
    • DevOps (52)
      • Kubernetes (19)
      • Docker (14)
      • AWS (3)
      • Nginx (4)
      • Linux (4)
      • ArgoCD (1)
      • CN (2)
      • NATS (0)
      • Git (5)
    • Back-End (30)
      • Django (18)
      • Spring (5)
      • JPA (1)
      • MSA (5)
    • CS (87)
      • SystemSoftware (20)
      • OS (25)
      • Computer Architecture (16)
      • Network (23)
      • Database (2)
    • Lang (21)
      • Java (9)
      • Python (4)
      • C# (8)
    • Life (12)
    • 블록체인 (2)
    • Algorithm (204)
      • BOJ (160)
      • 프로그래머스 (19)
      • LeetCode (4)
      • SWEA (1)
      • 알고리즘 문제 해결 전략 (8)
      • DS, algorithms (7)
      • Checkio (5)
    • IT (2)

블로그 메뉴

  • GitHub
  • 글쓰기
  • 관리자

공지사항

  • Free!

인기 글

태그

  • Kubernetes
  • DFS
  • BFS
  • django
  • 프로그래머스
  • 다이나믹 프로그래밍
  • 백트래킹
  • docker
  • Network
  • boj

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Henu

개발냥발

[백준 2026] 소풍 (C++)
Algorithm/BOJ

[백준 2026] 소풍 (C++)

2021. 8. 4. 02:23
 

2026번: 소풍

만약 K명의 친구 관계인 학생들이 존재하지 않는다면 -1을 출력한다. 그 외의 경우에는, K개의 줄에 학생들의 번호를 증가하는 순서로 한 줄에 한 개씩 출력한다. 여러 경우가 존재한다면 첫 번째

www.acmicpc.net

백트래킹, 브루트 포스


소풍에 참여할 후보들을 nums배열에 차곡차곡 담아준다.

새로운 친구를 추가하려고 할 때 -> 새 친구는 지금까지 nums배열에 담긴 친구들과 모두 친구여야 한다.

 

이렇게 친구를 추가해 나가면서 nums배열의 길이가 K가 되면 지금까지 구한 친구들의 목록을 출력하고 exit한다.

 

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
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cstring>
#define fasti ios_base::sync_with_stdio(false); cin.tie(0);
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define INF 1e9+7
#define pii pair<int, int>
 
typedef long long ll;
// typedef pair<int, int> pii;
 
using namespace std;
 
int K, N, F;
bool is_friend[901][901];
 
void input(){
    cin >> K >> N >> F;
    int a, b;
    for(int i = 0; i < F; i++){
        cin >> a >> b;
        is_friend[a][b] = is_friend[b][a] = true;
        is_friend[a][a] = is_friend[b][b] = true;
    }
}
 
void recurecurecursive(int now, vector<int> &nums){
    if(nums.size() == K){
        for(auto &w : nums){
            cout << w << "\n";
        }
        exit(0);
    }
    
    for(int next = now+1; next <= N; next++){
        if(!is_friend[now][next]) continue;
        bool ad = true;
        for(auto &i : nums){
            if(!is_friend[i][next]){
                ad = false;
                break;
            }
        }
        if(!ad) break;
        nums.push_back(next);
        recurecurecursive(next, nums);
        nums.pop_back();
    }
}
 
void solve(){
    vector<int> num;
    for(int i = 1; i <= N; i++){
        num.push_back(i);
        recurecurecursive(i, num);
        num.pop_back();
    }
    cout << -1 << "\n";
}
 
int main(){
    fastio
    input();
    solve();
    
    return 0;
}
 
Colored by Color Scripter
cs

 

'Algorithm > BOJ' 카테고리의 다른 글

[백준 17142] 연구소 3 (C++)  (0) 2021.08.04
[백준 17182] 우주 탐사선 (C++)  (0) 2021.08.04
[백준 14284] 간선 이어가기 2 (C++)  (0) 2021.08.04
[백준 14391] 종이 조각 (C++)  (2) 2021.08.04
[백준 2243] 사탕상자 (C++)  (0) 2021.08.03
    'Algorithm/BOJ' 카테고리의 다른 글
    • [백준 17142] 연구소 3 (C++)
    • [백준 17182] 우주 탐사선 (C++)
    • [백준 14284] 간선 이어가기 2 (C++)
    • [백준 14391] 종이 조각 (C++)

    티스토리툴바