[APIO2014]

3647 [APIO2014] Lianzhu line< /h1>

Title description< /h2>

During the Da Vinci era, there was a popular children’s game called Renzhu Line. Of course, this game is about beads and threads. The thread is red or blue, and the beads are numbered 1< span class="strut bottom">1 to nn. This game starts with a bead, and each time a new bead is added in the following way:

Append(w, v): a new bead $w$< span class="mord mathit"> and an added bead v Connect with a red line. span>

Insert(w, u, v): a new bead $w$insert into the two connected by a red line Beads $u,v$. The specific process is to delete u, vu,v 之Connect the red lines between uu,w And w, v< span class="base">. span> span>

Every line has A length. After the game is over, your final score is the sum of the length of the blue line.

Give you the game situation after the bead line game is over. It only tells you the connection method of the beads and the chain and the length of each line, but does not tell you what color each line is.

You need to write a program to find the maximum possible score. That is, among all the renju games that ended with the given final position, the one with the highest score is found, and then the largest possible score is output.

input format

A positive integer in the first line $n$ indicates the number of beads. Beads from 1 to n number. span>

Next n-1< span class="mord mathit"> Three integers per line $a_ {i},b_{i},c_{i}$ < span class="mord">< span class="mord rule">?. Guarantee $1 \leqslant a_{i},b_{i} \leqslant n$, $1 \leqslant c_{i} \leqslant 10000$< span class="mrel">< span class="mrel"> < span class="vlist-s">. Represents ai? No. beads and bi? Number of beads inter-connected The length is ci? the line. span> span> span> span> span> span>< /span>< /span>

Output format

Output an integer that represents the maximum possible score.

Input and output sample

Enter #1

5
1 2 10
1 3 40
1 4 15
1 5 20

Output#1
60

Input#2
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
output #2

140

Instructions/Tips

[Sample Description 1]

It can be obtained as follows 60 points: First start with bead number 3 starts. span>

Put 5 and < span class="katex-html">3 connected together. (Line length is arbitrary)< /span>

In 3 and 5 insert $11$< span class="katex">. (The line lengths are respectively 40and20). span> p>

put 2 and 1 The use length is $10$ to connect the lines. span>

put < span class="mord">4 and 1 The use length is $15$ to connect the lines. span>

[Restrictions and Conventions]

The first subtask has 13 points, which satisfies $1\leqslant n \leqslant 10$< /span>

The second subtask has 15 points, satisfying $1\leqslant n \leqslant 200 $

The third subtask has 29 points, satisfying $1\leqslant n \leqslant 10000$

The fourth subtask has 43 points, satisfying $1\leqslant n \leqslant 200000$< /p>

Solution:

For convenience, we treat each bead as a node

We are very happy to find that the result must be a tree with different sides

Since there is only one node at the beginning,is equivalent to choosing a node as the root,

Then do two operations:

1. Each time the son of a node is expanded, a node and a point already on the tree are connected to a red edge, and Do not add beads between these two points:

share picture

Or expand the grandson of a node and then expand the dad of this grandson Dad, just connect a node on the tree to another node, insert a node between these two nodes, and dye the two edges blue, that is: p>

share picture

share picture

Okay, assuming point 1 is the root, we can list one DP equation

f[i][0/1] indicates whether the current node is the maximum value obtained by the midpoint

$f[i][0]=\sum\limits_{j}^{j \in son[i]}max(f[ j][0],f[j][1]+dis(i,j));$

3647 [APIO2014] Lianzhu thread

3647 [APIO2014] Lianzhu thread

< div class="card problem-card padding-default" data-v-796309f8="" data-v-31a03e4d="">

Title description

During the time of Da Vinci, there was a popular children’s game called renju line. Of course, this game is about beads and threads. The thread is red or blue, and the beads are numbered 1< span class="strut bottom">1 to nn. This game starts with a bead, and each time a new bead is added in the following way:

Append(w, v): a new bead $w$< span class="mord mathit"> and an added bead v Connect with a red line. span>

