QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#288475 | #7877. Balanced Array | ckiseki# | RE | 0ms | 0kb | C++20 | 4.4kb | 2023-12-22 18:39:33 | 2023-12-22 18:39:34 |
answer
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int f = 0;
(..., (cerr << (f++ ? ", " : "") << a));
cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
cerr << "\e[1;33m[ " << s << " ] = [ ";
using namespace experimental;
copy(L, R, make_ostream_joiner(cerr, ", "));
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
const int mod = 998244353;
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int sub(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
int mul(int64_t a, int64_t b) {
return static_cast<int>(a * b % mod);
}
int modpow(int e, int p) {
int r = 1;
while (p) {
if (p & 1) r = mul(r, e);
e = mul(e, e);
p >>= 1;
}
return r;
}
int modinv(int x) {
return modpow(x, mod - 2);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<vector<pair<int,int>>> g(n);
vector<vector<pair<int,int>>> rev(n);
for (int i = 0; i < m; i++) {
int u, v, p, q;
cin >> u >> v >> p >> q;
--u, --v;
g[u].emplace_back(v, mul(p, modinv(q)));
}
vector<int> vis(n), inStk(n);
vector<array<int, 20>> pa(n);
vector<array<int, 20>> prod(n);
for (int l = 0; l < 20; l++)
prod[0][l] = 1;
vector<int> dep(n);
vector<int> dfn(n);
int dfc = 0;
const auto dfs = [&](auto self, int i) -> void {
dfn[i] = dfc++;
vis[i] = true;
inStk[i] = true;
for (auto [j, w] : g[i]) {
if (!vis[j]) {
dep[j] = dep[i] + 1;
pa[j][0] = i;
prod[j][0] = w;
for (int l = 0; l + 1 < 20; l++)
pa[j][l + 1] = pa[pa[j][l]][l];
for (int l = 0; l + 1 < 20; l++)
prod[j][l + 1] = mul(prod[j][l], prod[pa[j][l]][l]);
// T[i].emplace_back(j, w);
self(self, j);
} else {
if (!inStk[j])
rev[j].emplace_back(i, w);
}
}
inStk[i] = false;
};
dfs(dfs, 0);
const auto lca = [&](int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
int d = dep[b] - dep[a];
for (int i = 0; i < 20; i++)
if (d >> i & 1)
b = pa[b][i];
if (a == b)
return a;
for (int i = 19; i >= 0; i--)
if (pa[a][i] != pa[b][i]) {
a = pa[a][i];
b = pa[b][i];
}
return pa[a][0];
};
const auto chainProd = [&](int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
int d = dep[b] - dep[a];
int p = 1;
for (int i = 0; i < 20; i++)
if (d >> i & 1) {
p = mul(p, prod[b][i]);
b = pa[b][i];
}
assert(a == b);
return p;
};
const auto build = [&](vector<int> vs, int r) {
vector<pair<int,int>> res;
sort(all(vs), [&dfn](int i, int j) {
return dfn[i] < dfn[j];
});
vector<int> s = {r};
for (int v : vs) if (v != r) {
if (int o = lca(v, s.back()); o != s.back()) {
while (s.size() >= 2) {
if (dfn[s[s.size() - 2]] < dfn[o]) break;
res.emplace_back(s[s.size() - 2], s.back());
s.pop_back();
}
if (s.back() != o) {
res.emplace_back(o, s.back());
s.back() = o;
}
}
s.push_back(v);
}
for (size_t i = 1; i < s.size(); ++i)
res.emplace_back(s[i - 1], s[i]);
return res;
};
vector<vector<pair<int,int>>> T(n);
vector<vector<int>> virtual_leaf(n);
const auto dfs2 = [&](auto self, int i) -> int {
int p = 1;
for (int w : virtual_leaf[i])
p = mul(p, sub(1, w));
for (auto [j, w] : T[i]) {
int q = self(self, j);
p = add(mul(p, sub(1, w)), mul(q, w));
}
return p;
};
vector<int> ans(n);
for (int i = 0; i < n; i++) {
vector<int> vs = {i};
virtual_leaf[i].emplace_back(1);
for (auto [j, w] : rev[i]) {
vs.emplace_back(j);
virtual_leaf[j].emplace_back(w);
}
debug(i + 1);
auto e = build(vs, 0);
for (auto [x, y] : e) {
T[x].emplace_back(y, chainProd(x, y));
debug(x + 1, y + 1, chainProd(x, y));
}
ans[i] = sub(1, dfs2(dfs2, 0));
for (auto [x, y] : e) {
T[x].clear();
}
for (int x : vs) {
virtual_leaf[x].clear();
}
}
for (int i = 0; i < n; i++)
cout << ans[i] << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 1 2 3