ZOJ 2374 Marbles on a tree
来源:互联网 发布:如何删除数据库中的表 编辑:程序博客网 时间:2024/06/06 09:06
查看原题
题意
把每一个节点上的大理石均匀移到树的每一个节点上,一次只能向相邻的移一个,问最少需要多少步骤
思路
从子节点算起,大于1的就把1以外的部分向上传,小于1的就往上拿1
注意要建立一个队列保存没有子节点的节点
代码
#include <iostream>#include <queue>using namespace std;struct node{ int marbles,fathernode,childnum;}nodes[10010];int main(){ int n; while(cin>>n){ if(n==0){ return 0; } queue<int>que; int moves=0; for(int i=1;i<=n;i++){ int childs,marble,number; cin>>number>>marble>>childs; nodes[number].marbles=marble; nodes[number].childnum=childs; if(childs==0)que.push(number); while(childs--){ int temp; cin>>temp; nodes[temp].fathernode=number; } } while(!que.empty()){ int i=que.front(); que.pop(); nodes[nodes[i].fathernode].childnum--; if(nodes[nodes[i].fathernode].childnum==0)que.push(nodes[i].fathernode); if(nodes[i].marbles>1){ int temp=nodes[i].marbles-1; nodes[i].marbles=1; nodes[nodes[i].fathernode].marbles+=temp; moves+=temp; } if(nodes[i].marbles<1){ int temp=1-nodes[i].marbles; nodes[i].marbles=1; nodes[nodes[i].fathernode].marbles-=temp; moves+=temp; } } cout<<moves<<endl; } return 0;}
0 0
- ZOJ 2374 Marbles on a tree
- poj1909 Marbles on a tree
- UVa10672 - Marbles on a tree(BFS)
- POJ 1909 Marbles on a tree 笔记
- pku 1909 Marbles on a tree(我都不知道这能不能算DP)
- A - Marbles
- spoj375Query on a tree
- Query on A Tree
- 3019. A Game with Marbles
- ZOJ 3863Paths on the Tree
- ZOJ 3686 A A Simple Tree Problem
- Marbles
- 3804 Query on a tree
- SPOJ Query on a tree
- Query on a tree II
- [SPOJ375]Query on a tree
- SPOJ375 Query on a tree
- hdu_3804_Query on a tree(树链剖分)
- [BZOJ1084][SCOI2005]最大子矩阵(dp)
- Java父类与接口方法重名的分析
- 评自己博客
- 打开免疫
- 欢迎使用CSDN-markdown编辑器
- ZOJ 2374 Marbles on a tree
- SPI接口
- 工作流框架--activiti(五)--个人任务和组任务
- 解决QQ启动出错:Initialization failure:0x0000000C
- bzoj 1071: [SCOI2007]组队 (单调性乱搞)
- java理解程序逻辑4
- iOS 内存管理
- MySQL数据库(7)
- 苹果iOS自动扫描拍照识别身份证软件SDK