310734: CF1877G. Ball-Stackable

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

G. Ball-Stackabletime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

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

Chaneka has a graph with $n$ vertices and $n-1$ edges. Some of the edges are directed and some of the edges are undirected. Edge $i$ connects vertex $u_i$ to vertex $v_i$. If $t_i=0$, edge $i$ is undirected. If $t_i=1$, edge $i$ is directed in the direction from $u_i$ to $v_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 $x$ to an arbitrary vertex $y$ (it is possible that $x=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.

Input

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

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

Output

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

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

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

ExampleInput
5
2 5 1
1 3 0
5 4 0
1 5 1
Output
3
1 5 2
5 4 1
2 5 2
3 1 3
Note

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 $3→1→5→2→5→4→5$. Let's show that that walk is ball-stackable.

  1. Chaneka starts in vertex $3$. The stack is $[]$.
  2. Chaneka moves to vertex $1$. She puts a ball of colour $3$. The stack is $[3]$.
  3. Chaneka moves to vertex $5$. She puts a ball of colour $2$. The stack is $[3,2]$.
  4. Chaneka moves to vertex $2$. She removes a ball of colour $2$ (same colour as the edge). The stack is $[3]$.
  5. Chaneka moves to vertex $5$. She puts a ball of colour $2$. The stack is $[3,2]$.
  6. Chaneka moves to vertex $4$. She puts a ball of colour $1$. The stack is $[3,2,1]$.
  7. Chaneka moves to vertex $5$. She removes a ball of colour $1$ (same colour as the edge). The stack is $[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.

Output

题目大意:

题目描述了一个图论问题,给定一个有 n 个顶点和 n-1 条边的图,其中一些边是有向的,一些边是无向的。每条边连接两个顶点,如果边的类型为 0,则表示该边是无向的;如果为 1,则表示该边是有向的。已知如果将所有边都看作无向的,那么这个图将变成一棵树。

题目要求将所有无向边都改为有向边,并对每条边进行着色。定义一种“可堆叠”的路径,要求在路径上每经过一条边时,如果沿着边的方向前进,就在堆栈顶部放置一个与边同色的球;如果逆着边的方向前进,就从堆栈顶部移除一个球。一个路径是“可堆叠”的,当且仅当在每次逆着边方向前进时,堆栈都不为空。一个路径是“球堆叠”的,当且仅当它是“可堆叠”的,并且在每次逆着边方向前进时,从堆栈顶部移除的球的颜色与边的颜色相同。

问题要求判断是否可以将所有无向边都改为有向边,并着色,使得所有“可堆叠”的路径都是“球堆叠”的。如果可以,需要找到一个着色方案,使得使用的不同颜色的数量在所有有效的方案中是最多的。如果存在多个这样的方案,输出其中任意一个。

输入输出数据格式:

输入:
- 第一行包含一个整数 n(2≤n≤10^5),表示顶点的数量。
- 接下来的 n-1 行,每行包含三个整数 u_i, v_i, 和 t_i(1≤u_i,v_i≤n;0≤t_i≤1),表示一条边的信息。如果 t_i=0,则表示边是无向的,连接顶点 u_i 和 v_i;如果 t_i=1,则表示边是有向的,从顶点 u_i 指向顶点 v_i。

输出:
- 如果不存在满足条件的方案,输出一行包含 -1。
- 如果存在满足条件的方案,输出 n 行描述构造方案:
- 第一行包含一个整数 z,表示使用的不同颜色的数量。
- 接下来的 n-1 行,每行包含三个整数 p, q, 和 c(1≤p,q≤n;1≤c≤z),表示边连接顶点 p 和 q,并且是从顶点 p 到顶点 q 的有向边,边的颜色为 c。每个颜色 c 都至少在图中出现一次。

示例输入输出见原文。题目大意: 题目描述了一个图论问题,给定一个有 n 个顶点和 n-1 条边的图,其中一些边是有向的,一些边是无向的。每条边连接两个顶点,如果边的类型为 0,则表示该边是无向的;如果为 1,则表示该边是有向的。已知如果将所有边都看作无向的,那么这个图将变成一棵树。 题目要求将所有无向边都改为有向边,并对每条边进行着色。定义一种“可堆叠”的路径,要求在路径上每经过一条边时,如果沿着边的方向前进,就在堆栈顶部放置一个与边同色的球;如果逆着边的方向前进,就从堆栈顶部移除一个球。一个路径是“可堆叠”的,当且仅当在每次逆着边方向前进时,堆栈都不为空。一个路径是“球堆叠”的,当且仅当它是“可堆叠”的,并且在每次逆着边方向前进时,从堆栈顶部移除的球的颜色与边的颜色相同。 问题要求判断是否可以将所有无向边都改为有向边,并着色,使得所有“可堆叠”的路径都是“球堆叠”的。如果可以,需要找到一个着色方案,使得使用的不同颜色的数量在所有有效的方案中是最多的。如果存在多个这样的方案,输出其中任意一个。 输入输出数据格式: 输入: - 第一行包含一个整数 n(2≤n≤10^5),表示顶点的数量。 - 接下来的 n-1 行,每行包含三个整数 u_i, v_i, 和 t_i(1≤u_i,v_i≤n;0≤t_i≤1),表示一条边的信息。如果 t_i=0,则表示边是无向的,连接顶点 u_i 和 v_i;如果 t_i=1,则表示边是有向的,从顶点 u_i 指向顶点 v_i。 输出: - 如果不存在满足条件的方案,输出一行包含 -1。 - 如果存在满足条件的方案,输出 n 行描述构造方案: - 第一行包含一个整数 z,表示使用的不同颜色的数量。 - 接下来的 n-1 行,每行包含三个整数 p, q, 和 c(1≤p,q≤n;1≤c≤z),表示边连接顶点 p 和 q,并且是从顶点 p 到顶点 q 的有向边,边的颜色为 c。每个颜色 c 都至少在图中出现一次。 示例输入输出见原文。

加入题单

上一题 下一题 算法标签: