简单DFS

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std; const int maxn=+;
vector<int>Tree[maxn];
long long val[maxn];
long long path[maxn];
int n,m;
long long W; bool cmp(const int &a,const int &b)
{
return val[a]>val[b];
} void read()
{
scanf("%d%d%lld",&n,&m,&W);
for(int i=;i<n;i++) scanf("%lld",&val[i]);
for(int i=;i<=m;i++)
{
int id; scanf("%d",&id);
int k; scanf("%d",&k);
while(k--)
{
int to; scanf("%d",&to);
Tree[id].push_back(to);
}
}
} void dfs(long long sum,int x,int deep)
{
if(sum>W) return;
if(sum==W)
{
if(Tree[x].size()==){
printf("%lld",val[]);
for(int i=;i<deep;i++)
printf(" %lld",path[i]);
printf("\n");
}
return;
} for(int i=;i<Tree[x].size();i++)
{
path[deep]=val[Tree[x][i]];
dfs(sum+val[Tree[x][i]],Tree[x][i],deep+);
}
} void work()
{
for(int i=;i<n;i++)
sort(Tree[i].begin(),Tree[i].end(),cmp);
dfs(val[],,);
} int main()
{
read();
work();
return ;
}

PAT (Advanced Level) 1053. Path of Equal Weight (30)的更多相关文章

  1. 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)

    题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...

  2. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  3. PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)

    1053 Path of Equal Weight (30 分)   Given a non-empty tree with root R, and with weight W​i​​ assigne ...

  4. PAT Advanced 1053 Path of Equal Weight (30) [树的遍历]

    题目 Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight ...

  5. PAT甲题题解-1053. Path of Equal Weight (30)-dfs

    由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...

  6. 1053. Path of Equal Weight (30)

    Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...

  7. 1053 Path of Equal Weight (30)(30 分)

    Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight ...

  8. 1053 Path of Equal Weight (30分)(并查集)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  9. pat1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

随机推荐

  1. Chapter 2 Open Book——21

    I kept my head down and glanced up under my lashes. 我一直保持低着头并且在睫毛之下瞟了一眼. None of them were looking t ...

  2. csuoj1009

    AC代码: #include <iostream>#include <iomanip>using namespace std;//计算数学期望值,可以自己直接通过数组的方式来实 ...

  3. 自动加载U盘

    编辑/etc/fstab 比如想在开机的时候将/dev/sda1安装在/mnt 可以在/etc/fstab中加入一行 /dev/sda1   /mnt    ext3    defaults    0 ...

  4. servlet第2讲(下集)----创建servlet实例(继承GenericServlet)

  5. 模拟摇奖:从1-36中随机抽出8个不重复的数字(math)

    public class Yaojiang { public static void main(String[] args) { // TODO 自动生成的方法存根 int[] a=new int[8 ...

  6. MULE-ET0 、 ET1、ET2、PT1、PT2

    设计验证阶段中的五个样车试制概念 骡子车( mule car ) ET0 第一轮设计工程样车试制 ET1 第二轮设计工程样车试制 ET2 第一轮产品工装样车试制 PT1 第二轮产品工装样车试制 PT2 ...

  7. Linq skip skipwhile take takewhile

    一.Skip()跳过 static void Main(string[] args) { //skip()跳过 ,,,,,,,,,}; //跳过3条 nums.Skip().ToList().ForE ...

  8. spice-vdagent

    The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...

  9. spring 入门级程序示例

    public interface Action { public String execute(String str); } 接口 public class UpperAction implement ...

  10. Update Case的用法与execute执行字符串

    摘自于网路:http://www.cnblogs.com/joinger/articles/1297160.html update h_crm_SafetyAccessUser set         ...