Magical Plants

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

요약
식물이 임계 조건에 따라 하루에 1미터씩 자랄 때, 모든 식물이 K미터가 되는 최소 일수와 그 식재 순서를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 그래프, 위상 정렬, 이분 탐색
정답자
아직 제출이 없습니다

문제

John is growing plants on the windowsill. These are no ordinary plants: they are in telepathic communication with one another and will grow only if some other plants are already tall enough.

There are NN pots on the windowsill, numbered 1…N1 \ldots N. Initially there is nothing planted in any of the pots. Also, there are MM constraints of the form "the plant in pot U_iU\_i can grow A_iA\_i metres tall only if the plant in V_iV\_i is already at least B_iB\_i metres tall".

Days consist of N+1N + 1 minutes. Each day, the following happens:

  1. On the ii-th minute (for every 1≤i≤N1 \le i \le N): if there is a plant growing in the ii-th pot, it will grow 1 metre taller unless that would violate one of the constraints.
  2. On the N+1N + 1-st minute: John can choose a pot with no plant in it, and plant a plant there. Initially, a plant is 1 metre tall.

We need each plant to be at least KK metres tall to brew a potion. Find the minimum number of days necessary, assuming John plants the plants optimally. Find one optimal way to plant the plants.

It is guaranteed that in all test cases it is possible to plant the plants so that all plants will grow KK metres tall in at most 101810^{18} days.

입력

The first line of the input consists of three space-separated integers NN, MM and KK (1≤N,M≤2⋅1051 \le N, M \le 2 \cdot 10^5, 2≤K≤1092 \le K \le 10^9).

The next MM lines describe the constraints. The ii-th such row consists of four integers U_iU\_i, A_iA\_i, V_iV\_i, B_iB\_i (1≤U_i,V_i≤N1 \le U\_i, V\_i \le N, U_i≠V_iU\_i \ne V\_i, 2≤A_i,B_i≤K2 \le A\_i, B\_i \le K), describing a constraint.

출력

The first line of the output must consist of the minimum number of days needed for all plants to grow at least KK metres tall.

The second line must consist of NN integers, each from the interval 1…1091 \ldots 10^9. Of those, the ii-th should be the day John plants the ii-th plant.

If there are multiple optimal solutions, you can print any one of them.

예제2

  1. 예제 1

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

    입력
    5 4 1000000000
    1 2 2 1000000000
    2 2 3 1000000000
    3 2 4 1000000000
    4 2 5 1000000000
    
    예상 출력
    4999999996
    5 4 3 2 1