CF1876E.Ball-Stackable

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

With a problem title like that, there is no way this is going to be a graph problem.

Chaneka has a graph with nn vertices and n1n-1 edges. Some of the edges are directed and some of the edges are undirected. Edge ii connects vertex uiu_i to vertex viv_i . If ti=0t_i=0 , edge ii is undirected. If ti=1t_i=1 , edge ii is directed in the direction from uiu_i to viv_i . It is known that if you make all edges undirected, the graph becomes a tree ^\dagger .

Chaneka wants to direct all undirected edges and colour each edge (different edges can have the same colour).

After doing that, suppose Chaneka starts a walk from an arbitrary vertex xx to an arbitrary vertex yy (it is possible that x=yx=y ) going through one or more edges. She is allowed to go through each edge either following the direction or opposite to the direction of the edge. She is also allowed to visit a vertex or an edge more than once. During the walk, Chaneka maintains a stack of balls that is initially empty before the walk. Each time Chaneka goes through an edge, she does the following:

  • If Chaneka goes through it in the right direction, she puts a new ball with a colour that is the same as the edge's colour to the top of the stack.
  • If Chaneka goes through it in the opposite direction, she removes the ball that is on the top of the stack.

A walk is stackable if and only if the stack is not empty before each time Chaneka goes through an edge in the opposite direction.

A walk is ball-stackable if and only if it is stackable and each time Chaneka goes through an edge in the opposite direction, the colour of the ball removed from the stack is the same as the colour of the edge traversed.

Is it possible to direct all undirected edges and colour each edge such that all stackable walks are also ball-stackable? If it is possible, find a construction example that uses the maximum number of different colours among all valid ways of directing and colouring. If there are multiple such solutions, output any of them.

^\dagger A tree is a connected graph with no cycles.

输入格式

The first line contains a single integer nn ( 2n1052\leq n\leq10^5 ) — the number of vertices in the graph.

The ii -th of the next n1n-1 lines contains three integers uiu_i , viv_i , and tit_i ( 1ui,vin1 \leq u_i,v_i \leq n ; 0ti10\leq t_i\leq1 ) — an undirected edge connecting vectices uiu_i and viv_i if ti=0t_i=0 , or a directed edge from vertex uiu_i to vertex viv_i if ti=1t_i=1 . If you make all edges undirected, the graph becomes a tree.

输出格式

A single line containing 1-1 if it is impossible.

Otherwise, the output consists of nn lines describing your construction. The first line contains an integer zz representing the number of different colours used. The ii -th of the next n1n-1 lines contains three integers pp , qq , and cc ( 1p,qn1\leq p,q\leq n ; 1cz1\leq c\leq z ) — the edge connecting vertices pp and qq in the graph is directed from vertex pp to vertex qq and is coloured with colour cc . If there are multiple such solutions, output any of them.

Note that since there should be zz different colours in your construction, that means each colour from 11 to zz must appear at least once in the graph.

输入输出样例

  • 输入#1

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

    输出#1

    3
    1 5 2
    5 4 1
    2 5 2
    3 1 3

说明/提示

The following is the given graph.

Chaneka can direct all undirected edges and colour each edge as follows.

As an example, consider a stackable walk 31525453→1→5→2→5→4→5 . Let's show that that walk is ball-stackable.

  1. Chaneka starts in vertex 33 . The stack is [][] .
  2. Chaneka moves to vertex 11 . She puts a ball of colour 33 . The stack is [3][3] .
  3. Chaneka moves to vertex 55 . She puts a ball of colour 22 . The stack is [3,2][3,2] .
  4. Chaneka moves to vertex 22 . She removes a ball of colour 22 (same colour as the edge). The stack is [3][3] .
  5. Chaneka moves to vertex 55 . She puts a ball of colour 22 . The stack is [3,2][3,2] .
  6. Chaneka moves to vertex 44 . She puts a ball of colour 11 . The stack is [3,2,1][3,2,1] .
  7. Chaneka moves to vertex 55 . She removes a ball of colour 11 (same colour as the edge). The stack is [3,2][3,2] .

Since every time Chaneka removes a ball from the stack, it has the same colour as the edge traversed, then the walk above is ball-stackable. It can be shown that if we direct and colour the edges as shown above, any possible stackable walk is also ball-stackable.

首页