Insert(w, u, v): a new bead $w$insert into the two connected by a red line Beads $u,v$. The specific process is to delete u, vu,v 之Connect the red lines between uu,w And w, v< span class="base">. span> span>

Every line has A length. After the game is over, your final score is the sum of the length of the blue line.

Give you the game situation after the bead line game is over. It only tells you the connection method of the beads and the chain and the length of each line, but does not tell you what color each line is.

You need to write a program to find the maximum possible score. That is, among all the renju games that ended with the given final position, the one with the highest score is found, and then the largest possible score is output.

input format

A positive integer in the first line $n$ indicates the number of beads. Beads from 1 to n number. span>

Next n-1< span class="mord mathit"> Three integers per line $a_ {i},b_{i},c_{i}$ < span class="mord"> ?. Guarantee $1 \leqslant a_{i},b_{i} \leqslant n$, $1 \leqslant c_{i} \leqslant 10000$< span class="mrel">< span class="mrel"> < span class="vlist-s">. Represents ai? No. beads and bi? Number of beads inter-connected The length is ci? the line. span> span> span> span>

输出格式

输出一个整数,表示最大可能得分。

输入输出样例

输入 #1

5
1 2 10
1 3 40
1 4 15
1 5 20

输出 #1
60

输入 #2
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
输出 #2

140

说明/提示

【样例描述1】

可以通过如下方式获得 60 分:首先从 3 号珠子开始。

5 和 3 连起来。(线长度任意)

在 3 和 5 之间插入 $11$。(线长分别为 40 和 20)。

把 2 和 1 用长度为 $10$ 的线连起来。

把 4 和 1 用长度为 $15$ 的线连起来。

【限制与约定】

第一个子任务共 13 分,满足 $1\leqslant n \leqslant 10$

第二个子任务共 15 分,满足 $1\leqslant n \leqslant 200$

第三个子任务共 29 分,满足 $1\leqslant n \leqslant 10000$

第四个子任务共 43 分,满足 $1\leqslant n \leqslant 200000$

 

Solution:

为了方便,我们把每个珠子看成一个节点

我们非常开心的发现,结果肯定是一棵边颜色不同的树

由于刚开始只有任意的一个节点,就相当于我们选择一个节点为根,

然后进行两种操作:

1.每次扩展一个节点的儿子,即把一个节点和已经在树上的一个点连上一条红边,且不在这两个点之间加珠子:

分享图片

或者扩展一个节点的孙子再扩展这个孙子的爸爸,就是把树上的一个节点连到另一个节点上,在把一个节点插到这两个节点之间,把两条边染成蓝色,即:

分享图片

分享图片

 好的,假设以1号点为根,我们可以列出一个DP方程式

f[i][0/1]表示当前节点是否为中点获得的最大价值

$f[i][0]=\sum\limits_{j}^{j \in son[i]}max(f[j][0],f[j][1]+dis(i,j));$

题目描述

在达芬奇时代,有一个流行的儿童游戏称为连珠线。当然,这个游戏是关于珠子和线的。线是红色或蓝色的,珠子被编号为 11 到 nn。这个游戏从一个珠子开始,每次会用如下方式添加一个新的珠子:

Append(w, v):一个新的珠子 $w$ 和一个已经添加的珠子 v 用红线连接起来。

Insert(w, u, v):一个新的珠子$w$插入到用红线连起来的两个珠子$u,v$之间。具体过程是删去 u, vu,v 之间红线,分别用蓝线连接 uu,w 和 w, v

每条线都有一个长度。游戏结束后,你的最终得分为蓝线长度之和。

给你连珠线游戏结束后的游戏局面,只告诉了你珠子和链的连接方式以及每条线的长度,没有告诉你每条线分别是什么颜色。

你需要写一个程序来找出最大可能得分。即,在所有以给出的最终局面结束的连珠线游戏中找出那个得分最大的,然后输出最大可能得分。

输入格式

第一行一个正整数 $n$,表示珠子的数量。珠子从 1 到 n 编号。

