M. Addition

Codeforces
IDCF10115M
Time2000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Six years ago, a robot, Bob, with infant's intelligence has been invented by an evil scientist, Alice. Now the robot is six years old and studies in primary school. Addition is the first operation he learned in math. Due to his strong reasoning ability, he could now conclude a + b = 12 from a = 2 and b = 10. Alice wanted to test Bob's addition skills. Some equations were given to Bob in form of a = 2, b = 10, c = 4, and Bob has to find out the answers of questions like a + b, a + c, etc. Alice checked Bob's answers one by one in the test papers, and no mistake has been found so far, but Alice lost the given equations after a cup of coffee poured on them. However she has some of Bob's correct answers, e.g. a + b = 12, a + c = 6, c + d = 5. She wants to continue with the checkable equations, e.g. b + d = 11 could be concluded by a + b = 12, a + c = 6, c + d = 5, and thus the question b + d is checkable. To prevent the artificial intelligence technology from being under the control of Alice, you disguised yourself as her assistant. Now Alice wants you to figure out which of the rest of questions are checkable and their answers. The first line contains a single integer n (1 ≤ n ≤ 5000): the number of correctly answered questions. Each of the next n lines contain one correctly answered question in the form "_x+y=z_", where x and y are names of variables and z ( - 200000 ≤ z ≤ 200000) is a decimal integer. The next line contains a single integer q (1 ≤ q ≤ 5000): the number of remaining questions. Each of the next q lines contain one question in the form "_x+y_", where x and y are names of variables. Names of variables are strings of lowercase English letters. Each name contains at most 10 characters. There is no contradiction in the answered questions and if the answer is checkable, the result is an integer. For each question in the input that was checkable, output a single line with the answer in the form "_x+y=z_", where x and y are names of variables and z is a decimal integer. Questions should be listed in the same order as they were given in the input. Please do *NOT* ignore duplicated questions, since Alice would fire you if you pointed any mistake of hers. ## Input The first line contains a single integer n (1 ≤ n ≤ 5000): the number of correctly answered questions. Each of the next n lines contain one correctly answered question in the form "_x+y=z_", where x and y are names of variables and z ( - 200000 ≤ z ≤ 200000) is a decimal integer.The next line contains a single integer q (1 ≤ q ≤ 5000): the number of remaining questions. Each of the next q lines contain one question in the form "_x+y_", where x and y are names of variables.Names of variables are strings of lowercase English letters. Each name contains at most 10 characters.There is no contradiction in the answered questions and if the answer is checkable, the result is an integer. ## Output For each question in the input that was checkable, output a single line with the answer in the form "_x+y=z_", where x and y are names of variables and z is a decimal integer. Questions should be listed in the same order as they were given in the input. Please do *NOT* ignore duplicated questions, since Alice would fire you if you pointed any mistake of hers. [samples]
**Definitions** Let $ V $ be a set of variable names (strings of lowercase English letters). Let $ E = \{ (x_i, y_i, z_i) \mid i \in \{1, \dots, n\} \} $ be the set of known equations, where $ x_i, y_i \in V $, $ z_i \in \mathbb{Z} $, and $ x_i + y_i = z_i $. Let $ Q = \{ (u_j, v_j) \mid j \in \{1, \dots, q\} \} $ be the set of queries, where $ u_j, v_j \in V $, and we seek to determine if $ u_j + v_j $ is deducible. **Constraints** 1. $ 1 \le n \le 5000 $ 2. $ 1 \le q \le 5000 $ 3. $ -200000 \le z_i \le 200000 $ for all $ i $ 4. Variable names are non-empty strings of at most 10 lowercase English letters. 5. No contradictions in $ E $. 6. If $ u_j + v_j $ is deducible, the result is an integer. **Objective** For each query $ (u_j, v_j) \in Q $, determine if the value $ u_j + v_j $ can be logically deduced from $ E $ via transitivity and symmetry of equality over sums. If deducible, output $ u_j + v_j = z $ for the unique integer $ z $. Otherwise, do not output anything for that query. Queries must be output in the same order as input, including duplicates.
API Response (JSON)
{
  "problem": {
    "name": "M. Addition",
    "description": {
      "content": "Six years ago, a robot, Bob, with infant's intelligence has been invented by an evil scientist, Alice. Now the robot is six years old and studies in primary school. Addition is the first operation he",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10115M"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Six years ago, a robot, Bob, with infant's intelligence has been invented by an evil scientist, Alice.\n\nNow the robot is six years old and studies in primary school. Addition is the first operation he...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ V $ be a set of variable names (strings of lowercase English letters).  \nLet $ E = \\{ (x_i, y_i, z_i) \\mid i \\in \\{1, \\dots, n\\} \\} $ be the set of known equations, where $ x_i...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments