D. Viruses

Codeforces
IDCF10214D
Time1000ms
Memory512MB
Difficulty
English · Original
Formal · Original
Modeling biological processes is one of the most important tasks in modern computer science. Recently, biologists have found $n$ viruses, each of which was assigned a unique code number from $1$ to $n$. A virus has the ability to integrate into the cells of other organisms. Initially, at the disposal of scientists, there are $n$ cells numbered from $1$ to $n$. The cell $i$ is infected with the virus $i$. Each cell can be infected with only one virus. For each cell, the scientists know the level of susceptibility of this cell to each of the viruses, i.e. how easy it is for each virus to infect this cell. This is described by a permutation of code numbers of viruses. Virus-infected cells can attack each other. If the cell $i$ is now infected with the virus $a$, it can be attacked by the virus $b$ from another cell, assuming that $b$ is before $a$ in the susceptibility permutation of the cell $i$. It doesn't matter from which cell the virus $b$ comes. As a result of such an attack, the cell $i$ becomes infected with the virus $b$. For example, if the cell $i$ has permutation $(2, 3, 4, 1)$ and is now infected with the virus $1$, and some other cell $j$ is infected with the virus $3$, this virus can attack the cell $i$ – because $3$ is before $1$ in the permutation of the attacked cell $i$. After that, both cells $i$ and $j$ are infected with the virus $3$. As an experiment, the scientists placed all the $n$ cells in a closed environment, causing cells to attack each other at random. The experiment ends when no virus can attack another cell anymore. The scientists call the virus $i$ _stable_ if it must survive the process, and they call the virus $i$ _viable_ if it's possible it will survive the process. More formally, the virus $i$ is _stable_ if it remains in at least one cell at the end of the process after *every* possible sequence of attacks. The virus $i$ is _viable_ if it remains in at least one cell at the end of the process after *some* possible sequence of attacks. The scientists will want to know either which viruses are stable or which viruses are viable. Write a program that takes the description of the susceptibility of cells and the type of the question, and determines all the viruses with the given property (stable or viable). The first line of the input contains an integer $n$ — the number of viruses ($1 <= n <= 500$). This is also the number of cells. The following $n$ lines describe the susceptibility of cells. The $i$-th line contains a permutation of numbers from $1$ to $n$ — the susceptibility permutation of the $i$-th cell. The last line contains the number $p$ that specifies the property that is interesting for the scientists. The value $p = 1$ means you need to identify all stable viruses, while $p = 2$ means you need to identify all viable viruses. The first line of the output should contain an integer $k$ — the number of viruses with the given property ($0 <= k <= n$). The second line should contain $k$ integers — the code numbers of viruses that have this property. The numbers should be displayed in ascending order. $$ \begin{array}{|c|c|c|c|} \hline \text{Subtask} & \text{Points} & \text{Constraints} & \text{Dependencies} \\ \hline 1 & 11 & 1 \le n \le 5; p = 1 & \\ \hline 2 & 21 & 1 \le n \le 500; p = 1 & 1 \\ \hline 3 & 22 & 1 \le n \le 5 & 0, 1 \\ \hline 4 & 31 & 1 \le n \le 50 & 0 - 3 \\ \hline 5 & 15 & 1 \le n \le 500 & 0 - 4 \\ \hline \end{array}$$ You will get points for a group if you pass all tests in this group and all groups listed in the dependencies. The group $0$ denotes the sample test(s) from the statement. In two first sample tests, there are two viruses and the susceptibility permutations are $(1, 2)$ and $(2, 1)$ for the cell $1$ and the cell $2$ respectively. The virus $1$ is initially in the cell $1$ and it can't attack the cell $2$ (infected by the virus $2$) because the susceptibility permutation of the cell $2$ has the virus $2$ first. Similarly, the virus $2$ can't attack the cell $1$. The experiment is terminated immediately because no attack is possible. Hence, both viruses are stable and viable. In the third and fourth sample, each of the two cells can be overtaken by the other virus. The experiment will end after one attack, and either the virus $1$ will be in both cells or the virus $2$ will be in both cells. No virus is stable, but both are viable. ## Input The first line of the input contains an integer $n$ — the number of viruses ($1 <= n <= 500$). This is also the number of cells.The following $n$ lines describe the susceptibility of cells. The $i$-th line contains a permutation of numbers from $1$ to $n$ — the susceptibility permutation of the $i$-th cell.The last line contains the number $p$ that specifies the property that is interesting for the scientists. The value $p = 1$ means you need to identify all stable viruses, while $p = 2$ means you need to identify all viable viruses. ## Output The first line of the output should contain an integer $k$ — the number of viruses with the given property ($0 <= k <= n$).The second line should contain $k$ integers — the code numbers of viruses that have this property. The numbers should be displayed in ascending order. [samples] ## Scoring $$ \begin{array}{|c|c|c|c|} \hline \text{Subtask} & \text{Points} & \text{Constraints} & \text{Dependencies} \\ \hline 1 & 11 & 1 \le n \le 5; p = 1 & \\ \hline 2 & 21 & 1 \le n \le 500; p = 1 & 1 \\ \hline 3 & 22 & 1 \le n \le 5 & 0, 1 \\ \hline 4 & 31 & 1 \le n \le 50 & 0 - 3 \\ \hline 5 & 15 & 1 \le n \le 500 & 0 - 4 \\ \hline \end{array}$$You will get points for a group if you pass all tests in this group and all groups listed in the dependencies. The group $0$ denotes the sample test(s) from the statement. ## Note In two first sample tests, there are two viruses and the susceptibility permutations are $(1, 2)$ and $(2, 1)$ for the cell $1$ and the cell $2$ respectively. The virus $1$ is initially in the cell $1$ and it can't attack the cell $2$ (infected by the virus $2$) because the susceptibility permutation of the cell $2$ has the virus $2$ first. Similarly, the virus $2$ can't attack the cell $1$. The experiment is terminated immediately because no attack is possible. Hence, both viruses are stable and viable.In the third and fourth sample, each of the two cells can be overtaken by the other virus. The experiment will end after one attack, and either the virus $1$ will be in both cells or the virus $2$ will be in both cells. No virus is stable, but both are viable.
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of cells and viruses. For each cell $ i \in \{1, \dots, n\} $, let $ \pi_i \in S_n $ be the susceptibility permutation of virus order (a bijection $ \pi_i : \{1, \dots, n\} \to \{1, \dots, n\} $), where $ \pi_i(j) $ is the $ j $-th most susceptible virus to cell $ i $. Initially, cell $ i $ is infected with virus $ i $. An attack from virus $ b $ (in some cell) to cell $ i $ infected with virus $ a $ is valid if $ b $ appears before $ a $ in $ \pi_i $, i.e., $ \pi_i^{-1}(b) < \pi_i^{-1}(a) $. After a valid attack, cell $ i $ becomes infected with virus $ b $. The process terminates when no valid attack exists. **Constraints** 1. $ 1 \leq n \leq 500 $ 2. Each $ \pi_i $ is a permutation of $ \{1, \dots, n\} $ 3. $ p \in \{1, 2\} $: - If $ p = 1 $, compute **stable** viruses. - If $ p = 2 $, compute **viable** viruses. **Objective** - A virus $ v $ is **stable** if, for *every* possible sequence of attacks, $ v $ remains in at least one cell at termination. - A virus $ v $ is **viable** if there *exists* some sequence of attacks such that $ v $ remains in at least one cell at termination. **Output** Let $ S \subseteq \{1, \dots, n\} $ be the set of viruses with the property specified by $ p $. Output $ |S| $ and the elements of $ S $ in ascending order.
API Response (JSON)
{
  "problem": {
    "name": "D. Viruses",
    "description": {
      "content": "Modeling biological processes is one of the most important tasks in modern computer science. Recently, biologists have found $n$ viruses, each of which was assigned a unique code number from $1$ to $n",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10214D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Modeling biological processes is one of the most important tasks in modern computer science. Recently, biologists have found $n$ viruses, each of which was assigned a unique code number from $1$ to $n...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of cells and viruses.  \nFor each cell $ i \\in \\{1, \\dots, n\\} $, let $ \\pi_i \\in S_n $ be the susceptibility permutation of virus order (a bi...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments