poj1694 排序+DFS
2014-03-12 00:25
190 查看
/** * poj1694 * 深搜+排序 * 以深搜的方式计算每一点所需的点数,计算的方法是 * 如果该点为叶子,返回1 * 否则,将其子点以子点的子点个数排序 * 从子点最多的子点开始模拟,根据需要的点和剩余的点,直到模拟出该点所需要的点的个数 * 最后返回root所需点的个数即可 */ #include <iostream> #include <stdlib.h> using namespace std; int tree[201][201]; int child_num[201]; int status[201]; int cmp ( const void *a , const void *b ) { return *(int *)b - *(int *)a; } int dfs(const int idx){ const int num = child_num[idx]; if(num == 0) return 1; int needed[num+1]; //计算各child的need值 for(int i=1;i<=num;++i){ needed[i] = dfs(tree[idx][i]); } //对所有need值排序 qsort(needed+1,num,sizeof(needed[0]),cmp); /* cout << idx << "th sorted: "; for(int i=1;i<=num;++i){ cout << " " << needed[i]; } cout << endl; */ //计算到达本点的needed值,返回 int need = 0,left = 0; for(int i=1;i<=num;++i){ if(left<needed[i]){ need += needed[i] - left; left = needed[i] - 1; } else{ left -= 1; } } //cout << "idx: " << idx << " need " << need << endl; return need; } int main(){ int m; cin >> m; while(m--){ int leaf_num; cin >> leaf_num; for(int i=1;i<=leaf_num;++i){ int leaf_idx; cin >> leaf_idx; cin >> child_num[leaf_idx]; for(int j=1;j<=child_num[leaf_idx];++j){ cin >> tree[leaf_idx][j]; } } int needed = 0,left = 0; /* for(int i=1;i<=leaf_num;++i){ for(int j=1;j<=child_num[i];++j){ cout << tree[i][j] << " "; } cout << endl; } */ needed = dfs(1); cout << needed << endl; } return 0; }
相关文章推荐
- An Old Stone Game(poj1694模拟与排序)
- [POJ 1691]Painting A Board[DFS][排序]
- POJ 1694 An Old Stone Game【递归+排序】
- POJ 1694 An Old Stone Game【递归+排序】
- ZOJ1711 POJ1564 Sum It Up,DFS+输出排序+去重复
- poj 1330 LCA [ vector 模拟邻接表存图 DFS 找到每个节点的层]
- poj 2488 dfs+回溯
- POJ 3009 dfs
- POJ 3256 (简单的DFS)
- POJ 1111 Image Perimeters 简单DFS
- POJ 1020 Anniversary Cake dfs
- poj 1129(dfs+图的四色定理)
- POJ 2531 Network Saboteur(DFS分两点集求最大权)
- poj 1979 Red and Black 【dfs】
- poj_1691 Painting A Board(dfs+拓扑)
- POJ 1321 棋盘问题(dfs回溯)
- <poj,sicily>Anti-prime Sequences (DFS)
- poj- 1321-棋子问题-dfs
- poj 2007 Scrambled Polygon (极角排序模板题)
- POJ 3411 Paid Roads(SPFA || DFS)