CF993B.Open Communication

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Two participants are each given a pair of distinct numbers from 1 to 9 such that there's exactly one number that is present in both pairs. They want to figure out the number that matches by using a communication channel you have access to without revealing it to you.

Both participants communicated to each other a set of pairs of numbers, that includes the pair given to them. Each pair in the communicated sets comprises two different numbers.

Determine if you can with certainty deduce the common number, or if you can determine with certainty that both participants know the number but you do not.

两名参与者各自获得一对来自 1 到 9 的互异数字,且这两对数字中恰好有一个公共数字。他们希望借助你可访问的通信信道,协作推断出这个公共数字,但不能向你透露该数字。

两名参与者各自向对方发送了一个数字对集合,其中包含各自被分配到的那对数字;该集合中的每个数字对均由两个互异的数字组成。

请判断:你是否能确定地推断出该公共数字;或者,你是否能确定地判断出两名参与者均已知晓该数字,而你却无法得知。

输入格式

The first line contains two integers nn and mm (1≤n,m≤121 \le n, m \le 12) — the number of pairs the first participant communicated to the second and vice versa.

The second line contains nn pairs of integers, each between 11 and 99, — pairs of numbers communicated from first participant to the second.

The third line contains mm pairs of integers, each between 11 and 99, — pairs of numbers communicated from the second participant to the first.

All pairs within each set are distinct (in particular, if there is a pair (1,2)(1,2), there will be no pair (2,1)(2,1) within the same set), and no pair contains the same number twice.

It is guaranteed that the two sets do not contradict the statements, in other words, there is pair from the first set and a pair from the second set that share exactly one number.

第一行包含两个整数 nn 和 mm(1≤n,m≤121 \le n, m \le 12)—— 分别表示第一位参与者向第二位参与者传达的数对数量,以及第二位参与者向第一位参与者传达的数对数量。

第二行包含 nn 个整数对,每个整数均在 11 到 99 之间——这些数对由第一位参与者传达给第二位参与者。

第三行包含 mm 个整数对,每个整数均在 11 到 99 之间——这些数对由第二位参与者传达给第一位参与者。

每组内的所有数对互不相同(特别地,若存在数对 (1,2)(1,2),则同一组内不会出现 (2,1)(2,1)),且任意数对中不包含重复数字。

保证这两组数对彼此不矛盾,即:存在一个来自第一组的数对和一个来自第二组的数对,它们恰好共享一个数字。

输出格式

If you can deduce the shared number with certainty, print that number.

If you can with certainty deduce that both participants know the shared number, but you do not know it, print 00.

Otherwise print −1-1.

如果你能确定地推断出共享的数字,请输出该数字。

如果你能确定地推断出两位参与者都知道该共享数字,但你自己并不知道该数字,则输出 00。

否则输出 −1-1。

输入输出样例

  • 输入#1

    2 2
    1 2 3 4
    1 5 3 4

    输出#1

    1
  • 输入#2

    2 2
    1 2 3 4
    1 5 6 4

    输出#2

    0
  • 输入#3

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

    输出#3

    -1

说明/提示

In the first example the first participant communicated pairs (1,2)(1,2) and (3,4)(3,4), and the second communicated (1,5)(1,5), (3,4)(3,4). Since we know that the actual pairs they received share exactly one number, it can't be that they both have (3,4)(3,4). Thus, the first participant has (1,2)(1,2) and the second has (1,5)(1,5), and at this point you already know the shared number is 11.

In the second example either the first participant has (1,2)(1,2) and the second has (1,5)(1,5), or the first has (3,4)(3,4) and the second has (6,4)(6,4). In the first case both of them know the shared number is 11, in the second case both of them know the shared number is 44. You don't have enough information to tell 11 and 44 apart.

In the third case if the first participant was given (1,2)(1,2), they don't know what the shared number is, since from their perspective the second participant might have been given either (1,3)(1,3), in which case the shared number is 11, or (2,3)(2,3), in which case the shared number is 22. While the second participant does know the number with certainty, neither you nor the first participant do, so the output is −1-1.

在第一个例子中,第一位参与者通报了数对 (1,2)(1,2) 和 (3,4)(3,4),第二位参与者通报了 (1,5)(1,5) 与 (3,4)(3,4)。由于我们已知他们实际收到的数对恰好共享一个数字,因此他们不可能都持有 (3,4)(3,4)。于是,第一位参与者持有的是 (1,2)(1,2),第二位参与者持有的是 (1,5)(1,5);此时你已经可以确定共享数字为 11。

在第二个例子中,可能的情形有两种:要么第一位参与者持有 (1,2)(1,2) 且第二位持有 (1,5)(1,5),要么第一位持有 (3,4)(3,4) 且第二位持有 (6,4)(6,4)。在第一种情形下,双方均能确定共享数字为 11;在第二种情形下,双方均能确定共享数字为 44。你所掌握的信息不足以区分 11 和 44。

在第三个例子中,若第一位参与者收到的是 (1,2)(1,2),则其无法确定共享数字究竟是多少:因为在该参与者看来,第二位参与者可能收到的是 (1,3)(1,3)(此时共享数字为 11),也可能是 (2,3)(2,3)(此时共享数字为 22)。尽管第二位参与者能确切知道该共享数字,但你和第一位参与者均无法确定,因此输出为 −1-1。

输入解题思路,AI测评打分。不知道怎么写?

首页