E. Quantifier Question
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 (∀∀) and existential (∃∃). 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∀x,x<100 is read as: for all real numbers xx, xx is less than 100100. This statement is false.
- ∀x,x>x−1∀x,x>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∃x,x<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∃x,x>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∀x,∃y,x<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∃y,∀x,x<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,…,xnx1,x2,…,xn, and you are given some formula of the form
f(x1,…,xn):=(xj1<xk1)∧(xj2<xk2)∧⋯∧(xjm<xkm),f(x1,…,xn):=(xj1<xk1)∧(xj2<xk2)∧⋯∧(xjm<xkm),
where ∧∧ denotes logical AND. That is, f(x1,…,xn)f(x1,…,xn) is true if every inequality xji<xkixji<xki holds. Otherwise, if at least one inequality does not hold, then f(x1,…,xn)f(x1,…,xn) is false.
Your task is to assign quantifiers Q1,…,QnQ1,…,Qn to either universal (∀∀) or existential (∃∃) so that the statement
Q1x1,Q2x2,…,Qnxn,f(x1,…,xn)Q1x1,Q2x2,…,Qnxn,f(x1,…,xn)
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(x1,x2):=(x1<x2) then you are not allowed to make x2x2 appear first and use the statement ∀x2,∃x1,x1<x2∀x2,∃x1,x1<x2. If you assign Q1=∃Q1=∃ and Q2=∀Q2=∀, it will only be interpreted as ∃x1,∀x2,x1<x2∃x1,∀x2,x1<x2.
Input
The first line contains two integers nn and mm (2≤n≤2⋅1052≤n≤2⋅105; 1≤m≤2⋅1051≤m≤2⋅105) — 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 jiji,kiki (1≤ji,ki≤n1≤ji,ki≤n, ji≠kiji≠ki).
Output
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 QiQi should be a universal quantifier (∀∀), or "E" if QiQi should be an existential quantifier (∃∃). All letters should be upper-case. If there are multiple solutions where the number of universal quantifiers is maximum, print any.
Examples
input
Copy
2 1 1 2
output
Copy
1 AE
input
Copy
4 3 1 2 2 3 3 1
output
Copy
-1
input
Copy
3 2 1 3 2 3
output
Copy
2 AAE
Note
For the first test, the statement ∀x1,∃x2,x1<x2∀x1,∃x2,x1<x2 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)∀x1,∀x2,∃x3,(x1<x3)∧(x2<x3) is true: We can set x3=max{x1,x2}+1x3=max{x1,x2}+1.
思路:
有环肯定不行。
从题中例子可以看出,排在靠前面的xi在没有限制的情况下,都是A,后出现的有限制才用E.
利用并查集判断有无环,并对遍历到的点进行标记,表示该点有限制。
双向遍历,因为限制是相互的。
#include<iostream>
#include<vector>
using namespace std;
const int N = 2e5 + 10;
vector <int> v1[N],v2[N];
int vis1[N],vis2[N];
bool dfs1(int x)
{
vis1[x] = -1;
for(auto a : v1[x])
{
if(vis1[a] < 0) return 0;
else if(!vis1[a]&&!dfs1(a)) return 0;
}
vis1[x] = 1;
return 1;
}
bool dfs2(int x)
{
vis2[x] = -1;
for(auto a : v2[x])
{
if(vis2[a] < 0) return 0;
else if(!vis2[a]&&!dfs2(a)) return 0;
}
vis2[x] = 1;
return 1;
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0; i<m; i++)
{
int a,b;
cin>>a>>b;
v1[a].push_back(b);
v2[b].push_back(a);
}
string ans;
int cnt=0;
for(int i=1; i<=n; i++)
{
if(!vis1[i]&&!vis2[i]) ans += 'A',cnt++;
else ans += 'E';
if(!dfs1(i) || !dfs2(i))
{
puts("-1");
return 0;
}
}
cout<<cnt<<endl<<ans<<endl;
return 0;
}