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
1 2 10
1 3 40
1 4 15
1 5 20
Sample Output
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;
}