题目链接:https://codeforces.com/contest/1345/problem/E
#include <algorithm> #include <string> #include <cstring> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <cmath> #include <cstdio> #include <iomanip> #include <ctime> #include <bitset> #include <cmath> #include <sstream> #include <iostream> #include <unordered_map> #define ll long long #define ull unsigned long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back #define INF 0x3f3f3f3f3f3f3f3f #define max(a, b) (a>b?a:b) #define min(a, b) (a<b?a:b) const double eps = 1e-10; const int maxn = 2e5 + 10; const ll MOD = 99999999999999; int sgn(double a) { return a < -eps ? -1 : a < eps ? 0 : 1; } using namespace std; vector<int> g[maxn],f[maxn]; int InDeg[maxn]; int pre[maxn],nxt[maxn]; bool ans[maxn]; int n,m; bool Topsort(vector<int> &q) { q.clear(); for (int i = 1;i <= n;i++) { if (!InDeg[i]) q.push_back(i); } for (int i = 0;i < q.size();i++) { for (auto j : g[q[i]]) { if (--InDeg[j] == 0) q.push_back(j); } } return q.size() == n; } int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1;i <= m;i++) { int u,v; cin >> u >> v; g[u].push_back(v); f[v].push_back(u); } for (int i = 1;i <= n;i++) { InDeg[i] = f[i].size(); } vector<int> top; if (!Topsort(top)) { cout << -1 << endl; return 0; } for (int i = 1;i <= n;i++) { pre[i] = nxt[i] = i; } // 找前驱 for (int i = 0;i < top.size();i++) { for (int j : f[top[i]]) pre[top[i]] = min(pre[top[i]],pre[j]); } // 找后继 for (int i = top.size()-1;i >= 0;i--) { for (int j : g[top[i]]) { nxt[top[i]] = min(nxt[top[i]],nxt[j]); } } int cnt = 0; for (int i = 1;i <= n;i++) { if (min(pre[i],nxt[i]) == i) { cnt++; ans[i] = 1; } } cout << cnt << endl; for (int i = 1;i <= n;i++) { if (ans[i]) cout << "A"; else cout << "E"; } cout << endl; return 0; }