luogu#P8066. [BalkanOI 2012] Fan Groups

    ID: 7321 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2012Special JudgeBalkanOI(巴尔干半岛)

[BalkanOI 2012] Fan Groups

Background

The BOI World Cup is about to start, and fan groups are promoting the teams they support in the city.

Problem Description

There are nn city squares (labeled from 11 to nn) and mm directed streets between some of them. Initially, there is one group of fans in each square (each group supports a different team).

These nn fan groups perform the following actions in some specific order:

  • They occupy the square where they are located (for example, the group in square 11 will first occupy square 11).
  • Starting from some occupied square uu, they go to every adjacent square vv. If square vv has already been occupied by someone else, they will have a conflict on edge (u,v)(u,v) and will not continue in that direction; otherwise, they will occupy square vv and repeat this step, until there are no more reachable squares.
  • If their own starting square has already been occupied earlier by another group, they will not have a conflict with that group. In this case, they do nothing.

Given the city and the set of edges where conflicts occur, find an order of actions of the fan groups such that the set of conflict edges matches the input.

Input Format

The first line contains two integers N,MN,M, representing the number of vertices and the number of edges.

The next MM lines each contain three integers Ai,Bi,CiA_i,B_i,C_i, representing a directed edge AiBiA_i\rightarrow B_i. If Ci=1C_i=1, then a conflict occurs on this edge.

Output Format

Output one permutation, the action order of the fan groups.

If there is no valid solution, output -1.

8 9
1 4 1
1 8 1
2 3 0
5 6 0
6 5 0
7 4 1
6 4 0
7 1 1
4 5 0

8 5 6 2 3 1 7 4

Hint

Special Judge is provided by TOBapNw. If the SPJ is incorrect, please contact them.

Constraints

Subtask#0 is the sample.

3N2×1043\le N\le 2\times10^4, Mmin(N×(N1)2,2×105)M\le\min(\frac{N\times(N-1)}{2},2\times10^5), 1Ai,BiN1\le A_i,B_i\le N, Ci{0,1}C_i\in\{0,1\}.

Sample Explanation

The fans in square 88 act first and occupy square 88.

Next, the fans in square 55 act and occupy squares 4,5,64,5,6.

Next, the fans in square 66 act; since square 66 has already been occupied, they do nothing.

Next, the fans in square 22 occupy squares 2,32,3.

The fans in square 33 do nothing.

Next, the fans in square 11 act, occupy square 11, and cause conflicts on edges (1,4),(1,8)(1,4),(1,8).

The fans in square 77 occupy square 77 and cause conflicts on edges (7,1),(7,4)(7,1),(7,4).

Note that there may be more than one valid order. In the sample, (2,3,8,4,1,7,5,6)(2,3,8,4,1,7,5,6) is also a valid solution.

Note that (8,5,6,3,2,1,7,4)(8,5,6,3,2,1,7,4) is not a valid solution, because under this order, a conflict would also occur on (2,3)(2,3), which does not match the input.

Translated by ChatGPT 5