310028: CF1773J. Jumbled Trees

Memory Limit:1024 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Jumbled Trees

题意翻译

你在一个无向图中选择若干生成树,并给每棵生成树的所有边的边权增加一个值 $v$,使得每条边的边权都能够达到它的目标值 $x_i$(对质数 $p$ 取模)。

题目描述

You are given an undirected connected graph with $ n $ vertices and $ m $ edges. Each edge has an associated counter, initially equal to $ 0 $ . In one operation, you can choose an arbitrary spanning tree and add any value $ v $ to all edges of this spanning tree. Determine if it's possible to make every counter equal to its target value $ x_i $ modulo prime $ p $ , and provide a sequence of operations that achieves it.

输入输出格式

输入格式


The first line contains three integers $ n $ , $ m $ , and $ p $ — the number of vertices, the number of edges, and the prime modulus ( $ 1 \le n \le 500 $ ; $ 1 \le m \le 1000 $ ; $ 2 \le p \le 10^9 $ , $ p $ is prime). Next $ m $ lines contain three integers $ u_i $ , $ v_i $ , $ x_i $ each — the two endpoints of the $ i $ -th edge and the target value of that edge's counter ( $ 1 \le u_i, v_i \le n $ ; $ 0 \le x_i < p $ ; $ u_i \neq v_i $ ). The graph is connected. There are no loops, but there may be multiple edges between the same two vertices.

输出格式


If the target values on counters cannot be achieved, print -1. Otherwise, print $ t $ — the number of operations, followed by $ t $ lines, describing the sequence of operations. Each line starts with integer $ v $ ( $ 0 \le v < p $ ) — the counter increment for this operation. Then, in the same line, followed by $ n - 1 $ integers $ e_1 $ , $ e_2 $ , ... $ e_{n - 1} $ ( $ 1 \le e_i \le m $ ) — the edges of the spanning tree. The number of operations $ t $ should not exceed $ 2m $ . You don't need to minimize $ t $ . Any correct answer within the $ 2m $ bound is accepted. You are allowed to repeat spanning trees.

输入输出样例

输入样例 #1

3 3 101
1 2 30
2 3 40
3 1 50

输出样例 #1

3
10 1 2
20 1 3
30 2 3

输入样例 #2

2 2 37
1 2 8
1 2 15

输出样例 #2

2
8 1
15 2

输入样例 #3

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

输出样例 #3

-1

Input

题意翻译

你在一个无向图中选择若干生成树,并给每棵生成树的所有边的边权增加一个值 $v$,使得每条边的边权都能够达到它的目标值 $x_i$(对质数 $p$ 取模)。

Output

**标题:** 混乱的树

**题意翻译:**
你在无向图中选择一些生成树,并对每棵生成树的所有边的边权增加一个值 $v$,使得每条边的边权都能达到它的目标值 $x_i$(对质数 $p$ 取模)。

**题目描述:**
给你一个无向连通图,包含 $n$ 个顶点和 $m$ 条边。每条边关联一个计数器,初始值为 $0$。你可以选择任意生成树,并将任何值 $v$ 添加到此生成树的所有边上。

确定是否能使每个计数器等于它的目标值 $x_i$ 模质数 $p$,并提供一系列操作来实现它。

**输入输出格式:**

**输入格式:**
第一行包含三个整数 $n$、$m$ 和 $p$ —— 顶点数、边数和质数模数($1 \le n \le 500$;$1 \le m \le 1000$;$2 \le p \le 10^9$,$p$ 为质数)。

接下来 $m$ 行,每行三个整数 $u_i$、$v_i$、$x_i$ —— 第 $i$ 条边的两个端点和该边计数器的目标值($1 \le u_i, v_i \le n$;$0 \le x_i < p$;$u_i \neq v_i$)。

图是连通的。没有自环,但同一对顶点之间可能有多条边。

**输出格式:**
如果无法实现计数器的目标值,打印 -1。

否则,打印 $t$ —— 操作次数,然后是 $t$ 行,描述操作序列。每行以整数 $v$($0 \le v < p$)开始 —— 本次操作的计数器增量。然后是 $n - 1$ 个整数 $e_1$、$e_2$、...、$e_{n - 1}$($1 \le e_i \le m$)—— 生成树的边。

操作次数 $t$ 不应超过 $2m$。你不需要最小化 $t$。任何在 $2m$ 范围内的正确答案都是可接受的。你可以重复使用生成树。

**输入输出样例:**

**输入样例 #1**
```
3 3 101
1 2 30
2 3 40
3 1 50
```
**输出样例 #1**
```
3
10 1 2
20 1 3
30 2 3
```

**输入样例 #2**
```
2 2 37
1 2 8
1 2 15
```
**输出样例 #2**
```
2
8 1
15 2
```

**输入样例 #3**
```
5 4 5
1 3 1
2 3 2
2 5 3
4 1 4
```
**输出样例 #3**
```
-1
```**标题:** 混乱的树 **题意翻译:** 你在无向图中选择一些生成树,并对每棵生成树的所有边的边权增加一个值 $v$,使得每条边的边权都能达到它的目标值 $x_i$(对质数 $p$ 取模)。 **题目描述:** 给你一个无向连通图,包含 $n$ 个顶点和 $m$ 条边。每条边关联一个计数器,初始值为 $0$。你可以选择任意生成树,并将任何值 $v$ 添加到此生成树的所有边上。 确定是否能使每个计数器等于它的目标值 $x_i$ 模质数 $p$,并提供一系列操作来实现它。 **输入输出格式:** **输入格式:** 第一行包含三个整数 $n$、$m$ 和 $p$ —— 顶点数、边数和质数模数($1 \le n \le 500$;$1 \le m \le 1000$;$2 \le p \le 10^9$,$p$ 为质数)。 接下来 $m$ 行,每行三个整数 $u_i$、$v_i$、$x_i$ —— 第 $i$ 条边的两个端点和该边计数器的目标值($1 \le u_i, v_i \le n$;$0 \le x_i < p$;$u_i \neq v_i$)。 图是连通的。没有自环,但同一对顶点之间可能有多条边。 **输出格式:** 如果无法实现计数器的目标值,打印 -1。 否则,打印 $t$ —— 操作次数,然后是 $t$ 行,描述操作序列。每行以整数 $v$($0 \le v < p$)开始 —— 本次操作的计数器增量。然后是 $n - 1$ 个整数 $e_1$、$e_2$、...、$e_{n - 1}$($1 \le e_i \le m$)—— 生成树的边。 操作次数 $t$ 不应超过 $2m$。你不需要最小化 $t$。任何在 $2m$ 范围内的正确答案都是可接受的。你可以重复使用生成树。 **输入输出样例:** **输入样例 #1** ``` 3 3 101 1 2 30 2 3 40 3 1 50 ``` **输出样例 #1** ``` 3 10 1 2 20 1 3 30 2 3 ``` **输入样例 #2** ``` 2 2 37 1 2 8 1 2 15 ``` **输出样例 #2** ``` 2 8 1 15 2 ``` **输入样例 #3** ``` 5 4 5 1 3 1 2 3 2 2 5 3 4 1 4 ``` **输出样例 #3** ``` -1 ```

加入题单

算法标签: