CF369C.Valera and Elections
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
The city Valera lives in is going to hold elections to the city Parliament.
The city has n districts and n - 1 bidirectional roads. We know that from any district there is a path along the roads to any other district. Let's enumerate all districts in some way by integers from 1 to n, inclusive. Furthermore, for each road the residents decided if it is the problem road or not. A problem road is a road that needs to be repaired.
There are n candidates running the elections. Let's enumerate all candidates in some way by integers from 1 to n, inclusive. If the candidate number i will be elected in the city Parliament, he will perform exactly one promise — to repair all problem roads on the way from the i-th district to the district 1, where the city Parliament is located.
Help Valera and determine the subset of candidates such that if all candidates from the subset will be elected to the city Parliament, all problem roads in the city will be repaired. If there are several such subsets, you should choose the subset consisting of the minimum number of candidates.
瓦列拉居住的城市即将举行市议会选举。
该城市有 n 个区和 n−1 条双向道路。已知从任意一个区出发,都存在一条沿道路通往其他任意区的路径。我们用 1 到 n(含端点)之间的整数对所有区进行某种编号。此外,对于每条道路,居民们已决定它是否为“问题道路”;所谓问题道路,即需要维修的道路。
共有 n 名候选人参与本次选举。我们用 1 到 n(含端点)之间的整数对所有候选人进行某种编号。若编号为 i 的候选人当选市议会议员,则他将严格履行一项承诺:维修从第 i 个区通往第 1 个区(即市议会所在地)路径上的所有问题道路。
请帮助瓦列拉确定一个候选人子集,使得若该子集中的所有候选人均当选市议会议员,则全市所有问题道路都将被维修。若存在多个满足条件的子集,请选择其中候选人数量最少的一个。
输入格式
The first line contains a single integer n (2 ≤ n ≤ 105) — the number of districts in the city.
Then n - 1 lines follow. Each line contains the description of a city road as three positive integers x__i, y__i, t__i (1 ≤ x__i, y__i ≤ n, 1 ≤ t__i ≤ 2) — the districts connected by the i-th bidirectional road and the road type. If t__i equals to one, then the i-th road isn't the problem road; if t__i equals to two, then the i-th road is the problem road.
It's guaranteed that the graph structure of the city is a tree.
第一行包含一个整数 n(2≤n≤105)—— 表示城市中区域的数量。
接下来是 n−1 行。每行以三个正整数 xi、yi、ti(1≤xi,yi≤n,1≤ti≤2)描述一条城市道路 —— 分别表示第 i 条双向道路所连接的两个区域以及该道路的类型。若 ti=1,则第 i 条道路不是问题道路;若 ti=2,则第 i 条道路是问题道路。
保证该城市的图结构是一棵树。
输出格式
In the first line print a single non-negative number k — the minimum size of the required subset of candidates. Then on the second line print k space-separated integers _a_1, _a_2, ... a__k — the numbers of the candidates that form the required subset. If there are multiple solutions, you are allowed to print any of them.
第一行输出一个非负整数 k —— 所需候选人子集的最小大小。
第二行输出 k 个用空格分隔的整数 a1, a2, …, ak —— 构成所需子集的候选人编号。
若存在多个解,可输出其中任意一个。
输入输出样例
输入#1
5 1 2 2 2 3 2 3 4 2 4 5 2
输出#1
1 5
输入#2
5 1 2 1 2 3 2 2 4 1 4 5 1
输出#2
1 3
输入#3
5 1 2 2 1 3 2 1 4 2 1 5 2
输出#3
4 5 4 3 2
输入解题思路,AI测评打分。不知道怎么写?