CF1344C.Quantifier Question

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Logical quantifiers are very useful tools for expressing claims about a set. For this problem, let's focus on the set of real numbers specifically. The set of real numbers includes zero and negatives. There are two kinds of quantifiers: universal (∀\forall) and existential (∃\exists). You can read more about them here.

The universal quantifier is used to make a claim that a statement holds for all real numbers. For example:

  • ∀x,x<100\forall x,x \lt 100 is read as: for all real numbers xx, xx is less than 100100. This statement is false.
  • ∀x,x>x−1\forall x,x \gt x-1 is read as: for all real numbers xx, xx is greater than x−1x-1. This statement is true.

The existential quantifier is used to make a claim that there exists some real number for which the statement holds. For example:

  • ∃x,x<100\exists x,x \lt 100 is read as: there exists a real number xx such that xx is less than 100100. This statement is true.
  • ∃x,x>x−1\exists x,x \gt x-1 is read as: there exists a real number xx such that xx is greater than x−1x-1. This statement is true.

Moreover, these quantifiers can be nested. For example:

  • ∀x,∃y,x<y\forall x,\exists y,x \lt y is read as: for all real numbers xx, there exists a real number yy such that xx is less than yy. This statement is true since for every xx, there exists y=x+1y=x+1.
  • ∃y,∀x,x<y\exists y,\forall x,x \lt y is read as: there exists a real number yy such that for all real numbers xx, xx is less than yy. This statement is false because it claims that there is a maximum real number: a number yy larger than every xx.

Note that the order of variables and quantifiers is important for the meaning and veracity of a statement.

There are nn variables x1,x2,…,xnx_1,x_2,\ldots,x_n, and you are given some formula of the form $$ f(x_1,\dots,x_n):=(x_{j_1} \lt x_{k_1})\land (x_{j_2} \lt x_{k_2})\land \cdots\land (x_{j_m} \lt x_{k_m}), $$

where ∧\land denotes logical AND. That is, f(x1,…,xn)f(x_1,\ldots, x_n) is true if every inequality xji<xkix_{j_i} \lt x_{k_i} holds. Otherwise, if at least one inequality does not hold, then f(x1,…,xn)f(x_1,\ldots,x_n) is false.

Your task is to assign quantifiers Q1,…,QnQ_1,\ldots,Q_n to either universal (∀\forall) or existential (∃\exists) so that the statement $$ Q_1 x_1, Q_2 x_2, \ldots, Q_n x_n, f(x_1,\ldots, x_n) $$

is true, and the number of universal quantifiers is maximized, or determine that the statement is false for every possible assignment of quantifiers.

Note that the order the variables appear in the statement is fixed. For example, if f(x1,x2):=(x1<x2)f(x_1,x_2):=(x_1 \lt x_2) then you are not allowed to make x2x_2 appear first and use the statement ∀x2,∃x1,x1<x2\forall x_2,\exists x_1, x_1 \lt x_2. If you assign Q1=∃Q_1=\exists and Q2=∀Q_2=\forall, it will only be interpreted as ∃x1,∀x2,x1<x2\exists x_1,\forall x_2,x_1 \lt x_2.

逻辑量词是表达关于集合的命题时非常有用的工具。在本题中,我们专门关注实数集。实数集包含零和负数。量词分为两类:全称量词(∀\forall)和存在量词(∃\exists)。你可在此处了解更多。

全称量词用于断言某个命题对所有实数均成立。例如:

  • ∀x,x<100\forall x,x \lt 100 读作:“对所有实数 xx,xx 小于 100100”。该命题为假。
  • ∀x,x>x−1\forall x,x \gt x-1 读作:“对所有实数 xx,xx 大于 x−1x-1”。该命题为真。

存在量词用于断言存在某个实数使得命题成立。例如:

  • ∃x,x<100\exists x,x \lt 100 读作:“存在一个实数 xx,使得 xx 小于 100100”。该命题为真。
  • ∃x,x>x−1\exists x,x \gt x-1 读作:“存在一个实数 xx,使得 xx 大于 x−1x-1”。该命题为真。

此外,这些量词可以嵌套使用。例如:

  • ∀x,∃y,x<y\forall x,\exists y,x \lt y 读作:“对所有实数 xx,存在一个实数 yy,使得 xx 小于 yy”。该命题为真,因为对任意 xx,取 y=x+1y=x+1 即可满足。
  • ∃y,∀x,x<y\exists y,\forall x,x \lt y 读作:“存在一个实数 yy,使得对所有实数 xx,xx 均小于 yy”。该命题为假,因为它声称存在一个最大的实数:即一个大于所有 xx 的数 yy。

注意,变量与量词的顺序对命题的含义及其真假至关重要。

现有 nn 个变量 x1,x2,…,xnx_1,x_2,\ldots,x_n,并给定一个形如

f(x_1,dots,x_n):=(x_j_1ltx_k_1)land(x_j_2ltx_k_2)landcdotsland(x_j_mltx_k_m)f(x\_1,\\dots,x\_n):=(x\_{j\_1} \\lt x\_{k\_1})\\land (x\_{j\_2} \\lt x\_{k\_2})\\land \\cdots\\land (x\_{j\_m} \\lt x\_{k\_m})

