#include #include using namespace std; struct Graph { int n; std::vector> g; Graph(){} Graph(int n) : n(n){ g.resize(n); } void add_edge(int from, int to){ g[from].push_back(to); } }; template struct Edge { int to; T cost; }; template struct WeightedGraph { int n; std::vector>> g; WeightedGraph(){} WeightedGraph(int n) : n(n){ g.resize(n); } void add_edge(int from, int to, T cost){ g[from].push_back((Edge){to, cost}); } }; std::vector bipartite_graph(Graph &g){ int n = g.n; std::vector b(n + 1, -1); b[n] = 0; std::queue que; for(int i = 0; i < n; i++){ if(b[i] != -1) continue; b[i] = 0; que.push(i); while(que.size()){ int u = que.front(); que.pop(); for(int v : g.g[u]){ if(b[u] == b[v]) return b; if(b[v] == -1){ b[v] = 1 - b[u]; que.push(v); } } } } b[n] = 1; return b; } struct SCC //StronglyConnectedComponents { int n; int k; std::vector> g, rg; std::vector used; std::vector cmp; std::vector vs; SCC(){} SCC(int n) : n(n){ g.resize(n); rg.resize(n); used.resize(n); cmp.resize(n); } void add_edge(int from, int to){ g[from].push_back(to); rg[to].push_back(from); } void dfs(int u){ used[u] = true; for(int v : g[u]){ if(!used[v]) dfs(v); } vs.push_back(u); } void rdfs(int u, int k){ used[u] = true; cmp[u] = k; for(int v : rg[u]){ if(!used[v]) rdfs(v, k); } } int decomposition(){ for(int i = 0; i < n; i++) used[i] = false; for(int i = 0; i < n; i++){ if(!used[i]) dfs(i); } for(int i = 0; i < n; i++) used[i] = false; k = 0; for(int i = n - 1; i >= 0; i--){ if(!used[vs[i]]){ rdfs(vs[i], k); k++; } } return k; } /* int decomposition(Graph &dag){ k = decomposition(); dag.n = k; dag.g.resize(k); std::map, bool> mp; for(int u = 0; u < n; u++){ for(int v : g[u]){ if(!mp[std::pair (cmp[u], cmp[v])] && cmp[u] != cmp[v]){ mp[std::pair (cmp[u], cmp[v])] = true; dag.add_edge(cmp[u], cmp[v]); } } } return k; } */ }; int main() { int n, m; cin >> n >> m; Graph g0(n); WeightedGraph g[2]; g[0].g.resize(n); g[1].g.resize(n); SCC g1(n); int a[2002], b[2002]; for(int i = 0; i < m; i++){ cin >> a[i] >> b[i]; a[i]--; b[i]--; g0.add_edge(a[i], b[i]); g[0].add_edge(a[i], b[i], i); g[1].add_edge(b[i], a[i], i); g1.add_edge(a[i], b[i]); } int k = g1.decomposition(); if(k > 1){ cout << -1 << endl; return 0; } vector z = bipartite_graph(g0); if(z[n]){ cout << -1 << endl; return 0; } vector loop; int s = n; for(int r = 0; r < n; r++){ int d[2][1002]; Edge p[2][1002]; queue que[2]; for(int c = 0; c < 2; c++){ for(int u = 0; u < n; u++) d[c][u] = -1; d[c][r] = 0; que[c].push(r); } while(que[0].size() && que[1].size()){ for(int c = 0; c < 2; c++){ if(que[c].empty()) continue; int u = que[c].front(); que[c].pop(); for(Edge e : g[c].g[u]){ int v = e.to; if(d[c][v] == -1){ d[c][v] = d[c][u] + 1; p[c][v] = Edge{u, e.cost}; que[c].push(v); } if(d[1 - c][v] != -1){ if(d[0][v] + d[1][v] < s && v != r){ loop.clear(); s = d[0][v] + d[1][v]; for(int i = 0; i < 2; i++){ int w = v; while(w != r){ loop.push_back(p[i][w].cost); w = p[i][w].to; } } } } } } } } if(s == n){ cout << -1 << endl; return 0; } int l; for(l = 0; l < s; l++){ int j = loop[l]; SCC g2(n); for(int i = 0; i < m; i++){ if(i != j) g2.add_edge(a[i], b[i]); } int k = g2.decomposition(); if(k < n) break; } int ans[2002]{0}; for(int i = 0; i <= s / 2; i++) ans[loop[(i + l) % s]] = 1; for(int i = 0; i < m; i++){ if(ans[i]) cout << 'R'; else cout << 'G'; } cout << endl; }