AT_arc225_c.K Spanning Tree

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a simple connected undirected graph GG with NN vertices and MM edges. Edge ii connects vertices aia_i and bib_i, and has weight cic_i. Here, each edge's weight is 00 or 11.

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

You are given TT test cases; solve each of them.

给你一个包含 NN 个顶点和 MM 条边的简单连通无向图 GG。第 ii 条边连接顶点 aia_i 和 bib_i,其权重为 cic_i。其中,每条边的权重为 00 或 11。

再给你一个非负整数 KK。请判断图 GG 是否存在一棵生成树,使得该生成树中所有边的权重之和恰好等于 KK;若存在,请找出这样的一棵生成树。

你将收到 TT 组测试数据,请对每组数据分别求解。

输入格式

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

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

Each case is given in the following format:

NN MM KK
a1a_1 b1b_1 c1c_1
a2a_2 b2b_2 c2c_2
⋮\vdots
aMa_M bMb_M cMc_M

输入从标准输入中以如下格式给出:

TT
case1\text{case}_1
case2\text{case}_2
⋮\vdots
caseT\text{case}_T

每个测试用例以如下格式给出:

NN MM KK
a1a_1 b1b_1 c1c_1
a2a_2 b2b_2 c2c_2
⋮\vdots
aMa_M bMb_M cMc_M

输出格式

Output TT lines. The ii-th line should contain the answer for casei\text{case}_i.

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

Otherwise, let x1,x2,⋯ ,xN−1x_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:

x1x_1 x2x_2 ⋯\cdots xN−1x_{N-1}

x1,x2,⋯ ,xN−1x_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.

输出 TT 行。第 ii 行应为第 ii 个测试用例的答案。

若不存在满足条件的生成树,则输出 -1。

否则,设 x1,x2,⋯ ,xN−1x_1,x_2,\cdots,x_{N-1} 为构成该测试用例中某棵生成树的各条边的编号,并按如下格式输出:

x1x_1 x2x_2 ⋯\cdots xN−1x_{N-1}

x1,x2,⋯ ,xN−1x_1,x_2,\cdots,x_{N-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,51,4,5 is a spanning tree of GG, and the sum of its edge weights is 22, 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 GG cannot be 11.

Constraints

  • 1≤T≤1051 \le T \le 10^5
  • 2≤N≤2×1052 \le N \le 2\times 10^5
  • N−1≤M≤min⁡(N(N−1)2,2×105)N-1 \le M \le \min\left(\frac{N(N-1)}{2}, 2\times 10^5\right)
  • 0≤K≤N−10 \le K \le N-1
  • 1≤ai,bi≤N1 \le a_i,b_i \le N
  • ci=0c_i=0 or ci=1c_i=1.
  • The graph GG is simple and connected.
  • The sum of NN over all test cases is at most 2×1052\times 10^5.
  • The sum of MM over all test cases is at most 2×1052\times 10^5.
  • All input values are integers.

样例 1 解释:
对于第一组测试数据,由边 1,4,51,4,5 构成的图是图 GG 的一棵生成树,其边权之和为 22,因此满足条件。

输出如 2 4 5 和 4 1 5 也同样被接受。

对于第二组测试数据,图 GG 的任意一棵生成树的边权之和都不可能为 11。

约束条件

  • 1≤T≤1051 \le T \le 10^5
  • 2≤N≤2×1052 \le N \le 2\times 10^5
  • N−1≤M≤min⁡(N(N−1)2,2×105)N-1 \le M \le \min\left(\frac{N(N-1)}{2}, 2\times 10^5\right)
  • 0≤K≤N−10 \le K \le N-1
  • 1≤ai,bi≤N1 \le a_i,b_i \le N
  • ci=0c_i=0 或 ci=1c_i=1。
  • 图 GG 是简单图且连通。
  • 所有测试数据中 NN 的总和不超过 2×1052\times 10^5。
  • 所有测试数据中 MM 的总和不超过 2×1052\times 10^5。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页