的公式,其中 ∧\land 表示逻辑“与”(AND)。即,当且仅当每个不等式 xji<xkix_{j_i} \lt x_{k_i} 均成立时,f(x1,…,xn)f(x_1,\ldots, x_n) 为真;否则(即至少有一个不等式不成立),f(x1,…,xn)f(x_1,\ldots,x_n) 为假。

你的任务是为变量 x1,…,xnx_1,\ldots,x_n 分别赋予量词 Q1,…,QnQ_1,\ldots,Q_n,每个量词为全称量词(∀\forall)或存在量词(∃\exists),使得命题

Q_1x_1,Q_2x_2,ldots,Q_nx_n,f(x_1,ldots,x_n)Q\_1 x\_1, Q\_2 x\_2, \\ldots, Q\_n x\_n, f(x\_1,\\ldots, x\_n)

为真,并在所有可行的赋值方案中使全称量词的个数最大化;若不存在任何赋值方案能使该命题为真,则判定其恒假。

注意:命题中变量的出现顺序是固定的。例如,若 f(x1,x2):=(x1<x2)f(x_1,x_2):=(x_1 \lt x_2),则不允许将 x2x_2 置于 x1x_1 之前,即不允许使用 ∀x2,∃x1,x1<x2\forall x_2,\exists x_1, x_1 \lt x_2 这一形式。若你指定 Q1=∃Q_1=\exists 且 Q2=∀Q_2=\forall,则该命题仅被解释为 ∃x1,∀x2,x1<x2\exists x_1,\forall x_2,x_1 \lt x_2。

输入格式

The first line contains two integers nn and mm (2≤n≤2⋅1052\le n\le 2\cdot 10^5; 1≤m≤2⋅1051\le m\le 2\cdot 10^5) — the number of variables and the number of inequalities in the formula, respectively.

The next mm lines describe the formula. The ii-th of these lines contains two integers jij_i,kik_i (1≤ji,ki≤n1\le j_i,k_i\le n, ji≠kij_i\ne k_i).

第一行包含两个整数 nn 和 mm(2≤n≤2⋅1052\le n\le 2\cdot 10^5;1≤m≤2⋅1051\le m\le 2\cdot 10^5),分别表示变量的个数和公式中不等式的个数。

接下来的 mm 行描述该公式。其中第 ii 行包含两个整数 jij_i、kik_i(1≤ji,ki≤n1\le j_i,k_i\le n,且 ji≠kij_i\ne k_i)。

输出格式

If there is no assignment of quantifiers for which the statement is true, output a single integer −1-1.

Otherwise, on the first line output an integer, the maximum possible number of universal quantifiers.

On the next line, output a string of length nn, where the ii-th character is "A" if QiQ_i should be a universal quantifier (∀\forall), or "E" if QiQ_i should be an existential quantifier (∃\exists). All letters should be upper-case. If there are multiple solutions where the number of universal quantifiers is maximum, print any.

如果不存在使该命题为真的量词赋值方案,则输出单个整数 −1-1。

否则,在第一行输出一个整数,表示可能的全称量词的最大数目。

在下一行输出一个长度为 nn 的字符串,其中第 ii 个字符为 "A" 表示 QiQ_i 应为全称量词(∀\forall),或为 "E" 表示 QiQ_i 应为存在量词(∃\exists)。所有字母均须为大写。若存在多个使全称量词数目达到最大的解,输出任意一个即可。

输入输出样例

  • 输入#1

    2 1
    1 2

    输出#1

    1
    AE
  • 输入#2

    4 3
    1 2
    2 3
    3 1

    输出#2

    -1
  • 输入#3

    3 2
    1 3
    2 3

    输出#3

    2
    AAE

说明/提示

For the first test, the statement ∀x1,∃x2,x1<x2\forall x_1, \exists x_2, x_1 \lt x_2 is true. Answers of "EA" and "AA" give false statements. The answer "EE" gives a true statement, but the number of universal quantifiers in this string is less than in our answer.

For the second test, we can show that no assignment of quantifiers, for which the statement is true exists.

For the third test, the statement ∀x1,∀x2,∃x3,(x1<x3)∧(x2<x3)\forall x_1, \forall x_2, \exists x_3, (x_1 \lt x_3)\land (x_2 \lt x_3) is true: We can set x3=max⁡x1,x2+1x_3=\max{x_1,x_2}+1.

对于第一个测试,命题 ∀x1,∃x2,x1<x2\forall x_1, \exists x_2, x_1 \lt x_2 为真。“EA”和“AA”的答案给出的是假命题。“EE”的答案给出的是真命题,但该字符串中全称量词的个数少于我们答案中的个数。

对于第二个测试,我们可以证明:不存在能使该命题为真的量词赋值方式。

对于第三个测试,命题 ∀x1,∀x2,∃x3,(x1<x3)∧(x2<x3)\forall x_1, \forall x_2, \exists x_3, (x_1 \lt x_3)\land (x_2 \lt x_3) 为真:我们可以取 x3=max⁡x1,x2+1x_3=\max{x_1,x_2}+1。

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

首页