1053 Path of Equal Weight
1. 以下两组关系很大的概念
树的深度优先搜索 - 先根遍历 - 递归
树的广度优先搜索 - 层序遍历 - 非递归
本题考察的是前者,我设置了这样一个结构体
struct Prestruct{int totalWei = 0;vector<int> pre;
};Prestruct pre[maxn];
pre[idx].pre向量存放父节点到当前结点的权重
递归式:父节点的pre向量赋给子节点
递归边界:当前结点为叶子结点,比对该结点的权重和与目标是否相等,如果相等,将其加入
vector<vector<int> > ans;
注意到第一次我写这道题还用数组存储,所以最后的非升序输就处理得又臭又长,而vector<int>自身是可以比较的。
AC代码
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>
#include<stdlib.h>
#include<time.h>
#include<vector>
#include<set>
#include<string>
#include<tr1/unordered_map>using namespace std;
using namespace std::tr1;
typedef long long LL;const int maxn = 107;
const int MOD = 1000000007;
const int INF = 1000000000;//INF:下确界
const LL SUP = (1LL<<63)-1;//SUP:上确界
const double eps = 1e-5;struct Node{int v;vector<int> children;
};Node node[maxn];int target;struct Prestruct{int totalWei = 0;vector<int> pre;
};Prestruct pre[maxn];vector<vector<int> > ans;bool cmp(vector<int> a,vector<int> b){return a>b;
}void DFS(int root){pre[root].pre.push_back(node[root].v);pre[root].totalWei += node[root].v;if(node[root].children.size()==0){if(pre[root].totalWei==target){ans.push_back(pre[root].pre);}}for(int i=node[root].children.size()-1;i>=0;i--){int u = node[root].children[i];pre[u].pre = pre[root].pre;pre[u].totalWei = pre[root].totalWei;DFS(u);}
}int main(){int vNum,eNum;cin>>vNum>>eNum>>target;for(int i=0;i<vNum;i++){cin>>node[i].v;}while(eNum--){int v,num,vc;cin>>v>>num;while(num--){cin>>vc;node[v].children.push_back(vc);}}DFS(0);// for(int i=0;i<vNum;i++){
// printf("%d\n",pre[i].totalWei);
// }sort(ans.begin(),ans.end(),cmp);for(int i=0;i<ans.size();i++){for(int j=0;j<ans[i].size();j++){printf("%d%s",ans[i][j],j!=ans[i].size()-1?" ":"\n");}}return 0;
}
1053 Path of Equal Weight相关推荐
- PAT甲级1053 Path of Equal Weight (30分) :[C++题解]dfs求树的路径长度、邻接表
文章目录 题目分析 题目链接 题目分析 输入样例: 20 9 24 10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2 00 4 01 02 03 04 02 1 ...
- 1053 Path of Equal Weight
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigned ...
- PAT (Advanced Level) Practice 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 to ...
- 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 分)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- 1053 Path of Equal Weight(超级无敌详细注释+45行代码)
分数 30 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 Given a non-empty tree with root R, and with weight Wi assig ...
- 1053 Path of Equal Weight (30 分)
题目 Given a non-empty tree with root R, and with weight WiW_iWi assigned to each tree node TiT_iTi. ...
- PAT:1053. Path of Equal Weight (30) AC
#include<stdio.h> #include<vector> #include<queue> #include<algorithm> using ...
- 1053 Path of Equal Weigh(甲级)
1053 Path of Equal Weight (30分) Given a non-empty tree with root R, and with weight W i assigned ...
最新文章
- 数据库入门浅析:ASP.NET与MySQL连接
- zigbee物联网模块市场:LTE标准Cat.1和nbiot无线通信模块差异
- android tee,Android 9.0的新增安全特性与TEE
- 一键安装mysql5.6_一键安装MySQL5.6.43脚本
- Leetcode每日一题:34find-first-and-last-position-of-element-in-sorted-array(排序数组中查找某元素的两端位置)
- c++中double类型控制小数位数
- 阿里开源 iOS 协程开发框架 coobjc源码分析
- 让 ASP.NET AJAX 支持浏览器的 History Navigation - Part 1
- 自己动手写CPU之第七阶段(2)——简单算术操作指令实现过程
- 广州有这么一个无聊的人
- 电脑有网络,但所有浏览器网页都打不开,是怎么回事?
- 2018私塾在线高级java软件架构师实战培训最新
- Java电子发票管理系统
- java实现AES密钥生成
- 计算机原理课堂,计算机原理课堂测验题集.ppt
- 手把手教你开发一款简单的AR软件
- 设置Adobe Reader为default的PDF阅读工具
- 水晶报表(Crystal Report)- 水晶报表常见问题总结
- NRF52832学习笔记(19)——TWI(I2C)接口使用
- iOS--色值转换RGB
热门文章
- MySQL如何判别InnoDB表是独立表空间还是共享表空间
- C#图片灰度处理(位深度24→位深度8),用灰度数组byte[]新建一个8位灰度图像Bitmap 。...
- 黑色星期五阿里云向海淘输出双11技术
- Unique Paths II
- 17.SpringMVC核心技术-拦截器
- 如何让phpmyadmin输入密码再进入
- windows下使用aspell开启emacs的单词拼写检查功能
- nodejs npm install -g 全局安装和非全局安装的区别
- no argument specified with option /LIBPATH:错误的解决
- 史上最强女游戏程序员