An Animal Contest 1 P5 - Odd Alpacas

View as PDF

Submit solution


Points: 12 (partial)
Time limit: 2.0s
Memory limit: 512M

Authors:
Problem type

It is a well-known fact that alpacas love numbers. The reason behind this fascination is uncertain, but that shouldn't be your concern at the moment. You've stumbled upon a herd of alpacas living in N connected villages, joined by N1 bidirectional roads of length wi.

Let us define x as the number of even length pathways between any two villages and y as the number of odd length pathways between any two villages. The length of a pathway between ui and vi is defined as the sum of the road lengths connecting ui and vi.

You look up and find yourself face to face with the queen alpaca and she's angry. She orders you to tell her the minimum value of |xy| given that you can change the length of at most 1 road. Fail to do so and you'll be turned into an alpaca. Can you make it out alive?

Constraints

1N2105

1wi104

1ui,viN

Subtask 1 [10%]

1N200

Subtask 2 [20%]

1N2103

Subtask 3 [70%]

No additional constraints.

Input Specification

The first line of input will contain the integer N, the number of villages.

The next N1 lines will contain the integers ui, vi, and wi, representing a bidirectional road between ui and vi that is wi units long.

Output Specification

Output the minimal possible value of |xy| after modifying the length of at most one edge.

Note that 64-bit integers may be required.

Sample Input 1

Copy
5
5 3 97
4 2 21
1 2 49
3 2 4

Sample Output 1

Copy
2

Explanation for Sample Output 1

An optimal solution is not changing any lengths.

In the original graph, there are 6 pairs of villages which form a path of odd length:

  • (1,2) of length 49.
  • (1,3) of length 53.
  • (2,4) of length 21.
  • (2,5) of length 101.
  • (3,4) of length 25.
  • (3,5) of length 97.

Furthermore, there are 4 pairs of villages which form a path of even length.

  • (1,4) of length 70.
  • (1,5) of length 150.
  • (2,3) of length 4.
  • (4,5) of length 122.

Thus, the answer is |64|=2.

Sample Input 2

Copy
6
6 1 100
2 1 86
4 3 12
3 2 40
5 2 44

Sample Output 2

Copy
1

Comments


  • 3
    JPEGraid  commented on Nov. 15, 2024, 7:16 p.m.

    getting turned into an alpaca doesn't sound so bad :)