AtCoder Regular Contest 225
C
K 生成树
K Spanning Tree
AtCoder
⌀ 1733
原题
← BD →

K 生成树AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given a simple connected undirected graph $G$ with $N$ vertices and $M$ edges. Edge $i$ connects vertices $a_i$ and $b_i$, and has weight $c_i$. Here, each edge's weight is $0$ or $1$.

给定一个有 $N$ 个顶点和 $M$ 条边的简单连通无向图 $G$。边 $i$ 连接顶点 $a_i$ 和 $b_i$,权值为 $c_i$。其中,每条边的权值均为 $0$ 或 $1$。AI (CI翻译)

You are given a non-negative integer $K$. Determine whether there exists a spanning tree of $G$ such that the sum of the weights of the edges composing the spanning tree is exactly $K$, and if one exists, find one.

给定一个非负整数 $K$。请判断是否存在一棵 $G$ 的生成树,使得组成该生成树的边的权值之和恰好为 $K$;如果存在,请找出这样一棵生成树。AI (CI翻译)

You are given $T$ test cases; solve each of them.

给定 $T$ 组测试用例,请分别求解每组测试用例。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1 \le T \le 10^5$
  • $2 \le N \le 2\times 10^5$
  • $N-1 \le M \le \min\left(\frac{N(N-1)}{2}, 2\times 10^5\right)$
  • $0 \le K \le N-1$
  • $1 \le a_i,b_i \le N$
  • $c_i=0$ or $c_i=1$.
  • The graph $G$ is simple and connected.
  • The sum of $N$ over all test cases is at most $2\times 10^5$.
  • The sum of $M$ over all test cases is at most $2\times 10^5$.
  • All input values are integers.
  • $1 \le T \le 10^5$
  • $2 \le N \le 2\times 10^5$
  • $N-1 \le M \le \min\left(\frac{N(N-1)}{2}, 2\times 10^5\right)$
  • $0 \le K \le N-1$
  • $1 \le a_i,b_i \le N$
  • $c_i=0$ 或 $c_i=1$。
  • 图 $G$ 是简单连通图。
  • 所有测试用例的 $N$ 之和不超过 $2\times 10^5$。
  • 所有测试用例的 $M$ 之和不超过 $2\times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

The input is given from Standard Input in the following format:

输入从标准输入给出,格式如下:AI (CI翻译)

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
AI (CI翻译)

Each case is given in the following format:

每组测试用例的格式如下:AI (CI翻译)

N M K
a_1 b_1 c_1
a_2 b_2 c_2
\vdots
a_M b_M c_M

N M K
a_1 b_1 c_1
a_2 b_2 c_2
\vdots
a_M b_M c_M
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output $T$ lines. The $i$-th line should contain the answer for $\text{case}_i$.

输出 $T$ 行。第 $i$ 行应包含第 $i$ 组测试用例的答案。AI (CI翻译)

If there is no spanning tree satisfying the condition, output -1.

如果不存在满足条件的生成树,输出 -1。AI (CI翻译)

Otherwise, let $x_1,x_2,\cdots,x_{N-1}$ be the numbers of the edges composing a spanning tree for that case, and output them in the following format:

否则,设 $x_1,x_2,\cdots,x_{N-1}$ 为该测试用例中组成一棵生成树的边的编号,并按以下格式输出:AI (CI翻译)

x_1 x_2 \cdots x_{N-1}

x_1 x_2 \cdots x_{N-1}
AI (CI翻译)

$x_1,x_2,\cdots,x_{N-1}$ can be in any order. The edge numbers correspond to the input order within each case. If multiple solutions exist, any of them is accepted.

$x_1,x_2,\cdots,x_{N-1}$ 可以按任意顺序排列。边的编号对应于每组测试用例中的输入顺序。如果存在多个解,输出其中任意一个均可。AI (CI翻译)

样例

Input #1
2
4 5 2
1 2 0
1 3 0
2 3 0
4 2 1
4 3 1
4 4 1
1 2 1
2 3 1
2 4 1
3 4 0
Output #1
1 4 5
-1
For the first case, the graph composed of edges $1,4,5$ is a spanning tree of $G$, and the sum of its edge weights is $2$, so it satisfies the condition. Outputs such as `2 4 5` and `4 1 5` are also accepted. For the second case, the sum of the edge weights of a spanning tree of graph $G$ cannot be $1$.
题目信息
题号
arc225_c
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。