PAT 1053 Path of Equal Weight
#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的更多相关文章
- PAT 1053 Path of Equal Weight[比较]
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- 【PAT】1053 Path of Equal Weight(30 分)
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- 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 ...
- 1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...
- 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 (Advanced Level) 1053. Path of Equal Weight (30)
简单DFS #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
随机推荐
- 【python】Python的logging模块封装
#!/usr/local/python/bin # coding=utf-8 '''Implements a simple log library. This module is a simple e ...
- 用shell脚本安装apache
我们首先创建一个文件为test.sh,执行此文件的方法有以下四种方式: 1../test.sh(必须chmod赋予执行权限) 2.. test.sh 3.sourse test.sh 4.[shell ...
- Wiki凭什么持续得到开发人员和团队的喜爱
大家好,我是华为云DevCloud项目管理服务的产品经理恒少,作为布道师和产品经理,出差各地接触客户是常态,线下和华为云的客户交流.布道.技术沙龙. 但是线下交流,覆盖的用户总还是少数.我希望借助线上 ...
- linux的发行版
Linux的不同版本以及应用领域 1.Linux内核及发行版介绍 <1>Linux内核版本 内核(kernel)是系统的心脏,是运行程序和管理像磁盘和打印机等硬件设备的核心程序,它提供了一 ...
- Rails应用系列(1):初识Rails
第一个Rails应用 Rails是一个"模型-视图-控制器"框架(MVC).是用Ruby写的,所以要对Ruby要有一定的了解才能对rails框架深入学习.其实Ruby与Rails就 ...
- codeforces 1100F Ivan and Burgers 线性基 离线
题目传送门 题意: 给出 n 个数,q次区间查询,每次查询,让你选择任意个下标为 [ l , r ] 区间内的任意数,使这些数异或起来最大,输出最大值. 思路:离线加线性基. 线性基学习博客1 线性基 ...
- PHP二次开发
一.CMS类 如:phpcms.dedecms.empirecms.wordprss drupal joomla 二.shop类 如:ecshop eschopx shopnc emall mag ...
- Hibernate复合主键的注解
[转自] http://blog.csdn.net/happylee6688/article/details/17636801 最近做项目用到了Hibernate框架,采用了纯面向对象的思想,使用OR ...
- if __name__ == '__main__' 详解
1.__name__是啥 __name__ 是Python的模块名字. 手册解释:The name of the module. 2.__name__的值 首先,一个变量一次只有一个值. 其次,__n ...
- kvm 虚拟网络命令操作
2018-11-06 ```使用brctl命令创建网桥br1```# brctl addbr br1``` 删除网桥br1```# brctl delbr br1``` 将eth0端口加入网桥br1 ...