QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#61069 | #2214. Link Cut Digraph | mamam | WA | 590ms | 67448kb | C++14 | 2.3kb | 2022-11-09 19:44:09 | 2022-11-09 19:44:10 |
Judging History
answer
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
#define pb push_back
const int NR = 1e5 + 10, MR = 3 * 1e5 + 10;
int n, m;
int eu[MR], ev[MR];
int dcnt, ccnt, dfn[NR], low[NR], col[NR];
int dsu[NR], sz[NR];
bool inS[NR];
LL sum;
LL ans[MR];
vi g[NR];
stack<int> st;
vector<vi> vvec;
int find(int x) {
if (dsu[x] == x) return x;
return dsu[x] = find(dsu[x]);
}
void tarjan(int now) {
dfn[now] = low[now] = ++dcnt;
st.push(now), inS[now] = 1;
for (int t: g[now])
if (!dfn[t]) tarjan(t), low[now] = min(low[now], low[t]);
else if (inS[t]) low[now] = min(low[now], low[t]);
if (dfn[now] == low[now]) {
ccnt++;
LL s1 = 0, s2 = 0;
vi tmp;
while (1) {
int cur = st.top(); st.pop();
col[cur] = ccnt, inS[cur] = 0, tmp.pb(cur);
s1 += sz[cur], s2 += 1ll * sz[cur] * sz[cur];
if (cur == now) break;
}
sum += s1 * s1 - s2, vvec.pb(tmp);
}
}
void solve(int l, int r, vi cur, LL res) {
/* cerr << l << ' ' << r << '\n';
for (int i = 1; i <= n; i++) cerr << find(i) << ' ';
cerr << '\n'; */
if (l == r) {
ans[l - 1] = res;
return;
}
int mid = (l + r) >> 1;
vi vec;
for (int t: cur) if (t <= mid && find(eu[t]) != find(ev[t])) vec.pb(find(eu[t])), vec.pb(find(ev[t]));
dcnt = ccnt = 0;
sum = 0, vvec.clear();
for (int t: vec) g[t].clear(), dfn[t] = low[t] = col[t] = 0;
for (int t: cur) if (t <= mid) g[find(eu[t])].pb(find(ev[t]));
for (int t: vec) if (!dfn[t]) tarjan(t);
auto cvec = vvec;
LL tsum = sum;
vi eL, eR;
for (int t: cur)
if (col[find(eu[t])] == col[find(ev[t])]) eL.pb(t);
else eR.pb(t);
solve(l, mid, eL, res);
for (auto tvec: cvec) {
int rt = find(tvec[0]);
for (int i = 1; i < tvec.size(); i++) {
int u = find(tvec[i]);
if (dsu[u] != rt) dsu[u] = rt, sz[rt] += sz[u];
}
}
solve(mid + 1, r, eR, res + tsum);
}
int main()
{
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> eu[i] >> ev[i];
for (int i = 1; i <= n; i++) dsu[i] = i, sz[i] = 1;
vector<int> E;
for (int i = 1; i <= m; i++) E.pb(i);
solve(1, m + 1, E, 0);
for (int i = 1; i <= m; i++) cout << ans[i] / 2 << '\n';
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 590ms
memory: 67448kb