接下来 n – 1 行每行三个整数 $a_{i},b_{i},c_{i}$?。保证 $1 \leqslant a_{i},b_{i} \leqslant n$,$1 \leqslant c_{i} \leqslant 10000$。表示 ai? 号珠子和 bi? 号珠子间连了长度为 ci? 的线。

输出格式

输出一个整数,表示最大可能得分。

输入输出样例

输入 #1

5
1 2 10
1 3 40
1 4 15
1 5 20

输出 #1
60

输入 #2
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
输出 #2

140

说明/提示

【样例描述1】

可以通过如下方式获得 60 分:首先从 3 号珠子开始。

5 和 3 连起来。(线长度任意)

在 3 和 5 之间插入 $11$。(线长分别为 40 和 20)。

把 2 和 1 用长度为 $10$ 的线连起来。

把 4 和 1 用长度为 $15$ 的线连起来。

【限制与约定】

第一个子任务共 13 分,满足 $1\leqslant n \leqslant 10$

第二个子任务共 15 分,满足 $1\leqslant n \leqslant 200$

第三个子任务共 29 分,满足 $1\leqslant n \leqslant 10000$

第四个子任务共 43 分,满足 $1\leqslant n \leqslant 200000$

 

Solution:

为了方便,我们把每个珠子看成一个节点

我们非常开心的发现,结果肯定是一棵边颜色不同的树

由于刚开始只有任意的一个节点,就相当于我们选择一个节点为根,

然后进行两种操作:

1.每次扩展一个节点的儿子,即把一个节点和已经在树上的一个点连上一条红边,且不在这两个点之间加珠子:

分享图片

或者扩展一个节点的孙子再扩展这个孙子的爸爸,就是把树上的一个节点连到另一个节点上,在把一个节点插到这两个节点之间,把两条边染成蓝色,即:

分享图片

分享图片

 好的,假设以1号点为根,我们可以列出一个DP方程式

f[i][0/1]表示当前节点是否为中点获得的最大价值

$f[i][0]=\sum\limits_{j}^{j \in son[i]}max(f[j][0],f[j][1]+dis(i,j));$

题目描述

在达芬奇时代,有一个流行的儿童游戏称为连珠线。当然,这个游戏是关于珠子和线的。线是红色或蓝色的,珠子被编号为 11 到 nn。这个游戏从一个珠子开始,每次会用如下方式添加一个新的珠子:

Append(w, v):一个新的珠子 $w$ 和一个已经添加的珠子 v 用红线连接起来。

Insert(w, u, v):一个新的珠子$w$插入到用红线连起来的两个珠子$u,v$之间。具体过程是删去 u, vu,v 之间红线,分别用蓝线连接 uu,w 和 w, v

每条线都有一个长度。游戏结束后,你的最终得分为蓝线长度之和。

给你连珠线游戏结束后的游戏局面,只告诉了你珠子和链的连接方式以及每条线的长度,没有告诉你每条线分别是什么颜色。

你需要写一个程序来找出最大可能得分。即,在所有以给出的最终局面结束的连珠线游戏中找出那个得分最大的,然后输出最大可能得分。

输入格式

第一行一个正整数 $n$,表示珠子的数量。珠子从 1 到 n 编号。

接下来 n – 1 行每行三个整数 $a_{i},b_{i},c_{i}$?。保证 $1 \leqslant a_{i},b_{i} \leqslant n$,$1 \leqslant c_{i} \leqslant 10000$。表示 ai? 号珠子和 bi? 号珠子间连了长度为 ci? 的线。

输出格式

输出一个整数,表示最大可能得分。

输入输出样例

输入 #1

5
1 2 10
1 3 40
1 4 15
1 5 20

输出 #1
60

输入 #2
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
输出 #2

140

说明/提示

【样例描述1】

可以通过如下方式获得 60 分:首先从 3 号珠子开始。

5 和 3 连起来。(线长度任意)

在 3 和 5 之间插入 $11$。(线长分别为 40 和 20)。

把 2 和 1 用长度为 $10$ 的线连起来。

