WHU1461(A problem on tree)贪心+找规律
2013-04-15 19:26
295 查看
/********************************************** 题目地址: http://acm.whu.edu.cn/land/problem/detail?problem_id=1461 题目大意: 定义的一个求ch值的函数(见下面注释部分); 即求树中根结点的ch值的方法,要你想办法帮他实现这个程序; 算法思想: 先一条链的情况: 设第一个点的值是a,第二个是b,第三个是c...那么有: ch[1]=a ch[2]=a+b ch[3]=max(2a+c,a+b+c) ch[4]=max(3a+d,2a+2b+d,2a+c+d,a+b+c+d) ch[5]=max(4a,3a+3b,4a+c,2a+2b+2c,value[4])+e 可以发现4a+c>4a,可得结论:一个点的ch值最多从不超过3层的地方转移而来; 记录每个节点最下三层最大的ch值,然后当前节点从这三个值中取最大的转移即可; 这是官方的解题报告,我是有点没怎么看懂,求大神来一发; ***********************************************/ #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #include<cstdlib> #include<vector> #include<algorithm> using namespace std; const int N=11111; typedef long long LL; int n; LL ch [4]; LL value ; vector<int>v[11000]; /* LL ch ,value ,depth ; int cal(int v) { ch[v]=value[v]; if (v is leaf) return value[v]; ch[v]=0; for( all u which is descendant of v ) ch[v]=max(ch[v],cal(u)*(depth[u]–depth[v])+value[v]) return ch[v]; } */ void dfs(int u,int father) { if(v[u].size()==1&&v[u][0]==father) { ch[u][0]=value[u]; ch[u][1]=ch[u][2]=0; return; } for(int i=0; i<v[u].size(); i++) { if (v[u][i]==father) continue; dfs(v[u][i],u); int j=v[u][i]; ch[u][0]=max(ch[u][0],max(ch[j][0],max(ch[j][1]*2,ch[j][2]*3))+value[u]); ch[u][1]=max(ch[u][1],ch[j][0]); ch[u][2]=max(ch[u][2],ch[j][1]); } } int main() { //freopen("C:\\Users\\Administrator\\Desktop\\kd.txt","r",stdin); int tcase; scanf("%d",&tcase); int t=0; while(tcase--) { memset(ch,0,sizeof(ch)); scanf("%d",&n); for(int i=1; i<=n; i++) { v[i].clear(); scanf("%lld",&value[i]); } int x,y; for(int i=1; i<n; i++) { scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } dfs(1,0); t++; printf("Case %d: %lld\n",t,ch[1][0]); } return 0; }
相关文章推荐
- whuProblem 1605 - Distance on Tree
- HDU 4912(Paths on the tree-树上取链,贪心)
- hdu 4912 Paths on the tree (LCA+贪心)
- hdu 4912 Paths on the tree(树链剖分+贪心)
- Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心
- HDU 4912 Paths on the tree(LCA+贪心)
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
- HDU 4912 Paths on the tree (LCA+贪心)
- 规律,模拟,贪心(Travelling Salesman Problem,HDU 5402)
- [贪心+DFS序列维护树上前缀和]2014 Multi-University Training Contest 5 - 1002 Paths on the tree
- hdu 5932: Backpack on Tree 贪心+dp
- hdu4912 Paths on the tree --- LCA贪心
- 【HDU5932 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 K】【树上背包 贪心乱搞】Backpack on Tree 物品成本只有12345下的树上背包
- HDU 4912 - Paths on the tree (LCA 贪心)
- 【HDU】5932.Backpack on Tree 【贪心+背包】
- HDU 4912 Paths on the tree 贪心+lca
- HDU 4912 Paths on the tree LCA 排序贪心
- HDU4912 Paths on the tree(数据结构,lca,贪心)
- HDU_4912_Paths on the tree LCA(最近公共祖先)+贪心
- 【2018Multi-University-Training Contest04】Problem C. Problems on a Tree