QOJ.ac

QOJ

実行時間制限: 2 s メモリ制限: 512 MB 満点: 100

#1349. 城镇与道路

統計

有 $n$ 个城镇和 $n-1$ 条双向道路。城镇编号为 $1$ 到 $n$,道路编号为 $1$ 到 $n-1$。第 $i$ 条道路连接城镇 $a_i$ 和城镇 $b_i$,长度为 $d_i$。

起初,所有道路都是开放的,且从任意城镇出发,都可以通过一条或多条道路到达其他所有城镇。有一个机器人,初始位于城镇 $1$。

你的任务是处理 $q$ 个查询。每个查询属于以下三种类型之一:

  • $1 \ x$:将机器人移动到城镇 $x$。保证在执行此查询时,机器人当前所在的城镇与城镇 $x$ 之间由一条开放的道路直接相连。
  • $2 \ y$:关闭道路 $y$。保证在执行此查询时,道路 $y$ 是开放的。
  • $3 \ z$:重新开放道路 $z$。保证在执行此查询时,道路 $z$ 是关闭的。

此外,在每次查询后,请立即输出当前机器人所在城镇在仅考虑当前开放道路的情况下,距离最远的所有城镇列表。

输入格式

第一行包含一个整数 $n$,表示城镇数量 ($1 \le n \le 2 \cdot 10^5$)。

接下来的 $n-1$ 行中,第 $i$ 行包含三个整数 $a_i, b_i$ 和 $d_i$:表示第 $i$ 条道路连接的城镇编号以及该道路的长度 ($1 \le a_i, b_i \le n, a_i \neq b_i, 1 \le d_i \le 10^6$)。保证从任意城镇出发,都可以通过一条或多条道路到达其他所有城镇。

下一行包含一个整数 $q$ ($1 \le q \le 2 \cdot 10^5$)。

随后有 $q$ 行,每行描述一个上述形式的查询。

输出格式

输出 $q$ 行:第 $i$ 行应包含第 $i$ 个查询的答案。

对于每个查询,行首输出一个整数 $c_i$:表示在第 $i$ 次查询后,距离机器人所在城镇最远的城镇数量。随后在同一行按升序输出这 $c_i$ 个城镇的编号。

保证在给出的每组测试数据中,所有 $c_i$ 的总和不超过 $4 \cdot 10^5$。

样例

样例输入 1

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

样例输出 1

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

样例输入 2

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

样例输出 2

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

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.