結果
問題 | No.2464 To DAG |
ユーザー | 👑 hos.lyric |
提出日時 | 2023-09-08 22:31:38 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 8,376 bytes |
コンパイル時間 | 1,396 ms |
コンパイル使用メモリ | 121,688 KB |
実行使用メモリ | 87,716 KB |
最終ジャッジ日時 | 2024-06-26 15:44:19 |
合計ジャッジ時間 | 15,942 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | WA | - |
testcase_02 | AC | 363 ms
87,716 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | AC | 163 ms
33,080 KB |
testcase_15 | AC | 237 ms
41,344 KB |
testcase_16 | AC | 110 ms
24,320 KB |
testcase_17 | AC | 17 ms
6,784 KB |
testcase_18 | AC | 68 ms
16,768 KB |
testcase_19 | AC | 2 ms
5,376 KB |
testcase_20 | AC | 2 ms
5,376 KB |
testcase_21 | AC | 1 ms
5,376 KB |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | AC | 40 ms
12,672 KB |
testcase_27 | AC | 264 ms
50,104 KB |
testcase_28 | AC | 266 ms
50,220 KB |
testcase_29 | AC | 257 ms
49,448 KB |
testcase_30 | AC | 140 ms
19,840 KB |
testcase_31 | AC | 126 ms
14,208 KB |
testcase_32 | AC | 114 ms
12,288 KB |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
testcase_37 | WA | - |
testcase_38 | WA | - |
testcase_39 | AC | 43 ms
39,780 KB |
testcase_40 | AC | 41 ms
39,784 KB |
ソースコード
#include <cassert> #include <cmath> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <bitset> #include <complex> #include <deque> #include <functional> #include <iostream> #include <limits> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using Int = long long; template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") struct HLD { int n, rt; // needs to be tree // vertex lists // modified in build(rt) (parent removed, heavy child first) vector<vector<int>> graph; vector<int> sz, par, dep; int zeit; vector<int> dis, fin, sid; // head vertex (minimum depth) in heavy path vector<int> head; HLD() : n(0), rt(-1), zeit(0) {} explicit HLD(int n_) : n(n_), rt(-1), graph(n), zeit(0) {} void ae(int u, int v) { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); graph[u].push_back(v); graph[v].push_back(u); } void dfsSz(int u) { sz[u] = 1; for (const int v : graph[u]) { auto it = std::find(graph[v].begin(), graph[v].end(), u); if (it != graph[v].end()) graph[v].erase(it); par[v] = u; dep[v] = dep[u] + 1; dfsSz(v); sz[u] += sz[v]; } } void dfsHLD(int u) { dis[u] = zeit++; const int deg = graph[u].size(); if (deg > 0) { int vm = graph[u][0]; int jm = 0; for (int j = 1; j < deg; ++j) { const int v = graph[u][j]; if (sz[vm] < sz[v]) { vm = v; jm = j; } } swap(graph[u][0], graph[u][jm]); head[vm] = head[u]; dfsHLD(vm); for (int j = 1; j < deg; ++j) { const int v = graph[u][j]; head[v] = v; dfsHLD(v); } } fin[u] = zeit; } void build(int rt_) { assert(0 <= rt_); assert(rt_ < n); rt = rt_; sz.assign(n, 0); par.assign(n, -1); dep.assign(n, -1); dep[rt] = 0; dfsSz(rt); zeit = 0; dis.assign(n, -1); fin.assign(n, -1); head.assign(n, -1); head[rt] = rt; dfsHLD(rt); assert(zeit == n); sid.assign(n, -1); for (int u = 0; u < n; ++u) sid[dis[u]] = u; } friend ostream &operator<<(ostream &os, const HLD &hld) { const int maxDep = *max_element(hld.dep.begin(), hld.dep.end()); vector<string> ss(2 * maxDep + 1); int pos = 0, maxPos = 0; for (int j = 0; j < hld.n; ++j) { const int u = hld.sid[j]; const int d = hld.dep[u]; if (hld.head[u] == u) { if (j != 0) { pos = maxPos + 1; ss[2 * d - 1].resize(pos, '-'); ss[2 * d - 1] += '+'; } } else { ss[2 * d - 1].resize(pos, ' '); ss[2 * d - 1] += '|'; } ss[2 * d].resize(pos, ' '); ss[2 * d] += std::to_string(u); if (maxPos < static_cast<int>(ss[2 * d].size())) { maxPos = ss[2 * d].size(); } } for (int d = 0; d <= 2 * maxDep; ++d) os << ss[d] << '\n'; return os; } bool contains(int u, int v) const { return (dis[u] <= dis[v] && dis[v] < fin[u]); } int lca(int u, int v) const { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); for (; head[u] != head[v]; ) (dis[u] > dis[v]) ? (u = par[head[u]]) : (v = par[head[v]]); return (dis[u] > dis[v]) ? v : u; } int jumpUp(int u, int d) const { assert(0 <= u); assert(u < n); assert(d >= 0); if (dep[u] < d) return -1; const int tar = dep[u] - d; for (u = head[u]; ; u = head[par[u]]) { if (dep[u] <= tar) return sid[dis[u] + (tar - dep[u])]; } } int jump(int u, int v, int d) const { assert(0 <= u); assert(u < n); assert(0 <= v); assert(v < n); assert(d >= 0); const int l = lca(u, v); const int du = dep[u] - dep[l], dv = dep[v] - dep[l]; if (d <= du) { return jumpUp(u, d); } else if (d <= du + dv) { return jumpUp(v, du + dv - d); } else { return -1; } } // [u, v) or [u, v] template <class F> void doPathUp(int u, int v, bool inclusive, F f) const { assert(contains(v, u)); for (; head[u] != head[v]; u = par[head[u]]) f(dis[head[u]], dis[u] + 1); if (inclusive) { f(dis[v], dis[u] + 1); } else { if (v != u) f(dis[v] + 1, dis[u] + 1); } } // not path order, include lca(u, v) or not template <class F> void doPath(int u, int v, bool inclusive, F f) const { const int l = lca(u, v); doPathUp(u, l, false, f); doPathUp(v, l, inclusive, f); } // (vs, ps): compressed tree // vs: DFS order (sorted by dis) // vs[ps[x]]: the parent of vs[x] // ids[vs[x]] = x, not set for non-tree vertex vector<int> ids; pair<vector<int>, vector<int>> compress(vector<int> us) { // O(n) first time ids.resize(n, -1); std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (dis[u] < dis[v]); }); us.erase(std::unique(us.begin(), us.end()), us.end()); int usLen = us.size(); assert(usLen >= 1); for (int x = 1; x < usLen; ++x) us.push_back(lca(us[x - 1], us[x])); std::sort(us.begin(), us.end(), [&](int u, int v) -> bool { return (dis[u] < dis[v]); }); us.erase(std::unique(us.begin(), us.end()), us.end()); usLen = us.size(); for (int x = 0; x < usLen; ++x) ids[us[x]] = x; vector<int> ps(usLen, -1); for (int x = 1; x < usLen; ++x) ps[x] = ids[lca(us[x - 1], us[x])]; return make_pair(us, ps); } }; //////////////////////////////////////////////////////////////////////////////// template <class T> void bAdd(vector<T> &bit, int pos, const T &val) { const int bitN = bit.size(); for (int x = pos; x < bitN; x |= x + 1) bit[x] += val; } template <class T> T bSum(const vector<T> &bit, int pos) { T ret = 0; for (int x = pos; x > 0; x &= x - 1) ret += bit[x - 1]; return ret; } template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) { return bSum(bit, pos1) - bSum(bit, pos0); } int N, M; vector<int> A, B; vector<vector<int>> G; vector<int> vis, on; vector<int> pari; void dfs(int u) { vis[u] = 1; for (const int i : G[u]) { const int v = B[i]; if (!vis[v]) { pari[v] = i; on[i] = 1; dfs(v); } } } int main() { for (; ~scanf("%d%d", &N, &M); ) { A.resize(M); B.resize(M); for (int i = 0; i < M; ++i) { scanf("%d%d", &A[i], &B[i]); --A[i]; --B[i]; } G.assign(N, {}); for (int i = 0; i < M; ++i) { G[A[i]].push_back(i); } vis.assign(N, 0); pari.assign(N, -1); on.assign(M, 0); HLD hld(N + 1); for (int u = 0; u < N; ++u) if (!vis[u]) { hld.ae(N, u); dfs(u); } for (int i = 0; i < M; ++i) if (on[i]) { hld.ae(A[i], B[i]); } hld.build(N); // cerr<<hld<<endl; vector<int> bit(N, 0); vector<int> del(M, 0); for (int i = 0; i < M; ++i) if (!on[i]) { if (hld.contains(B[i], A[i])) { // cerr<<"back "<<A[i]<<" "<<B[i]<<endl; int sum = 0; hld.doPathUp(A[i], B[i], false, [&](int x, int y) -> void { sum += bSum(bit, x, y); }); if (sum == 0) { del[i] = 1; for (int v = A[i]; v != B[i]; v = hld.par[v]) { del[pari[v]] = 1; bAdd(bit, hld.dis[v], +1); } } } } int ansCnt = 0; for (int i = 0; i < M; ++i) if (!del[i]) { ++ansCnt; } printf("%d %d\n", N, ansCnt); for (int i = 0; i < M; ++i) if (!del[i]) { printf("%d %d\n", A[i] + 1, B[i] + 1); } cerr<<"========"<<endl; } return 0; }