Parity Constraint Perfect Matching
Time limit1sMemory limit1024 MB
Given a weighted bipartite graph, find one perfect matching with an even weight sum and one with an odd weight sum, or print -1 when none exists.
Problem
Gukryeol wants to set one more Parity Constraint problem out of respect for a graduated senior.
A bipartite graph is given with vertices on the left, vertices on the right, and weighted edges. Vertices on each side are numbered 1 to , and there is at most one edge between any pair of vertices.
A Perfect Matching is a set of edges that share no endpoints. Find one Perfect Matching whose weight sum is even and one whose weight sum is odd.
Input
The first line contains and . (, )
Each of the next lines contains in order. is the left vertex number, is the right vertex number, and is the edge weight. (, ) No two vertices are connected by more than one edge.
Output
Print lines with the edges of a Perfect Matching whose weight sum is even, giving the left and right endpoint numbers in that order. If no such matching exists, print -1.
From the second line on, print the same way for a Perfect Matching whose weight sum is odd. If none exists, print -1.
The weight sum does not need to be minimal, and the edges may be printed in any order. If several answers exist, print any of them.