#P16041. [ICPC 2022 NAC] Contact Tracing

[ICPC 2022 NAC] Contact Tracing

题目描述

A novel infectious disease has started spreading through the population. You are tasked with figuring out who might be infected in order to get them to quarantine. The behavior of the disease among members of the population is as follows:

  • When a person comes into contact with someone who is infectious, they may (but do not always) catch the disease. If they catch the disease, they are not infectious for the rest of that day, so they will not spread it to other people they come into contact with that day.
  • When a person catches the disease, they are infectious starting the day after they caught the disease.
  • Starting two days after they caught the disease, they are symptomatic; as a result, they will quarantine themselves and not come into contact with any other people from then on.

You know that on day 00, a single Patient Zero (of unknown identity) caught the disease. On day 11, they were infectious and potentially came into contact with people, potentially infecting those people. On day 22, Patient Zero became symptomatic and therefore stopped coming into contact with other people, but any people who were infected on day 11 could potentially spread the disease to the people they come into contact with.

Today is day kk, and it is the end of the day. You have access to a list of all pairs of people who came into contact with each other on each day, including today. If you can identify all people who could be infectious but not symptomatic tomorrow (because they caught the disease today) and tell them to quarantine, you can halt the outbreak, because all people who are symptomatic tomorrow will quarantine themselves anyway. What is the minimum number of people you need to tell to quarantine to be sure that you halt the outbreak?

输入格式

The first line of input contains three integers nn (1n1,0001 \le n \le 1{,}000), kk (1k101 \le k \le 10), and cc (0c1,0000 \le c \le 1{,}000), where nn is the number of people, kk is the number of days since Patient Zero became infected, and cc is the number of contacts that occurred between people.

Each of the next cc lines contains three space-separated integers aa, bb (1a<bn1 \le a < b \le n), and dd (1dk1 \le d \le k), denoting that people aa and bb came into contact with each other on day dd. All of these cc lines will be distinct. There will be at least one person with no contacts after day 11.

输出格式

Output a line with a single integer xx denoting the minimum number of people you must tell to quarantine beginning on day k+1k + 1. Then, output xx lines listing the people who must quarantine, one per line, in ascending order.

4 2 4
1 4 1
2 3 2
2 4 1
3 4 1
2
2
3

提示

Explanation of Sample Input 1

In Sample Input 1, Patient Zero could be person 11 or person 44. Persons 22 and 33 can be eliminated, because they had contact on day 22, when Patient Zero would be symptomatic and quarantined.

Suppose person 11 is Patient Zero. Person 11 may have infected person 44 on day 11. Person 11 is symptomatic on day 22, so they begin quarantining. Therefore, there is no chain of contact from person 11 (Patient Zero) to persons 22 or 33, so person 22 and person 33 are safe and do not need to be quarantined. If person 11 infected person 44 on day 11, then person 44 will be symptomatic tomorrow (day 33), so there is no need to contact them because they will isolate on their own. Therefore if Patient Zero is person 11, no one needs to be notified.

Now suppose person 44 is Patient Zero. Person 44 may have infected person 11 on day 11; using the same logic as above, there is no need to notify person 11 or person 44 to quarantine on day 33. This leaves persons 22 and 33, which requires us to consider multiple infection patterns.

If person 44 infected both persons 22 and 33 on day 11, then both of them will be symptomatic and self-quarantine starting on day 33, so we do not need to notify them.

Suppose instead person 44 infected person 22 but NOT person 33 on day 11. (Remember that transmission is not guaranteed.) In this case, person 22 could go on to infect person 33 on day 22, and person 33 would not yet be symptomatic on day 33, so we would need to notify person 33 to quarantine.

Similarly, if person 44 infected person 33 but NOT person 22 on day 11, we would need to notify person 22 to quarantine.

Therefore to be sure we can halt the outbreak, we need to notify both person 22 and person 33 to quarantine. This is guaranteed to stop the outbreak, whether person 11 or person 44 is Patient Zero.