QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#61069#2214. Link Cut DigraphmamamWA 590ms67448kbC++142.3kb2022-11-09 19:44:092022-11-09 19:44:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-09 19:44:10]
  • 评测
  • 测评结果:WA
  • 用时:590ms
  • 内存:67448kb
  • [2022-11-09 19:44:09]
  • 提交

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