pat 1053

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#define MAX 110
using namespace std;
struct node
{
int data;
vector<int> child;
}Node[MAX];
int N , M , S;
int sum[MAX] = {0};
int max_h = 1;
vector<int> path;//存放路径
vector< vector<int> > global;
stack<int> s;
bool cmp(int a , int b)
{
return Node[a].data > Node[b].data;
}
bool judge(vector<int> &path)
{
int sum = 0;
for(vector<int>::iterator it = path.begin(); it != path.end() ; ++it)
{
sum += Node[(*it)].data;
}
if(sum == S)
return true;
else
return false;
}
void DFS(int root)
{
path.push_back(root);
if(Node[root].child.size() == 0)
{
if(judge(path))
{
global.push_back(path);
}
return;
}
for(int i = 0 ; i < Node[root].child.size() ; ++i)
{
DFS(Node[root].child[i]);
path.pop_back();
}
}
int main()
{
cin>>N>>M>>S;
int i,j;
for(i = 0 ; i < N ; ++i)
{
cin>>Node[i].data;
}
for(i = 0 ; i < M ; ++i)
{
int ID,num;
cin>>ID>>num;
int mem;
for(j = 0 ; j < num; ++j)
{
cin>>mem;
Node[ID].child.push_back(mem);
}
sort(Node[ID].child.begin() , Node[ID].child.end(), cmp);//能想出这个真吊,我反正想不到,输入的同时进行排序,按层排好,简直无敌
}
DFS(0);
int res[MAX][MAX];
for(vector<vector<int> >::iterator it = global.begin() ; it != global.end() ; ++it)
{
bool is = true;
for(vector<int>::iterator iter = (*it).begin() ; iter != (*it).end(); ++iter)
{
if(is)
{
cout<<Node[(*iter)].data;
is = false;
}
else
cout<<" "<<Node[(*iter)].data;
}
cout<<endl;
}
/*
for(vector<vector<int> >::iterator it = global.begin() ; it != global.end() ; ++it)
{
i = 0;
for(vector<int>::iterator iter = (*it).begin() ; iter != (*it).end(); ++iter)
{
res[i][j] = Node[(*iter)].data;
++j;
}
++i
}
int max = 0 , k = 0,temp = 0;
for(j = 0 ; j < global[k].size() ; ++j)
{
for(i = 0 ; i < global.size(); ++i)
{
if(max < res[i][j])
{
max = res[i][j];
temp = i;
}
}
}*/
return 0;
}

热评文章