Parity Constraint Minimum Spanning Tree
Time limit1sMemory limit1024 MB
Given a weighted connected graph, find the smallest odd and smallest even total cost over all spanning trees, printing -1 when a parity is impossible.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Graph, Union-find, Greedy
- Solved
- No attempts yet
Problem
In 2019, the Yonsei University Optimization Lab suddenly became fond of solving problems with a parity constraint added. Kim Kangsan, a former lab member who has since graduated, also wrote a paper on parity constraints. To commemorate this, Kuklyeol decided to set a parity constraint problem for the Yonsei University Programming Contest.
You are given a graph with vertices and undirected edges with costs. The vertices are numbered from 1 to . Starting from any vertex, there is always a path to every other vertex, and two distinct vertices are connected by at most one edge.
A spanning tree is a subgraph of the given graph that is a tree. Find the minimum odd value and the minimum even value among the possible total costs of spanning trees.
Input
. . .
Output
Print the minimum odd total cost and the minimum even total cost among the spanning trees. If the corresponding tree does not exist, print -1.
Constraints
- 2 ≤ N ≤ 100,000.
- 1 ≤ M ≤ 300,000.
- 1 ≤ , ≤ N. ≠ . and are the endpoints of the i-th edge.
- 1 ≤ ≤ 1,000,000,000. is the cost of the i-th edge.
- All numbers in the input are integers.