PAT1053. Path of Equal Weight
//之前一直尝试用vector存储path,但是每次错误后回退上一级节点时不能争取回退,导致探索路径正确,但是输出不正确,用参数num,标记前一个路径点的位置传递参数,就好多了
//其中在输入时就将后继节点按照权值大小排列,是可以学习方法,再输入时就处理好,方便dfs探索,有几道PAT题目都是在该基础上对路径加要求,都可以直接改进输出那一部分代码,更新更和要求的路径,全部遍历完,就可得到所求符合要求的的路径
//参数传递的思想也不错,之前不习惯,总是直接把ans存进vector,最后输出
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=;
const double INF=<<;
struct node
{
int total;
int weight;
vector<int>child;
};
node list[maxn];
queue<int >q;
int path[maxn];
int n,m,s;
bool cmp(int a,int b)
{
return list[a].weight>list[b].weight;
}
void dfs(int v,int num)
{
if(list[v].total>s)return;
if(list[v].total==s)
{
if(list[v].child.size()!=)return ;
for(int j=;j<num;j++)
{
if(j==num-)printf("%d\n",list[path[j]].weight);
else printf("%d ",list[path[j]].weight);
}
}
for(int i=;i<list[v].child.size();i++)
{
int t=list[v].child[i];
list[t].total+=list[v].total;
path[num]=t;
dfs(t,num+);
}
}
int main()
{
freopen("input.txt","r",stdin);
int i,j,k,id,tmp;
while(scanf("%d%d%d",&n,&m,&s)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%d",&list[i].weight);
list[i].total=list[i].weight;
}
for(i=;i<m;i++)
{
scanf("%d%d",&id,&k);
for(j=;j<k;j++)
{
scanf("%d",&tmp);
list[id].child.push_back(tmp);
}
sort(list[id].child.begin(),list[id].child.end(),cmp);
}
dfs(,);
}
return ;
}
PAT1053. Path of Equal Weight的更多相关文章
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- Path of Equal Weight (DFS)
Path of Equal Weight (DFS) Given a non-empty tree with root R, and with weight Wi assigned to each ...
- 【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[比较]
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 ...
- PAT_A1053#Path of Equal Weight
Source: PAT A1053 Path of Equal Weight (30 分) Description: Given a non-empty tree with root R, and w ...
- 1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...
- 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 of ...
随机推荐
- Ignoring HTTPS certificates
Our Development setups won't have valid or trusted certificates. When do you want test our webserver ...
- POJ 1410 Intersection(计算几何)
题目大意:题目意思很简单,就是说有一个矩阵是实心的,给出一条线段,问线段和矩阵是否相交解题思路:用到了线段与线段是否交叉,然后再判断线段是否在矩阵里面,这里要注意的是,他给出的矩阵的坐标明显不是左上和 ...
- C语言小技巧
/* 求阶乘时设置最大调用层数,防止栈占满 当从函数进入另一个函数时当前函数的内容会入栈,另一个函数调用完时在出栈 */ int factorial(int n, int level) { //pri ...
- JAVA 文本框、密码框、标签
//文本框,密码框,标签 import java.awt.*; import javax.swing.*; public class Jiemian5 extends JFrame{ JPanel m ...
- Java拾穗
1.Class.forName("com.wzh.test.loadClass"); Class.forName("com.mysql.jdbc.Driver" ...
- UIActionSheet 传值
#pragma mark - actionSheet - (void)shareOrder:(NSDictionary *)product { UIActionSheet *as = [[UIActi ...
- vs报算术运算溢出的错误
是因为查询的数据量太大,把数据量减少点就不会报这个错了. 或者查询速度快点比如加索引也可能解决,待确定.
- 20145305《Java程序设计》实验三
(一)敏捷开发与XP 1.了解什么是敏捷开发 敏捷开发(Agile Development)是一种以人为核心.迭代.循序渐进的开发方法."敏捷流程"是一系列价值观和方法论的集合. ...
- SQL 去特殊字符
)) ) as begin declare @i int while patindex('%[^%@+*,=../_ <>''" ^0-9 ^a-Z ^''- ^吖-座]%' , ...
- POJ - 2533 Longest Ordered Subsequence(最长上升子序列)
d.最长上升子序列 s.注意是严格递增 c.O(nlogn) #include<iostream> #include<stdio.h> using namespace std; ...