Under construction

Still being built. Things may change

Parity Constraint Perfect Matching

Time limit1sMemory limit1024 MB

Summary
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.
Level

Hard9 of 10

Topics
Graph, Matrix, Math
Solved
No attempts yet

Problem

Gukryeol wants to set one more Parity Constraint problem out of respect for a graduated senior.

A bipartite graph is given with NN vertices on the left, NN vertices on the right, and MM weighted edges. Vertices on each side are numbered 1 to NN, and there is at most one edge between any pair of vertices.

A Perfect Matching is a set of NN 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 NN and MM. (1≤N≤5001 \le N \le 500, 1≤M≤N21 \le M \le N^2)

Each of the next MM lines contains (x,y,c)(x, y, c) in order. xx is the left vertex number, yy is the right vertex number, and cc is the edge weight. (1≤x,y≤N1 \le x, y \le N, 1≤c≤1091 \le c \le 10^9) No two vertices are connected by more than one edge.

Output

Print NN 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.

Examples3

  1. Example 1

    Input
    3 5
    1 1 2
    2 2 2
    3 3 4
    2 3 1
    3 2 2
    
    Expected output
    1 1
    2 2
    3 3
    1 1
    2 3
    3 2
    
  2. Example 2

    Input
    3 5
    1 1 2
    2 2 2
    3 3 4
    2 3 1
    3 2 1
    
    Expected output
    1 1
    2 2
    3 3
    -1
    
  3. Example 3

    Input
    3 5
    1 1 2
    2 2 1
    3 3 4
    2 3 1
    3 2 2
    
    Expected output
    -1
    1 1
    2 2
    3 3