把 4 和 1 用长度为 $15$ 的线连起来。

【限制与约定】

第一个子任务共 13 分,满足 $1\leqslant n \leqslant 10$

第二个子任务共 15 分,满足 $1\leqslant n \leqslant 200$

第三个子任务共 29 分,满足 $1\leqslant n \leqslant 10000$

第四个子任务共 43 分,满足 $1\leqslant n \leqslant 200000$

 

Solution:

为了方便,我们把每个珠子看成一个节点

我们非常开心的发现,结果肯定是一棵边颜色不同的树

由于刚开始只有任意的一个节点,就相当于我们选择一个节点为根,

然后进行两种操作:

1.每次扩展一个节点的儿子,即把一个节点和已经在树上的一个点连上一条红边,且不在这两个点之间加珠子:

分享图片

或者扩展一个节点的孙子再扩展这个孙子的爸爸,就是把树上的一个节点连到另一个节点上,在把一个节点插到这两个节点之间,把两条边染成蓝色,即:

分享图片

分享图片

 好的,假设以1号点为根,我们可以列出一个DP方程式

f[i][0/1]表示当前节点是否为中点获得的最大价值

$f[i][0]=\sum\limits_{j}^{j \in son[i]}max(f[j][0],f[j][1]+dis(i,j));$

题目描述

在达芬奇时代,有一个流行的儿童游戏称为连珠线。当然,这个游戏是关于珠子和线的。线是红色或蓝色的,珠子被编号为 11 到 nn。这个游戏从一个珠子开始,每次会用如下方式添加一个新的珠子:

Append(w, v):一个新的珠子 $w$ 和一个已经添加的珠子 v 用红线连接起来。

Insert(w, u, v):一个新的珠子$w$插入到用红线连起来的两个珠子$u,v$之间。具体过程是删去 u, vu,v 之间红线,分别用蓝线连接 uu,w 和 w, v

每条线都有一个长度。游戏结束后,你的最终得分为蓝线长度之和。

给你连珠线游戏结束后的游戏局面,只告诉了你珠子和链的连接方式以及每条线的长度,没有告诉你每条线分别是什么颜色。

你需要写一个程序来找出最大可能得分。即,在所有以给出的最终局面结束的连珠线游戏中找出那个得分最大的,然后输出最大可能得分。

输入格式

第一行一个正整数 $n$,表示珠子的数量。珠子从 1 到 n 编号。

接下来 n – 1 行每行三个整数 $a_{i},b_{i},c_{i}$?。保证 $1 \leqslant a_{i},b_{i} \leqslant n$,$1 \leqslant c_{i} \leqslant 10000$。表示 ai? 号珠子和 bi? 号珠子间连了长度为 ci? 的线。

输出格式

输出一个整数,表示最大可能得分。

输入输出样例

输入 #1

5
1 2 10
1 3 40
1 4 15
1 5 20

输出 #1
60

输入 #2
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
输出 #2

140

说明/提示

【样例描述1】

可以通过如下方式获得 60 分:首先从 3 号珠子开始。

5 和 3 连起来。(线长度任意)

在 3 和 5 之间插入 $11$。(线长分别为 40 和 20)。

把 2 和 1 用长度为 $10$ 的线连起来。

把 4 和 1 用长度为 $15$ 的线连起来。

【限制与约定】

第一个子任务共 13 分,满足 $1\leqslant n \leqslant 10$

第二个子任务共 15 分,满足 $1\leqslant n \leqslant 200$

第三个子任务共 29 分,满足 $1\leqslant n \leqslant 10000$

第四个子任务共 43 分,满足 $1\leqslant n \leqslant 200000$

 

Solution:

为了方便,我们把每个珠子看成一个节点

我们非常开心的发现,结果肯定是一棵边颜色不同的树

由于刚开始只有任意的一个节点,就相当于我们选择一个节点为根,

然后进行两种操作:

1.每次扩展一个节点的儿子,即把一个节点和已经在树上的一个点连上一条红边,且不在这两个点之间加珠子:

