3677: [Apio2014]连珠线
来源:互联网 发布:sql 默认值绑定到表中 编辑:程序博客网 时间:2024/04/28 18:50
3677: [Apio2014]连珠线
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 320 Solved: 194
[Submit][Status][Discuss]
Description
在列奥纳多·达·芬奇时期,有一个流行的童年游戏,叫做“连珠线”。不出所料,玩这个游戏只需要珠子和线,珠子从1到礼编号,线分为红色和蓝色。游戏
开始时,只有1个珠子,而接下来新的珠子只能通过线由以下两种方式被加入:
1.Append(w,杪):-个新的珠子w和一个已有的珠子杪连接,连接使用红线。
2.Insert(w,u,v):-个新的珠子w加入到一对通过红线连接的珠子(u,杪)
之间,并将红线改成蓝线。也就是将原来u连到1的红线变为u连到w的蓝线与W连到V的蓝线。
无论红线还是蓝线,每条线都有一个长度。而在游戏的最后,将得到游戏的
最后得分:所有蓝线的长度总和。
现在有一个这个游戏的最终结构:你将获取到所有珠子之间的连接情况和所
有连线的长度,但是你并不知道每条线的颜色是什么。
你现在需要找到这个结构下的最大得分,也就是说:你需要给每条线一个颜
色f红色或蓝色),使得这种连线的配色方案是可以通过上述提到的两种连线方式
操作得到的,并且游戏得分最大。在本题中你只需要输出最大的得分即可。
Input
第一行是一个正整数n,表示珠子的个数,珠子编号为1刭n。
接下来n-l行,每行三个正整数ai,bi(l≤ai10000),表示有一条长度为ci的线连接了珠子ai和珠子bi。
Output
输出一个整数,为游戏的最大得分。
Sample Input
5
1 2 10
1 3 40
1 4 15
1 5 20
1 2 10
1 3 40
1 4 15
1 5 20
Sample Output
60
HINT
数据范围满足1≤n≤200000。
Source
#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std; const int maxn = 2E5 + 20;const int INF = ~0U>>1; struct E{ int to,w; E(){} E(int to,int w): to(to),w(w){}}; int n,Ans,f[maxn],g[maxn],fr[maxn],pos[maxn],sc[maxn]; vector <E> v[maxn]; void Dfs1(int x,int va,int fa){ fr[x] = sc[x] = -INF; for (int i = 0; i < v[x].size(); i++) { E e = v[x][i]; if (e.to == fa) continue; Dfs1(e.to,e.w,x); g[x] += max(f[e.to],g[e.to]); } for (int i = 0; i < v[x].size(); i++) { E e = v[x][i]; if (e.to == fa) continue; int now = g[x] - max(f[e.to],g[e.to]); f[x] = max(f[x],now + g[e.to] + e.w + va); now = g[e.to] - max(g[e.to],f[e.to]) + e.w; if (now < fr[x]) sc[x] = max(sc[x],now); else sc[x] = fr[x],fr[x] = now,pos[x] = i; }} void Dfs2(int x,int va,int sf,int sg,int fa){ Ans = max(Ans,g[x] + max(sf,sg)); for (int i = 0; i < v[x].size(); i++) { E e = v[x][i]; if (e.to == fa) continue; int nf = 0,ng; ng = g[x] - max(f[e.to],g[e.to]) + max(sf,sg); if (va) nf = ng - max(sf,sg) + sg + va + e.w; if (pos[x] == i) nf = max(nf,ng + sc[x] + e.w); else nf = max(nf,ng + fr[x] + e.w); Dfs2(e.to,e.w,nf,ng,x); }} int getint(){ char ch = getchar(); int ret = 0; while (ch < '0' || '9' < ch) ch = getchar(); while ('0' <= ch && ch <= '9') ret = ret * 10 + ch - '0',ch = getchar(); return ret;} int main(){ #ifdef DMC freopen("DMC.txt","r",stdin); #endif n = getint(); for (int i = 1; i < n; i++) { int x = getint(),y,w; y = getint(); w = getint(); v[x].push_back(E(y,w)); v[y].push_back(E(x,w)); } Dfs1(1,0,0); Dfs2(1,0,0,0,0); cout << Ans << endl; return 0;}
0 0
- 3677: [Apio2014]连珠线
- 【BZOJ 3677】 [Apio2014]连珠线
- BZOJ 3677【Apio2014】连珠线 树形dp
- bzoj3677【APIO2014】连珠线
- BZOJ 3677 & UOJ 105 [APIO2014]Beads and wires
- APIO2014题解
- 【APIO2014】Palindromes
- 连珠五子棋宝典 官方
- [游戏]五子连珠
- 回转连珠demo
- 开源 cocos2dx 五彩连珠.
- 五子连珠
- Android-五子连珠
- Android五子连珠
- 三子连珠游戏
- [Apio2014]回文串
- uoj 104 [apio2014 T2]
- bzoj3676: [Apio2014]回文串
- 高并发秒杀系统分析
- 第十一章、持有对象(容器)
- hdu3631(floyd入门)
- PHP+JavaScript 实现图片无刷新上传
- react组件化开发关于events库的解读
- 3677: [Apio2014]连珠线
- 1085. Perfect Sequence (25)
- 宏定义交换两个变量值的陷阱
- App外包流程,2017最新
- Spring MVC 参考文档
- MySQL学习(二)DQL 加强,多表查询
- 简述unity中Instantiate实例化函数的使用
- C++智能指针shared_ptr剖析
- 用数组去实现队列(c)