-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNYOJ:1058_DFS.cpp
43 lines (39 loc) · 1.03 KB
/
NYOJ:1058_DFS.cpp
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
/*************************************************************************
> File Name: NYOJ:1058_DFS.cpp
> Author: heheql
> Mail: [email protected]
> Created Time: 2017年08月15日 星期二 19时41分02秒
************************************************************************/
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e8;
int a[20] = {0}, k, n;
bool b[20];
bool dfs(int x, int sum){
if(x == n) return sum == k;
b[x] = true;
if(dfs(x+1,sum+a[x])) return true;
b[x] = false;
if(dfs(x+1,sum)) return true;
return false;
}
int main(){
freopen("input.txt","r",stdin);
ios::sync_with_stdio(false);
while(cin >> n >> k){
for(int i = 0; i < n; i++){
cin >> a[i];
}
if(dfs(0,0)){
cout << "YES" << endl;
for(int i = 0; i < n; i++) {
if(b[i]) cout << a[i] << " ";
}
cout << endl;
}else {
cout << "NO" << endl;
}
}
return 0;
}