#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm> using namespace std; vector<vector<int>* > paths; class Node {
public:
vector<int> child;
int weight;
Node(int w = ) : weight(w){}
}; int str2num(const char* str) {
if (str == NULL) return ;
int v = ;
int i = ;
char ch;
while ((ch = str[i++]) != '\0') {
v = v * + (ch - '');
}
return v;
} void print_nodes(vector<Node> &nodes) {
for (int i=; i<nodes.size(); i++) {
printf("id:%d nchild:%d\n", i, nodes[i].child.size());
for (int j=; j<nodes[i].child.size(); j++) {
printf(" %d", nodes[i].child[j]);
}
printf("\n");
}
} void dfs(vector<Node> &nodes, vector<int> &path, int idx, int sum, int target) {
if (idx >= nodes.size()) return; // invalid case;
int path_weight = sum + nodes[idx].weight;
if (path_weight == target) {
// not a leaf node, ignore it
if (!nodes[idx].child.empty()) {
return;
}
// leaf node, so we got a Root->Leaf path weight equal to the target weight
path.push_back(nodes[idx].weight);
// record it
paths.push_back(new vector<int>(path));
path.pop_back();
return;
} else if (path_weight > target) {
// impossible continue to find a valid path
return;
} int clen = nodes[idx].child.size();
for (int i=; i<clen; i++) {
path.push_back(nodes[idx].weight);
dfs(nodes, path, nodes[idx].child[i], path_weight, target);
path.pop_back();
} }
class cmpcls {
private:
vector<Node>* nodes;
public:
cmpcls(vector<Node>* ns) : nodes(ns) {}
bool operator()(int a, int b) {
if ((*nodes)[a].weight > (*nodes)[b].weight) {
return true;
} else {
return false;
}
}
}; bool mycmp(const vector<int>* a, const vector<int>* b) {
int len = ;
if (a->size() > b->size()) {
len = b->size();
} else {
len = a->size();
}
for (int i=; i<len; i++) {
if ((*a)[i] > (*b)[i]) return true;
}
return false;
} void print_path() {
int len = paths.size();
for (int i=; i<len; i++) {
int plen = paths[i]->size();
printf("%d", (*paths[i])[]);
for (int j=; j<plen; j++) {
printf(" %d", (*paths[i])[j]);
}
printf("\n");
}
}
int main() {
int N, M, S; scanf("%d%d%d", &N, &M, &S);
vector<Node> nodes(N); char buf[]; for (int i=; i<N; i++) {
int w = ;
scanf("%d", &w);
nodes[i].weight = w;
}
cmpcls cmpobj(&nodes); for (int i=; i<M; i++) {
int nchild = ;
scanf("%s%d", buf, &nchild);
Node& node = nodes[str2num(buf)];
for (int j=; j<nchild; j++) {
scanf("%s", buf);
node.child.push_back(str2num(buf));
}
sort(node.child.begin(), node.child.end(), cmpobj);
}
vector<int> path;
dfs(nodes, path, , , S);
print_path();
return ;
}

原先写的比较函数有问题一直有个case不对,换个思路直接把childnode的顺序按照weight大小来排,这样最终的结果就是按照规定排序。

PAT 1053 Path of Equal Weight的更多相关文章

  1. PAT 1053 Path of Equal Weight[比较]

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

  2. 【PAT】1053 Path of Equal Weight(30 分)

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

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

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

  4. 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 ...

  5. 1053 Path of Equal Weight——PAT甲级真题

    1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...

  6. 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 ...

  7. PAT (Advanced Level) 1053. Path of Equal Weight (30)

    简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

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

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

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

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

随机推荐

  1. SDUT OJ 顺序表应用4:元素位置互换之逆置算法

    顺序表应用4:元素位置互换之逆置算法 Time Limit: 10 ms Memory Limit: 570 KiB Submit Statistic Discuss Problem Descript ...

  2. Centos7服务器启动jar包项目最佳方式

    jar后台运行:nohup java -jar xx.jar >/dev/null & 此处的“>/dev/null”作用是将终端输出信息输出到空洞中,即不保存输出信息,若要查看输 ...

  3. (转)取消目录与SVN的关联

    第一种方法: 直接.逐级地删除目标目录中的隐藏属性的.svn目录 第二种方法: 如果用的是TortoiseSVN客户端,则先在另外一处建立一个新目录A,右键点住svn目录并拖动到A上松手,在弹出的菜单 ...

  4. Hibernate学习笔记(二)—— 实体规则&对象的状态&一级缓存

    一.持久化类 1.1 什么是持久化类? Hibernate是持久层的ORM映射框架,专注于数据的持久化工作.所谓的持久化,就是将内存中的数据永久存储到关系型数据库中.那么知道了什么是持久化,什么又是持 ...

  5. C++_IO与文件1-输入与输出概述

    为了方便起步先从istream类对象cin和ostream类对象cout开始,了解输入和输出的基本方法: 同时使用ifstream和ofstream对象进行文件的输入和输出: 然后详细学习cin和co ...

  6. LeetCode902. Numbers At Most N Given Digit Set

    题目: We have a sorted set of digits D, a non-empty subset of {'1','2','3','4','5','6','7','8','9'}.  ...

  7. opencv基本操作

    src.convertTo(dst, type, scale, shift) 缩放并转换到另外一种数据类型: dst:目的矩阵 type:需要的输出矩阵类型,或者更明确的,是输出矩阵的深度,如果是负值 ...

  8. Python入门书的读书笔记

    入门书地址 三引号 (""" 或 ''') 来指定多行字符串字符串是不可变的输出小数点后三位 print('{0:.3f}'.format(1 / 3))输出字符串长度为 ...

  9. Photoshop入门教程(七):蒙版

    学习心得:蒙版在Photoshop中也是很常用的,学会使用蒙版,可以提高图像处理能力,并且可以保护原片不被破坏,建议多使用一些蒙版. 蒙板是灰度的,是将不同灰度色值转化为不同的透明度,并作用到它所在的 ...

  10. 在Spark shell中基于HDFS文件系统进行wordcount交互式分析

    Spark是一个分布式内存计算框架,可部署在YARN或者MESOS管理的分布式系统中(Fully Distributed),也可以以Pseudo Distributed方式部署在单个机器上面,还可以以 ...