PAT 1053. Path of Equal Weight
来源:互联网 发布:linux如何删除文件夹 编辑:程序博客网 时间:2024/04/27 16:00
题目:http://pat.zju.edu.cn/contests/pat-a-practise/1053
题解:
给一棵树和各个树结点的权重,问哪些从根到叶子的权重和等于K。
深搜枚举所有可能即可。
代码:
#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<algorithm>using namespace std;#define INF 0x6fffffff#define MAX 105vector<int> link[MAX];vector<int> path;int weight[MAX];bool flag[MAX];int n,m,s;void dfs(int weit,int idx){ int len; weit+=weight[idx]; if(weit>s) return;//如果权重和大于要求,返回 else if(weit==s&&link[idx].size()==0) {//如果权重和等于要求且当前结点无子树 path.push_back(weight[idx]); printf("%d",path[0]); len=path.size(); for(int i=1;i<len;++i) printf(" %d",path[i]); printf("\n"); path.pop_back(); } else if(weit<s&&link[idx].size()>0) { int maxm,maxIdx=0; path.push_back(weight[idx]); for(;maxIdx!=-1;) { maxm=maxIdx=-1; len=link[idx].size(); for(int i=0;i<len;++i) { if(flag[link[idx][i]]==false&&weight[link[idx][i]]>maxm) { maxIdx=link[idx][i]; maxm=weight[link[idx][i]]; } } if(maxIdx!=-1) { flag[maxIdx]=true; dfs(weit,maxIdx); } } flag[idx]=true; path.pop_back(); } return;}int main(){ int a,b,k; scanf("%d%d%d",&n,&m,&s); memset(flag,false,sizeof(flag)); for(int i=0;i<n;++i) scanf("%d",weight+i); for(int i=0;i<m;++i) { scanf("%d%d",&a,&k); for(int j=0;j<k;++j) { scanf("%d",&b); link[a].push_back(b); } } dfs(0,0); return 0;}
来源:http://blog.csdn.net/acm_ted/article/details/20801651
0 0
- 1053. Path of Equal Weight (30)-PAT
- PAT 1053. Path of Equal Weight
- PAT 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- 【PAT】1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight (30)
- PAT 1053. Path of Equal Weight
- 【PAT】1053. Path of Equal Weight
- PAT--1053. Path of Equal Weight
- PAT 1053. Path of Equal Weight (30)
- 浙大PAT 1053题 1053. Path of Equal Weight
- PAT A 1053. Path of Equal Weight (30)
- 【PAT甲级】1053. Path of Equal Weight (30)
- 1053. Path of Equal Weight (30)PAT甲级
- PAT甲级练习1053. Path of Equal Weight (30)
- PAT甲级1053. Path of Equal Weight (30)
- PAT-A-1053. Path of Equal Weight (30)
- 第23条(第五章 泛型):不要在新代码中使用原生态类型
- 键盘常用ASCII码 & Ctrl组合键
- 舆情发展曲线相关度
- C++宏定义详解
- ACMCLUB 问题 A : 最爱回文串
- PAT 1053. Path of Equal Weight
- 黑马程序员 自动垃圾回收
- html 框架
- 浅谈ILOG-CP-Schedule
- Hash冲突解决办法
- RedHat&CentOS 6.4安装谷歌浏览器chrome
- JSTL自定义标签二SimpleTagSupport 生命周期(三)
- 一个80后程序员的转行创业日记9
- 舆情相似度计算