AT_arc229_e.Taka and Hashi

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Takahashi is known for being able to split into the famous comedy duo "Taka and Hashi", that is, Taka and Hashi. When Taka and Hashi merge, they return to being the original Takahashi.
There is a connected undirected graph with NN vertices and MM edges, and each edge is labeled with 11, 22, or 33. The ii-th edge connects vertices uiu_i and viv_i, and is labeled lil_i. The graph does not contain self-loops, but may contain multi-edges.
Each edge can be traversed by only one of Takahashi, Taka, and Hashi, and an edge labeled 11, 22, or 33 can be traversed only by Takahashi, Taka, or Hashi, respectively.

Initially, Takahashi is at vertex 11. Takahashi and the others can perform the following operations in any order, zero or more times.

  • Takahashi splits into Taka and Hashi at the vertex where he currently is. At this point, both Taka and Hashi are at that vertex.
  • One of the currently existing people moves by traversing one edge that they are able to traverse.
  • When Taka and Hashi are at the same vertex, they merge at that vertex and return to being Takahashi.

Enumerate, in ascending order, all vertices where Takahashi can be after performing a sequence of operations.

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

高桥以能分裂成著名搞笑组合“塔卡与哈希”(即塔卡和哈希)而闻名。当塔卡与哈希合并时,他们便恢复为原来的高桥。
现有一个包含 NN 个顶点和 MM 条边的连通无向图,每条边被标记为 11、22 或 33。第 ii 条边连接顶点 uiu_i 和 viv_i,其标记为 lil_i。该图不含自环,但可能含重边。
每条边仅能由高桥、塔卡或哈希中的一人 traversed(通行),且标记为 11、22 或 33 的边分别只能由高桥、塔卡或哈希通行。

初始时,高桥位于顶点 11。高桥及其他人员可按任意顺序执行以下操作零次或多次:

  • 高桥在当前所在顶点分裂为塔卡和哈希;此时塔卡和哈希均位于该顶点。
  • 当前存在的某一人沿一条其可通行的边移动一步。
  • 当塔卡与哈希处于同一顶点时,他们在该顶点合并,并恢复为高桥。

请按升序枚举所有高桥经过一系列操作后可能到达的顶点。

你将得到 TT 组测试用例,请对每组用例求解。

输入格式

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

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

Each test case caset\mathrm{case}_t is given in the following format:

NN MM
u1u_1 v1v_1 l1l_1
u2u_2 v2v_2 l2l_2
⋮\vdots
uMu_M vMv_M lMl_M

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

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

每个测试用例 caset\mathrm{case}_t 按以下格式给出:

NN MM
u1u_1 v1v_1 l1l_1
u2u_2 v2v_2 l2l_2
⋮\vdots
uMu_M vMv_M lMl_M

输出格式

Output 2T2T lines.
For each test case, letting a1,a2,…,aka_1,a_2,\dots,a_k be the sequence of numbers of the vertices satisfying the condition, listed in ascending order, output them in the following format:

kk
a1a_1 a2a_2 …\dots aka_k

输出 2T2T 行。
对于每个测试用例,设满足条件的顶点编号序列为 a1,a2,…,aka_1,a_2,\dots,a_k(按升序排列),则按以下格式输出:

kk
a1a_1 a2a_2 …\dots aka_k

输入输出样例

  • 输入#1

    3
    5 5
    1 2 1
    2 3 2
    3 4 2
    2 4 3
    4 5 1
    8 15
    1 3 3
    1 5 2
    1 5 3
    1 7 1
    2 3 2
    2 4 1
    2 5 3
    2 6 1
    2 7 3
    2 8 1
    3 4 3
    3 5 3
    4 7 2
    5 6 1
    6 7 1
    10 13
    1 3 2
    1 4 1
    1 6 1
    2 7 2
    2 8 2
    3 6 2
    3 9 2
    4 5 3
    4 7 2
    4 9 2
    5 8 2
    5 10 3
    6 7 2

    输出#1

    4
    1 2 4 5
    8
    1 2 3 4 5 6 7 8
    4
    1 4 5 6

说明/提示

Sample 1 Explanation:
Consider the first test case.
Takahashi can move from vertex 11 to vertex 22 by traversing an edge labeled 11. After splitting into Taka and Hashi at vertex 22, Taka moves 2→3→42 \to 3 \to 4 and Hashi moves 2→42 \to 4, and by merging at vertex 44, it is possible to make Takahashi be at vertex 44. Furthermore, Takahashi can move to vertex 55 by traversing an edge labeled 11.
It is impossible to make Takahashi be at vertex 33, so the vertices satisfying the condition are 1,2,4,51,2,4,5.

Constraints

  • 1≤T≤1051 \leq T \leq 10^5
  • 2≤N≤2×1052 \leq N \leq 2 \times 10^5
  • N−1≤M≤2×105N-1 \leq M \leq 2 \times 10^5
  • 1≤ui<vi≤N1 \leq u_i \lt v_i \leq N
  • 1≤li≤31 \leq l_i \leq 3
  • The given graph is 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 解释:
考虑第一个测试用例。
高桥可以从顶点 11 出发,经过一条标号为 11 的边到达顶点 22。在顶点 22 处分裂为“Taka”和“Hashi”后,“Taka”沿路径 2→3→42 \to 3 \to 4 移动,而“Hashi”沿路径 2→42 \to 4 移动;二者在顶点 44 处合并,从而使得高桥最终位于顶点 44。此外,高桥还可通过一条标号为 11 的边从当前所在顶点移动至顶点 55。
无法使高桥到达顶点 33,因此满足条件的顶点为 1,2,4,51,2,4,5。

限制条件

  • 1≤T≤1051 \leq T \leq 10^5
  • 2≤N≤2×1052 \leq N \leq 2 \times 10^5
  • N−1≤M≤2×105N-1 \leq M \leq 2 \times 10^5
  • 1≤ui<vi≤N1 \leq u_i \lt v_i \leq N
  • 1≤li≤31 \leq l_i \leq 3
  • 给定图是连通的。
  • 所有测试用例中 NN 的总和不超过 2×1052 \times 10^5。
  • 所有测试用例中 MM 的总和不超过 2×1052 \times 10^5。
  • 所有输入值均为整数。

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

首页