zoj 3516 Tree of Three
2011-07-24 22:50
363 查看
一看,恩,感觉以前见过类似的,貌似是树形DP。。
正在纠结着呢,党姐说这个可以用DFS解 = =。。想想也是,从根开始搜,回溯的时候更新父节点的前三个最大值即可。
话说。。。树形DP是神马。。
正在纠结着呢,党姐说这个可以用DFS解 = =。。想想也是,从根开始搜,回溯的时候更新父节点的前三个最大值即可。
话说。。。树形DP是神马。。
#include <cmath> #include <cstdio> #include <cstdlib> #include <iostream> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <stack> #include <climits> using namespace std; const int MAX = 10010; struct NODE{int to; NODE *next;}; struct AA{ int a[3];}; NODE node[MAX],*p[MAX]; int v[MAX]; int a[MAX][10]; int cou; void init() { cou = 0; memset(node,'\0',sizeof(node)); memset(p,'\0',sizeof(p)); memset(a,-1,sizeof(a)); } void Add(int u,int v) { node[cou].to = v; node[cou].next = p[u]; p[u] = &node[cou++]; } bool cmp(int a,int b) { return a > b; } void DFS(int x) { NODE *head = p[x]; if( head == NULL ) return ; while( head ) { int to = head->to; DFS(to); int b[10]; for(int i=0; i<3; i++) { b[i] = a[to][i]; b[i+3] = a[x][i]; } sort(b,b+6,cmp); memcpy(a[x],b,sizeof(b)); sort(a[x],a[x]+3,cmp); head = head->next; } } int main() { int n,x; while( ~scanf("%d",&n) ) { init(); scanf("%d",&v[0]); for(int i=1; i<n; i++) { scanf("%d%d",&x,&v[i]); Add(x,i); } for(int i=0; i<n; i++) a[i][0] = v[i]; DFS(0); int m; scanf("%d",&m); while( m-- ) { scanf("%d",&x); if( a[x][2] == -1 ) printf("-1\n"); else printf("%d %d %d\n",a[x][0],a[x][1],a[x][2]); } } return 0; }
相关文章推荐
- ZOJ 3516 Tree of Three
- ZOJ 3516 Tree of Three(DFS)
- ZOJ --- 3516 Tree of Three
- ZOJ Monthly, July 2011 - G Tree of Three 3516
- ZOJ 3201 Tree of Tree(树形DP+背包)
- ZOJ3516 Tree of Three
- ZOJ 3201 Tree of Tree
- ZOJ 3170 7 Levels of Binary Search Tree
- ZOJ Monthly, July 2011 【G】Tree of Three
- zoj 3170 7 Levels of Binary Search Tree(BST)
- ZOJ - 3201 Tree of Tree
- ZOJ - 3201 Tree of Tree
- TOJ 2676 ZOJ 3201 Tree of Tree / 树形DP
- ZOJ 3201 Tree of Tree(树形dp + 分组背包)
- ZOJ3516 Tree of Three,DFS
- ZOJ 3201 Tree of Tree
- zoj 3201 简单树形dp Tree of Tree
- ZOJ 3201 Tree of Tree(树形背包DP)
- ZOJ Monthly, July 2011 - G Tree of Three(树形DP)
- zoj 3170 7 Levels of Binary Search Tree(BST = =)