PAT (Advanced Level) 1053. Path of Equal Weight (30)
简单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)的更多相关文章
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 Wi assigne ...
- 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 ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- 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 ...
- 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 ...
- 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 weig ...
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
随机推荐
- Chapter 2 Open Book——21
I kept my head down and glanced up under my lashes. 我一直保持低着头并且在睫毛之下瞟了一眼. None of them were looking t ...
- csuoj1009
AC代码: #include <iostream>#include <iomanip>using namespace std;//计算数学期望值,可以自己直接通过数组的方式来实 ...
- 自动加载U盘
编辑/etc/fstab 比如想在开机的时候将/dev/sda1安装在/mnt 可以在/etc/fstab中加入一行 /dev/sda1 /mnt ext3 defaults 0 ...
- servlet第2讲(下集)----创建servlet实例(继承GenericServlet)
- 模拟摇奖:从1-36中随机抽出8个不重复的数字(math)
public class Yaojiang { public static void main(String[] args) { // TODO 自动生成的方法存根 int[] a=new int[8 ...
- MULE-ET0 、 ET1、ET2、PT1、PT2
设计验证阶段中的五个样车试制概念 骡子车( mule car ) ET0 第一轮设计工程样车试制 ET1 第二轮设计工程样车试制 ET2 第一轮产品工装样车试制 PT1 第二轮产品工装样车试制 PT2 ...
- Linq skip skipwhile take takewhile
一.Skip()跳过 static void Main(string[] args) { //skip()跳过 ,,,,,,,,,}; //跳过3条 nums.Skip().ToList().ForE ...
- spice-vdagent
The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...
- spring 入门级程序示例
public interface Action { public String execute(String str); } 接口 public class UpperAction implement ...
- Update Case的用法与execute执行字符串
摘自于网路:http://www.cnblogs.com/joinger/articles/1297160.html update h_crm_SafetyAccessUser set ...