分享图片

或者扩展一个节点的孙子再扩展这个孙子的爸爸,就是把树上的一个节点连到另一个节点上,在把一个节点插到这两个节点之间,把两条边染成蓝色,即:

分享图片

分享图片

 好的,假设以1号点为根,我们可以列出一个DP方程式

f[i][0/1]表示当前节点是否为中点获得的最大价值

$f[i][0]=\sum\limits_{j}^{j \in son[i]}max(f[j][0],f[j][1]+dis(i,j));$

在达芬奇时代,有一个流行的儿童游戏称为连珠线。当然,这个游戏是关于珠子和线的。线是红色或蓝色的,珠子被编号为 11 到 nn。这个游戏从一个珠子开始,每次会用如下方式添加一个新的珠子:

Append(w, v):一个新的珠子 $w$ 和一个已经添加的珠子 v 用红线连接起来。

Insert(w, u, v):一个新的珠子$w$插入到用红线连起来的两个珠子$u,v$之间。具体过程是删去 u, vu,v 之间红线,分别用蓝线连接 uu,w 和 w, v

每条线都有一个长度。游戏结束后,你的最终得分为蓝线长度之和。

给你连珠线游戏结束后的游戏局面,只告诉了你珠子和链的连接方式以及每条线的长度,没有告诉你每条线分别是什么颜色。

你需要写一个程序来找出最大可能得分。即,在所有以给出的最终局面结束的连珠线游戏中找出那个得分最大的,然后输出最大可能得分。

第一行一个正整数 $n$,表示珠子的数量。珠子从 1 到 n 编号。

接下来 n – 1 行每行三个整数 $a_{i},b_{i},c_{i}$?。保证 $1 \leqslant a_{i},b_{i} \leqslant n$,$1 \leqslant c_{i} \leqslant 10000$。表示 ai? 号珠子和 bi? 号珠子间连了长度为 ci? 的线。

输出一个整数,表示最大可能得分。

输入 #1

5
1 2 10
1 3 40
1 4 15
1 5 20

输出 #1
60

输入 #1

5
1 2 10
1 3 40
1 4 15
1 5 20

输出 #1

60

输入 #2
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
输出 #2

140

输入 #2

10

4 10 2

1 2 21

1 3 13

6 7 1

7 9 5

2 4 3

2 5 8

1 6 55

6 8 34

输出 #2

140

【样例描述1】

可以通过如下方式获得 60 分:首先从 3 号珠子开始。

5 和 3 连起来。(线长度任意)

在 3 和 5 之间插入 $11$。(线长分别为 40 和 20)。

把 2 和 1 用长度为 $10$ 的线连起来。

把 4 和 1 用长度为 $15$ 的线连起来。

【限制与约定】

第一个子任务共 13 分,满足 $1\leqslant n \leqslant 10$

第二个子任务共 15 分,满足 $1\leqslant n \leqslant 200$

第三个子任务共 29 分,满足 $1\leqslant n \leqslant 10000$

第四个子任务共 43 分,满足 $1\leqslant n \leqslant 200000$

 

Solution:

为了方便,我们把每个珠子看成一个节点

我们非常开心的发现,结果肯定是一棵边颜色不同的树

由于刚开始只有任意的一个节点,就相当于我们选择一个节点为根,

然后进行两种操作:

1.每次扩展一个节点的儿子,即把一个节点和已经在树上的一个点连上一条红边,且不在这两个点之间加珠子:

分享图片

或者扩展一个节点的孙子再扩展这个孙子的爸爸,就是把树上的一个节点连到另一个节点上,在把一个节点插到这两个节点之间,把两条边染成蓝色,即:

分享图片

分享图片

 好的,假设以1号点为根,我们可以列出一个DP方程式

f[i][0/1]表示当前节点是否为中点获得的最大价值

$f[i][0]=\sum\limits_{j}^{j \in son[i]}max(f[j][0],f[j][1]+dis(i,j));$

Leave a Comment

Your email address will not be published.