#P3623. [APIO2008] 免费道路

    ID: 1801 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2008并查集APIOSpecial Judge背包生成树

[APIO2008] 免费道路

Description

The kingdom of New Asia has NN villages connected by MM roads. Some roads are cobblestone, while others are cement. Keeping roads toll-free requires a large expense, and it seems impossible for the kingdom to keep all roads free. Therefore, a new road maintenance plan is urgently needed.

The king has decided to keep as few roads free as possible, but there must be exactly one path consisting of free roads between any two distinct villages. At the same time, although cement roads are better suited to modern traffic, the king also thinks that walking along cobblestone roads is interesting. So the king decides to keep exactly KK cobblestone roads free.

For example, suppose the villages and roads in New Asia are as shown in Figure 3(a). If the king wants to keep two cobblestone roads free, one possible plan is to keep roads (1,2)(1, 2), (2,3)(2, 3), (3,4)(3, 4), and (3,5)(3, 5) free, as shown in Figure 3(b). This plan satisfies the king’s requirements because:

  1. There is a path consisting of free roads between any two villages.
  2. The number of free roads is as small as possible.
  3. There are exactly two cobblestone roads, (2,3)(2, 3) and (3,4)(3, 4), in the plan.

Figure 3: (a) An example of villages and roads in New Asia. Solid lines denote cement roads, and dashed lines denote cobblestone roads. (b) A maintenance plan that keeps two cobblestone roads free. Only the free roads are shown.

Given a description of the villages and roads in New Asia and the number of cobblestone roads the king wants to keep free, write a program to determine whether there exists a road maintenance plan that satisfies the king’s requirements. If it exists, output any one such plan.

Input Format

The first line of input contains three integers separated by spaces:

NN, the number of villages (1N2×104)(1 \le N \le 2 \times 10^4);

MM, the number of roads (1M105)(1 \le M \le 10^5);

KK, the number of cobblestone roads the king wants to keep free (0KN1)(0 \le K \le N - 1).

The following MM lines describe the roads in New Asia, numbered from 11 to MM. The (i+1)(i+1)-th line describes the ii-th road with 3 space-separated integers:

uiu_i and viv_i, the indices of the two villages connected by the ii-th road, where villages are numbered from 11 to NN;

cic_i, indicating the type of the ii-th road. ci=0c_i = 0 means the ii-th road is a cobblestone road, and ci=1c_i = 1 means the ii-th road is a cement road.

It is guaranteed that between any pair of villages there is at most one connecting road.

Output Format

If no road maintenance plan satisfies the king’s requirements, your program should print no solution on the first line. Otherwise, your program should output a valid road maintenance plan, i.e., the list of roads to remain free. Output the free roads in the same way as they are given in the input. If there are multiple valid plans, you may output any one of them.

5 7 2 
1 3 0 
4 5 1 
3 2 0 
5 3 1 
4 3 0 
1 2 1 
4 2 1
3 2 0 
4 3 0 
5 3 1 
1 2 1 

Hint

Constraints

For 100%100\% of the testdata, it is guaranteed that 1N2×1041 \le N \le 2 \times 10^4, 1M1051 \le M \le 10^5, 0KN10 \le K \le N - 1.

Translated by ChatGPT 5