AT_arc225_c.K Spanning Tree
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a simple connected undirected graph G with N vertices and M edges. Edge i connects vertices ai and bi, and has weight ci. Here, each edge's weight is 0 or 1.
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.
You are given T test cases; solve each of them.
给你一个包含 N 个顶点和 M 条边的简单连通无向图 G。第 i 条边连接顶点 ai 和 bi,其权重为 ci。其中,每条边的权重为 0 或 1。
再给你一个非负整数 K。请判断图 G 是否存在一棵生成树,使得该生成树中所有边的权重之和恰好等于 K;若存在,请找出这样的一棵生成树。
你将收到 T 组测试数据,请对每组数据分别求解。
输入格式
The input is given from Standard Input in the following format:
T
case1
case2
⋮
caseT
Each case is given in the following format:
N M K
a1 b1 c1
a2 b2 c2
⋮
aM bM cM
输入从标准输入中以如下格式给出:
T
case1
case2
⋮
caseT
每个测试用例以如下格式给出:
N M K
a1 b1 c1
a2 b2 c2
⋮
aM bM cM
输出格式
Output T lines. The i-th line should contain the answer for casei.
If there is no spanning tree satisfying the condition, output -1.
Otherwise, let x1,x2,⋯,xN−1 be the numbers of the edges composing a spanning tree for that case, and output them in the following format:
x1 x2 ⋯ xN−1
x1,x2,⋯,xN−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.
输出 T 行。第 i 行应为第 i 个测试用例的答案。
若不存在满足条件的生成树,则输出 -1。
否则,设 x1,x2,⋯,xN−1 为构成该测试用例中某棵生成树的各条边的编号,并按如下格式输出:
x1 x2 ⋯ xN−1
x1,x2,⋯,xN−1 的顺序可任意。边的编号对应于每个测试用例中输入的边的顺序。若存在多个可行解,输出其中任意一个即可。
输入输出样例
输入#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
输出#1
1 4 5 -1
说明/提示
Sample 1 Explanation:
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.
Constraints
- 1≤T≤105
- 2≤N≤2×105
- N−1≤M≤min(2N(N−1),2×105)
- 0≤K≤N−1
- 1≤ai,bi≤N
- ci=0 or ci=1.
- The graph G is simple and connected.
- The sum of N over all test cases is at most 2×105.
- The sum of M over all test cases is at most 2×105.
- All input values are integers.
样例 1 解释:
对于第一组测试数据,由边 1,4,5 构成的图是图 G 的一棵生成树,其边权之和为 2,因此满足条件。
输出如 2 4 5 和 4 1 5 也同样被接受。
对于第二组测试数据,图 G 的任意一棵生成树的边权之和都不可能为 1。
约束条件
- 1≤T≤105
- 2≤N≤2×105
- N−1≤M≤min(2N(N−1),2×105)
- 0≤K≤N−1
- 1≤ai,bi≤N
- ci=0 或 ci=1。
- 图 G 是简单图且连通。
- 所有测试数据中 N 的总和不超过 2×105。
- 所有测试数据中 M 的总和不超过 2×105。
- 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?