Journey through Colors

시간 제한1초메모리 제한1024 MB

요약
모든 도로를 한 번씩 지나고 연속한 두 도로의 색이 다르며 처음과 마지막 도로의 색도 다른 오일러 회로를 찾는다.
난이도

어려움10점 중 8점

유형
그래프, DFS, 구현, 그리디
정답자
아직 제출이 없습니다

문제

In the land of Oz, roads are paved with colored stones. Each road connects exactly two cities, can be traveled in both directions, and is colored with stones of a single color.

Dorothy is visiting Oz for the first time and wants to take a tour of the country, meeting the following conditions:

  • The tour must start and end in the same city.
  • The tour must pass through each road in the country exactly once and cannot use two consecutive roads (i.e., one immediately after the other) that have the same color.
  • The first and last roads of the tour must have different colors.

Figure (a) below illustrates an example with five cities and six roads. Figure (b) shows a possible tour that starts and ends in city 22 and satisfies the road color restrictions. In figure (b), the tour starts in city 22 and goes, in sequence, through roads 11 (red), 33 (green), 44 (blue), 22 (red), 66 (blue) and, finally, 55 (green).

Help Dorothy find such a tour or, if it is not possible, indicate that it does not exist.

입력

The first line of the input contains three integers, NN, MM, and KK, representing the number of cities (2≤N≤10002 ≤ N ≤ 1000), the number of roads (1≤M≤10001 ≤ M ≤ 1000), and the number of colors (1≤K≤10001 ≤ K ≤ 1000), respectively. Cities are identified by integers from 11 to NN, roads are identified by integers from 11 to MM, and colors are identified by integers from 11 to KK. Each of the following MM lines describes a road and contains three integers II, JJ, and CC, where II and JJ represent cities (1≤I,J≤N1 ≤ I, J ≤ N, and I≠JI \ne J), and CC indicates the color of road 1≤C≤K1 ≤ C ≤ K. The roads are given in the order of their identification, that is, the first road in the input is number 11, the second road is number 22, and so on.

출력

If there is no tour that satisfies the constraints, print a single integer -1. Otherwise, your program should output two lines describing a valid tour. The first line should contain the identifier of the starting city of the tour. The second line should contain MM distinct integers, each identifying a road, in tour order. If there is more than one possible tour, print any one of them.

예제5

  1. 예제 1

    입력
    5 6 3
    1 2 1
    2 3 1
    1 4 2
    2 4 3
    2 5 2
    3 5 3
    
    예상 출력
    2
    1 3 4 2 6 5
    
  2. 예제 2

    입력
    2 4 2
    1 2 1
    1 2 1
    1 2 2
    1 2 2
    
    예상 출력
    1
    1 3 2 4
    
  3. 예제 3

    입력
    6 6 3
    1 2 1
    2 3 2
    3 1 3
    4 5 1
    5 6 2
    6 4 3
    
    예상 출력
    -1
    
  4. 예제 4

    입력
    3 2 2
    1 2 1
    1 2 2
    
    예상 출력
    1
    1 2
    
  5. 예제 5

    입력
    3 3 1
    1 2 1
    2 3 1
    3 1 1
    
    예상 출력
    -1