W
points and W-1
connections between points. Every point is connected together. Each connection has length of 1
. Computer program can add value 1
or 2
to some points.1
to point A
, then every points with is connected to A
(including point A
) will be painted red2
, then every point with is connected to some point with is connected to A
and every point connected to A
and point A
will be painted red. I need to paint all points. so.W
. In next W-1
lines i write pairs of numbers with are connected points.For example
I write
6
1 2
2 3
3 4
3 5
3 6
The result is 2
So, if I understood this right, you have:
Some points/nodes, and connections in the form start->end.
You want the minimal sum possible with all nodes marked, and
1 being added for each node and it's immediate children are marked
2 being added for each node and the whole tree below it are marked
There are n-1 connections for n nodes (that means it has to be a cycle-free tree with a root)
Right?
First find the root: The node which is not end of any connection, ie. 1 in your example.
Then check if any other connection has not the root as start.
If yes, the answer is 2, else 1. There are no other possible values.
Time complexity O(n).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.