QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#18335 | #2214. Link Cut Digraph | Komeiji | AC ✓ | 483ms | 38040kb | C++20 | 2.7kb | 2022-01-17 21:30:30 | 2022-05-04 17:58:40 |
Judging History
answer
#include <bits/stdc++.h>
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::vector<std::pair<int, int>> edges;
std::vector<int> f(n + 1), vis(n + 1), dfn(n + 1), low(n + 1), scc(n + 1), ins(n + 1), siz(n + 1, 1);
std::vector<long long> ans(m + 1);
std::iota(f.begin(), f.end(), 0);
std::stack<int> stack;
int curt = 0, dfnt = 0, stot = 0;
for (int i = 0; i < m; ++i) {
int x, y;
std::cin >> x >> y;
edges.emplace_back(x, y);
}
auto find = [&](int x) {
while (x != f[x])
x = f[x] = f[f[x]];
return x;
};
std::vector<std::vector<int>> G(n + 1, std::vector<int>());
std::function<void(int)> tarjan = [&](int x) {
low[x] = dfn[x] = ++dfnt;
stack.emplace(x);
ins[x] = true;
for (int y : G[x]) {
if (!dfn[y]) {
tarjan(y);
low[x] = std::min(low[x], low[y]);
}
else if (ins[y]) {
low[x] = std::min(low[x], dfn[y]);
}
}
if (dfn[x] == low[x]) {
++stot;
int y;
do {
y = stack.top(); stack.pop();
ins[y] = false;
scc[y] = stot;
} while (y != x);
}
};
std::function<void(int, int, std::vector<int>)> solve = [&](int l, int r, std::vector<int> eids) {
const int mid = l + r >> 1;
++curt;
std::vector<int> nodes;
auto push = [&](int x) {
if (vis[x] != curt) {
vis[x] = curt;
nodes.push_back(x);
}
};
for (int o : eids) {
if (o <= mid) {
auto [x, y] = edges[o];
int _x = x, _y = y;
x = find(x), y = find(y);
push(x), push(y);
if (x != y) {
G[x].push_back(y);
}
}
}
for (int x : nodes)
assert(!dfn[x]);
for (int x : nodes)
if (!dfn[x])
tarjan(x);
if (l == r) {
if (l) ans[l] = ans[l - 1];
for (int o: eids) {
auto [x, y] = edges[o];
if (find(x) != find(y) && scc[find(x)] == scc[find(y)]) {
int _x = x, _y = y;
x = find(x);
y = find(y);
ans[l] += 1ll * siz[x] * siz[y];
siz[y] += siz[x], siz[x] = 0;
f[x] = y;
}
}
for (int x : nodes) G[x].clear(), dfn[x] = low[x] = ins[x] = scc[x] = 0;
dfnt = stot = 0;
while (!stack.empty())
stack.pop();
}
else {
std::vector<int> le, ri;
for (int o : eids) {
auto [x, y] = edges[o];
if (find(x) == find(y)) continue;
if (o <= mid && scc[find(x)] == scc[find(y)]) {
le.push_back(o);
}
else {
ri.push_back(o);
}
}
for (int x : nodes) G[x].clear(), dfn[x] = low[x] = ins[x] = scc[x] = 0;
dfnt = stot = 0;
while (!stack.empty())
stack.pop();
solve(l, mid, le);
solve(mid + 1, r, ri);
}
};
std::vector<int> eids(m);
std::iota(eids.begin(), eids.end(), 0);
solve(0, m - 1, eids);
for (int i = 0; i < m; ++i)
std::cout << ans[i] << '\n';
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 462ms
memory: 34532kb
Test #2:
score: 0
Accepted
time: 444ms
memory: 34340kb
Test #3:
score: 0
Accepted
time: 475ms
memory: 34648kb
Test #4:
score: 0
Accepted
time: 433ms
memory: 37520kb
Test #5:
score: 0
Accepted
time: 480ms
memory: 34000kb
Test #6:
score: 0
Accepted
time: 445ms
memory: 33988kb
Test #7:
score: 0
Accepted
time: 441ms
memory: 33996kb
Test #8:
score: 0
Accepted
time: 459ms
memory: 34604kb
Test #9:
score: 0
Accepted
time: 373ms
memory: 38040kb
Test #10:
score: 0
Accepted
time: 483ms
memory: 35128kb
Test #11:
score: 0
Accepted
time: 483ms
memory: 34164kb
Test #12:
score: 0
Accepted
time: 462ms
memory: 34432kb
Test #13:
score: 0
Accepted
time: 339ms
memory: 37904kb
Test #14:
score: 0
Accepted
time: 342ms
memory: 37880kb
Test #15:
score: 0
Accepted
time: 129ms
memory: 21728kb
Test #16:
score: 0
Accepted
time: 381ms
memory: 28788kb
Test #17:
score: 0
Accepted
time: 401ms
memory: 29040kb
Test #18:
score: 0
Accepted
time: 388ms
memory: 28724kb
Test #19:
score: 0
Accepted
time: 446ms
memory: 34528kb
Test #20:
score: 0
Accepted
time: 401ms
memory: 36152kb
Test #21:
score: 0
Accepted
time: 427ms
memory: 35940kb
Test #22:
score: 0
Accepted
time: 422ms
memory: 35592kb
Test #23:
score: 0
Accepted
time: 446ms
memory: 36112kb
Test #24:
score: 0
Accepted
time: 432ms
memory: 35840kb
Test #25:
score: 0
Accepted
time: 414ms
memory: 35624kb
Test #26:
score: 0
Accepted
time: 413ms
memory: 35136kb
Test #27:
score: 0
Accepted
time: 413ms
memory: 34256kb