#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int a[105],fa[105];
vector<int>v[105];
int s;
bool cmp(int x,int y)
{
return a[x]>a[y];
}
void out(int u)
{
vector<int>tp;
while(u)
{
tp.push_back(u);
u = fa[u];
}
cout<<a[0]<<" ";
for(int i = tp.size()-1;i>0;i--)cout<<a[tp[i]]<<" ";
if(tp.size())cout<<a[tp[0]]<<endl;
}
void dfs(int u,int w)
{
if(w+a[u] == s&&v[u].size() == 0)
{
out(u);
return;
}
if(w+a[u]>s)return;
for(int i = 0;i<v[u].size();i++)
{
int t = v[u][i];
dfs(t,w+a[u]);
}
}
int main()
{
int n,m,i,j;
scanf("%d%d%d",&n,&m,&s);
memset(fa,-1,sizeof(fa));
for(i = 0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n == 1)
{
cout<<a[0]<<endl;
return 0;
}
for(i = 1;i<=m;i++)
{
int u,t,nm;
scanf("%d%d",&u,&nm);
for(j = 1;j<=nm;j++)
{
scanf("%d",&t);
v[u].push_back(t);
fa[t] = u;
}
sort(v[u].begin(),v[u].end(),cmp);
}
dfs(0,0);
return 0;
}
PAT 1053 Path of Equal Weight DFS
最新推荐文章于 2021-01-16 10:51:34 发布