H. Connecting Islands

Codeforces
IDCF10015H
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
The civil war in the Bytelandian ocean has ended. The two Bytelandian islands have decided to unite forming the United Kingdom of Bytelandia. One of the first concerns of the newly chosen government is transportation. Prior to the war each of the 2 islands had a perfect network of transportation. On any given island, every city was connected by a train to every other city. Connections are bidirectional. During the war, some train lines were disrupted and thus, the trains on these lines were suspended. On any island, no more than 15 trains were suspended. In addition to rerunning the disrupted trains, the government has to form inter-island ferry connections. The ferries will connect every city *X* to every city *Y*, given that *X* and *Y* are not on the same island. Commuting between cities on the same island will only be via trains. In Bytelandia, everything is binary, including train and ferry ticket prices. A ticket costs either 0 or 1 units of currency. The government decided not to change any ticket prices for trains that were not disrupted during the war. It will assign ticket costs to the trains suspended during the war and to all ferry lines. Priding themselves in being strong mathematicians, Bytelandians have decided to design the ticket system such that for any 3 distinct cities *X*, *Y* and *Z*, C(*X*, *Y*) + C(*Y*, *Z*)  ≥  C(*X*, *Z*) where C(*X*, *Y*) is the ticket price of getting from *X* to *Y* (by train if they belong to the same island or by ferry otherwise). You are given a description of the Bytelandian islands, your goal is to figure out a costs assignment if it is possible. The input consists of the description of both islands (one after the other). The description of one island starts with one line containing one integer *Ck* represents the number of cities in this island, followed by *Ck* lines each one contains *Ck* characters (1  ≤  *Ck*  ≤  100). Each character is '0', '1' or 'x'. The *jth* character of the *ith* line is the price of the train ticket from city *i* to city *j* on that island. A price 'x' means that it is one of the suspended train lines. Each *Ck* by *Ck* table is guaranteed to be symmetric (table[*i*][*j*] = table[*j*][*i*] for each different *i* and *j*), and each table will have a diagonal of '0's (table[*i*][*i*] = '0' for each different *i*), also each table will contain no more than 30 'x's (15 different trains, because each train will be mentioned twice in the table). _Note that the input might contain 3 different cities in the same islands and the trains connecting them are not suspended, and these 3 cities don't satisfy the described condition above._ If there is no way to do the costs assignment satisfying the conditions described above, output on a single line "NO" (without the quotes). Otherwise, print "YES" (without the quotes) on the first line followed by *C1* + *C2* lines each one contains *C1* + *C2* characters (*C1* is the number of cities in the first island, and *C2* is the number of cities in the second island). The *jth* character on the *ith* line is the cost of getting from city *i* to city *j* (by train if they belong to the same island or by ferry otherwise). In the output, the cities on the first island are numbered from *1* to *C1* (inclusive) while the cities in the second island are numbered from *C1* + *1* to *C1* + *C2* (inclusive). If there is more than one correct solution, print anyone of them. _Note that you can't change the costs given in the input, and the output table must be symmetric._ ## Input The input consists of the description of both islands (one after the other). The description of one island starts with one line containing one integer *Ck* represents the number of cities in this island, followed by *Ck* lines each one contains *Ck* characters (1  ≤  *Ck*  ≤  100). Each character is '0', '1' or 'x'. The *jth* character of the *ith* line is the price of the train ticket from city *i* to city *j* on that island. A price 'x' means that it is one of the suspended train lines. Each *Ck* by *Ck* table is guaranteed to be symmetric (table[*i*][*j*] = table[*j*][*i*] for each different *i* and *j*), and each table will have a diagonal of '0's (table[*i*][*i*] = '0' for each different *i*), also each table will contain no more than 30 'x's (15 different trains, because each train will be mentioned twice in the table)._Note that the input might contain 3 different cities in the same islands and the trains connecting them are not suspended, and these 3 cities don't satisfy the described condition above._ ## Output If there is no way to do the costs assignment satisfying the conditions described above, output on a single line "NO" (without the quotes). Otherwise, print "YES" (without the quotes) on the first line followed by *C1* + *C2* lines each one contains *C1* + *C2* characters (*C1* is the number of cities in the first island, and *C2* is the number of cities in the second island). The *jth* character on the *ith* line is the cost of getting from city *i* to city *j* (by train if they belong to the same island or by ferry otherwise). In the output, the cities on the first island are numbered from *1* to *C1* (inclusive) while the cities in the second island are numbered from *C1* + *1* to *C1* + *C2* (inclusive). If there is more than one correct solution, print anyone of them._Note that you can't change the costs given in the input, and the output table must be symmetric._ [samples]
**Definitions** Let $ G = (V, E) $ be a complete graph with vertex set $ V = V_1 \cup V_2 $, where $ |V_1| = n $, $ |V_2| = m $, and $ V_1 \cap V_2 = \emptyset $. Let $ C: V \times V \to \{0,1\} $ be a symmetric cost function such that: - $ C(i,i) = 0 $ for all $ i \in V $, - For $ i,j \in V_1 $ or $ i,j \in V_2 $, $ C(i,j) $ is given as '0', '1', or 'x' (unknown), - For $ i \in V_1, j \in V_2 $, $ C(i,j) $ is unknown ('x'), - At most 30 entries in total are 'x' (across both islands), - For all $ i,j \in V_1 \cup V_2 $, $ C(i,j) = C(j,i) $. **Constraints** 1. For all $ i,j \in V_1 $: - If input[i][j] ∈ {'0','1'}, then $ C(i,j) = \text{input}[i][j] $. - If input[i][j] = 'x', then $ C(i,j) \in \{0,1\} $ to be assigned. 2. For all $ i,j \in V_2 $: same as above. 3. For all $ i \in V_1, j \in V_2 $: $ C(i,j) \in \{0,1\} $ to be assigned. 4. **Triangle Inequality**: For all distinct $ x,y,z \in V $, $$ C(x,y) + C(y,z) \geq C(x,z) $$ **Objective** Determine whether there exists an assignment of values in $ \{0,1\} $ to all 'x' entries (suspended trains and all ferries) such that the triangle inequality holds for all triples. If yes, output the full $ (n+m) \times (n+m) $ symmetric cost matrix with all 'x' replaced by 0 or 1. If no, output "NO".
API Response (JSON)
{
  "problem": {
    "name": "H. Connecting Islands",
    "description": {
      "content": "The civil war in the Bytelandian ocean has ended. The two Bytelandian islands have decided to unite forming the United Kingdom of Bytelandia. One of the first concerns of the newly chosen government i",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10015H"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "The civil war in the Bytelandian ocean has ended. The two Bytelandian islands have decided to unite forming the United Kingdom of Bytelandia. One of the first concerns of the newly chosen government i...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ G = (V, E) $ be a complete graph with vertex set $ V = V_1 \\cup V_2 $, where $ |V_1| = n $, $ |V_2| = m $, and $ V_1 \\cap V_2 = \\emptyset $.  \nLet $ C: V \\times V \\to \\{0,1\\} $...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments