QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#76408 | #2214. Link Cut Digraph | alpha1022 | AC ✓ | 231ms | 18604kb | C++17 | 2.9kb | 2023-02-09 20:25:20 | 2023-02-09 20:25:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int BUFF_SIZE = 1 << 20;
char BUFF[BUFF_SIZE], *BB, *BE;
char gc() {
return BB == BE ? (BE = (BB = BUFF) + fread(BUFF, 1, BUFF_SIZE, stdin), BB == BE ? EOF : *BB++) : *BB++;
}
void read() {}
template<class T, class ...Ts>
void read(T &x, Ts &...args) {
x = 0;
char ch = 0, w = 0;
while (ch < '0' || ch > '9')
w |= ch == '-', ch = gc();
while (ch >= '0' && ch <= '9')
x = x * 10 + (ch ^ '0'), ch = gc();
if (w) x = -x;
read(args...);
}
using ll = long long;
const int N = 1e5;
const int M = 3e5;
int n, m;
vector<int> vec, e[N + 5];
int id[N + 5], low[N + 5], dfnTot, st[N + 5], top, bel[N + 5];
bool inStack[N + 5];
void clear() {
for (int u: vec)
e[u].clear(), id[u] = bel[u] = 0;
vec.clear();
}
void addEdge(int u, int v) { vec.push_back(u), vec.push_back(v), e[u].push_back(v); }
void dfs(int u) {
int &tot = dfnTot;
id[u] = low[u] = ++tot, st[++top] = u, inStack[u] = 1;
for (int v: e[u])
if (!id[v]) dfs(v), low[u] = min(low[u], low[v]);
else if (inStack[v]) low[u] = min(low[u], id[v]);
if (id[u] == low[u]) {
for (; st[top] != u; --top)
inStack[st[top]] = 0, bel[st[top]] = u;
inStack[u] = 0, bel[u] = u, --top;
}
}
void tarjan() {
dfnTot = 0;
for (int u: vec)
if (!id[u]) dfs(u);
}
tuple<int, int, int> g[M + 5];
void solve(int l, int r, int L, int R)
{
if (l == r || L > R) return;
int mid = (l + r) >> 1;
int Mid = R;
for (int i = R; i >= L; --i)
if (get<2>(g[i]) > mid)
swap(g[Mid--], g[i]);
clear();
for (int i = L; i <= Mid; ++i) addEdge(get<0>(g[i]), get<1>(g[i]));
tarjan();
for (int i = Mid; i >= L; --i)
if (bel[get<0>(g[i])] != bel[get<1>(g[i])] || !bel[get<0>(g[i])])
get<2>(g[i]) = mid + 1, swap(g[Mid--], g[i]);
for (int i = Mid + 1; i <= R; ++i) {
int u, v, id; tie(u, v, id) = g[i];
if (bel[u]) u = bel[u];
if (bel[v]) v = bel[v];
g[i] = make_tuple(u, v, id);
}
solve(l, mid, L, Mid), solve(mid + 1, r, Mid + 1, R);
}
ll ans;
struct UnionFind {
int fa[N + 5], siz[N + 5];
void init(int n) {
for (int i = 1; i <= n; ++i) siz[i] = 1;
}
bool isRoot(int x) { return !fa[x]; }
int find(int x) { return isRoot(x) ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
int fx = find(x), fy = find(y);
if (fx != fy)
ans += (ll)siz[fx] * siz[fy],
fa[fy] = fx, siz[fx] += siz[fy];
}
} uf;
int main() {
//freopen("graph.in", "r", stdin), freopen("graph.out", "w", stdout);
read(n, m);
for (int i = 1; i <= m; ++i) {
int u, v; read(u, v);
g[i] = make_tuple(u, v, i);
}
solve(0, m + 1, 1, m);
uf.init(n);
for (int i = 1, j = 1; i <= m; ++i) {
for (; j <= m && get<2>(g[j]) <= i; ++j)
uf.merge(get<0>(g[j]), get<1>(g[j]));
printf("%lld\n", ans);
}
}
详细
Test #1:
score: 100
Accepted
time: 211ms
memory: 18436kb
Test #2:
score: 0
Accepted
time: 208ms
memory: 18600kb
Test #3:
score: 0
Accepted
time: 219ms
memory: 18376kb
Test #4:
score: 0
Accepted
time: 210ms
memory: 17548kb
Test #5:
score: 0
Accepted
time: 214ms
memory: 18156kb
Test #6:
score: 0
Accepted
time: 219ms
memory: 18152kb
Test #7:
score: 0
Accepted
time: 222ms
memory: 18084kb
Test #8:
score: 0
Accepted
time: 206ms
memory: 18036kb
Test #9:
score: 0
Accepted
time: 194ms
memory: 17308kb
Test #10:
score: 0
Accepted
time: 207ms
memory: 18284kb
Test #11:
score: 0
Accepted
time: 211ms
memory: 18056kb
Test #12:
score: 0
Accepted
time: 216ms
memory: 18480kb
Test #13:
score: 0
Accepted
time: 201ms
memory: 17364kb
Test #14:
score: 0
Accepted
time: 189ms
memory: 17144kb
Test #15:
score: 0
Accepted
time: 95ms
memory: 15424kb
Test #16:
score: 0
Accepted
time: 179ms
memory: 17488kb
Test #17:
score: 0
Accepted
time: 231ms
memory: 17772kb
Test #18:
score: 0
Accepted
time: 173ms
memory: 17832kb
Test #19:
score: 0
Accepted
time: 216ms
memory: 18604kb
Test #20:
score: 0
Accepted
time: 192ms
memory: 17504kb
Test #21:
score: 0
Accepted
time: 193ms
memory: 17952kb
Test #22:
score: 0
Accepted
time: 196ms
memory: 17916kb
Test #23:
score: 0
Accepted
time: 212ms
memory: 18028kb
Test #24:
score: 0
Accepted
time: 203ms
memory: 17360kb
Test #25:
score: 0
Accepted
time: 185ms
memory: 17572kb
Test #26:
score: 0
Accepted
time: 189ms
memory: 17860kb
Test #27:
score: 0
Accepted
time: 181ms
memory: 17072kb