結果
問題 | No.1436 Rgaph |
ユーザー | SHIJOU |
提出日時 | 2021-03-26 00:33:09 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 6 ms / 2,000 ms |
コード長 | 5,312 bytes |
コンパイル時間 | 2,506 ms |
コンパイル使用メモリ | 220,664 KB |
実行使用メモリ | 8,192 KB |
最終ジャッジ日時 | 2024-05-05 12:44:30 |
合計ジャッジ時間 | 5,246 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 1 ms
5,376 KB |
testcase_10 | AC | 6 ms
7,936 KB |
testcase_11 | AC | 5 ms
8,192 KB |
testcase_12 | AC | 5 ms
8,192 KB |
testcase_13 | AC | 3 ms
5,376 KB |
testcase_14 | AC | 3 ms
5,504 KB |
testcase_15 | AC | 3 ms
5,376 KB |
testcase_16 | AC | 3 ms
5,376 KB |
testcase_17 | AC | 2 ms
5,376 KB |
testcase_18 | AC | 3 ms
5,376 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 3 ms
5,376 KB |
testcase_21 | AC | 3 ms
5,376 KB |
testcase_22 | AC | 4 ms
5,504 KB |
testcase_23 | AC | 4 ms
5,760 KB |
testcase_24 | AC | 5 ms
6,144 KB |
testcase_25 | AC | 5 ms
6,912 KB |
testcase_26 | AC | 6 ms
7,808 KB |
testcase_27 | AC | 6 ms
7,808 KB |
testcase_28 | AC | 2 ms
5,376 KB |
testcase_29 | AC | 6 ms
7,936 KB |
ソースコード
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using ll = int64_t; using ull = uint64_t; using ld = long double; using P = pair<int, int>; using vs = vector<string>; using vi = vector<int>; using vvi = vector<vi>; template<class T> using PQ = priority_queue<T>; template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>; const int INF = 0xccccccc; const ll LINF = 0xcccccccccccccccLL; template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template<typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;} template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;} struct SCC { struct csr { vector<int> start, elist; csr(int n, const vector<pair<int, int>> &edges) : start(n+1), elist(edges.size()) { for(pair<int, int> e:edges) { start[e.first+1]++; } for(int i = 1; i <= n; i++) { start[i] += start[i-1]; } vector<int> counter = start; for(pair<int, int> e:edges) { elist[counter[e.first]++] = e.second; } } }; int n; vector<pair<int, int>> edges; SCC(int n_) : n(n_) {} void add(int from, int to) {edges.emplace_back(from, to);} pair<int, vector<int>> scc_ids() { csr g(n, edges); int now_ord = 0, group_num = 0; vector<int> used, low(n), ord(n, -1), ids(n); used.reserve(n); auto dfs = [&](auto self, int v)->void { low[v] = ord[v] = now_ord++; used.push_back(v); for(int i = g.start[v]; i < g.start[v+1]; i++) { int to = g.elist[i]; if(ord[to] == -1) { self(self, to); low[v] = min(low[v], low[to]); } else { low[v] = min(low[v], ord[to]); } } if(low[v] == ord[v]) { while(true) { int u = used.back(); used.pop_back(); ord[u] = n; ids[u] = group_num; if(u == v) break; } group_num++; } }; for(int i = 0; i < n; i++) { if(ord[i] == -1) dfs(dfs, i); } for(int &x:ids) { x = group_num - 1 - x; } return {group_num, ids}; } vector<vector<int>> scc() { pair<int, vector<int>> ids = scc_ids(); int group_num = ids.first; vector<int> counts(group_num); for(int x:ids.second) counts[x]++; vector<vector<int>> groups(group_num); for(int i = 0; i < group_num; i++) { groups[i].reserve(counts[i]); } for(int i = 0; i < n; i++) { groups[ids.second[i]].push_back(i); } return groups; } }; //head int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; if(m <= n) { puts("-1"); return 0; } SCC scc(n); vector<vector<P>> G(n); rep(i, m) { int a, b; cin >> a >> b; a--; b--; scc.add(a, b); G[a].emplace_back(b, i); } if(scc.scc_ids().first != 1) { puts("-1"); return 0; } vi check(n, -1); vvi r(n); auto dfs = [&](auto self, int i) -> vi { vi res; for(auto [ne, id]:G[i]) { vi u; if(check[ne] == -1) { check[ne] = check[i]^1; r[ne] = r[i]; r[ne].emplace_back(id); u = self(self, ne); if(not u.empty()) u.emplace_back(id); } else if(check[ne] == check[i]) { u.emplace_back(id); } if(not u.empty()) res = u; } return res; }; auto __s = [&](int root) { fill(all(check), -1); check[root] = 0; rep(i, n) r[i].clear(); return dfs(dfs, root); }; vi x = __s(0); if(x.empty()) { puts("-1"); return 0; } int to = scc.edges[x[0]].second; vi y = r[to]; __s(to); vi z = r[0]; if((int(z.size())^int(x.size()))&1) { while(not x.empty()) { z.emplace_back(x.back()); x.pop_back(); } } else { z.reserve(int(z.size())+int(y.size())); copy(all(y), back_inserter(z)); } vector<P> pa(n, {-1, -1}); pa[to] = {0, 0}; vi re; rep(i, int(z.size())) { auto [f, t] = scc.edges[z[i]]; if(pa[t].first == -1) { pa[t] = {pa[f].first^1, i+1}; } else { if(pa[t].first == pa[f].first) { vi zz; for(int j = pa[t].second; j <= i; j++) { zz.emplace_back(z[j]); } rep(k, pa[t].second) re.emplace_back(z[k]); for(int k = i+1; k < int(z.size()); k++) re.emplace_back(z[k]); z.swap(zz); break; } else { pa[t].second = i+1; } } } string ans(m, '_'); for(auto x:re) ans[x] = 'R'; if(re.empty()) { int u = -1; vi ch(m); for(auto x:z) ch[x] = 1; rep(i, m) if(not ch[i]) { chmax(u, pa[scc.edges[i].second].second); } ans[z[u]] = 'R'; } rep(i, int(z.size())) if(ans[z[i]] == 'R') { rotate(z.begin(), z.begin()+i+1, z.end()); } ans[z[0]] = 'G'; for(int i = 1; i < int(z.size()); i++) ans[z[i]] = (i&1?'G':'R'); rep(i, m) if(ans[i] == '_') ans[i] = 'R'; cout << ans << endl; }