#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. Java面向对象之抽象类abstract 入门实例

    一.基础概念 抽象事物,是若没有具体的信息可以描述这个事物,这个事物可以称为抽象事物. 抽象类,是不断的向上抽取而来.抽取方法声明而不确定具体的方法内容.由不同的子类来完成具体的方法内容. (一)抽象 ...

  2. app.use和app.get的区别及解析

    转载至:http://blog.csdn.net/wthfeng/article/details/53366169 写在前面:最近研究nodejs及其web框架express,对app.use和app ...

  3. springcloud微服务总结 zuul

    一 springcloud网关组件理解: 为什么需要网关呢? 我们知道我们要进入一个服务本身,很明显我们没有特别好的办法,直接输入IP地址+端口号,我们知道这样的做法很糟糕的,这样的做法大有问题,首先 ...

  4. R语言结果输出方法

    输出函数:cat,sink,writeLines,write.table 根据输出的方向分为输出到屏幕和输出到文件. 1.cat函数即能输出到屏幕,也能输出到文件. 使用方式:cat(... , fi ...

  5. Loj 6433. 「PKUSC2018」最大前缀和 (状压dp)

    题面 Loj 题解 感觉挺难的啊- 状压\(dp\) 首先,有一个性质 对于一个序列的最大前缀和\(\sum_{i=1}^{p} A[i]\) 显然对于每个\(\sum_{i=p+1}^{x}A[i] ...

  6. POJ - 3696 同余

    给定\(L\),求最小的\(x\)满足$ L|8\frac{10^x-1}{9} $ /*H E A D*/ inline ll gcd(ll a,ll b){return b?gcd(b,a%b): ...

  7. SQL中的object_id函数

    关于SQL中的object_id函数:应该就是指系统表中存储着数据库的所有对象 每一个对象都有一个唯一的标识符Id进行标识object_id 就是根据对象名称返回改对象的Idobject_name 就 ...

  8. 我所理解的session_set_save_handler的执行顺序机制

    默认的session handler启动顺序 <?php ini_set('session.gc_maxlifetime',10); ini_set('session.gc_probabilit ...

  9. kindeditor<=4.1.5文件上传漏洞

    最近发现很多网页篡改与暗链都是利用kindeditor编辑器,于是搜了一下kindeditor的漏洞,发现低于4.1.5版本的存在文件上传的漏洞,可以上传txt,html后缀的文档,许多恶意的文档貌似 ...

  10. Java 写入pdf文件

    import java.awt.Color; import java.io.File; import java.io.FileOutputStream; import com.lowagie.text ...