QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#61078#2214. Link Cut DigraphmamamAC ✓612ms102984kbC++142.4kb2022-11-09 20:04:262022-11-09 20:04:29

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 20:04:29]
  • 评测
  • 测评结果:AC
  • 用时:612ms
  • 内存:102984kb
  • [2022-11-09 20:04:26]
  • 提交

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) {
	
	/* if (l == 4 && r == 6) {
		for (int t: cur) cerr << t << ' ';
		cerr << '\n';
	} */
	/* 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: 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(eu[t])] == col[find(ev[t])]) eL.pb(t);
		else eR.pb(t);
	for (int t: vec) g[t].clear(), dfn[t] = low[t] = col[t] = 0;
	
	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: 100
Accepted
time: 592ms
memory: 68404kb

Test #2:

score: 0
Accepted
time: 588ms
memory: 67348kb

Test #3:

score: 0
Accepted
time: 573ms
memory: 68192kb

Test #4:

score: 0
Accepted
time: 605ms
memory: 85372kb

Test #5:

score: 0
Accepted
time: 603ms
memory: 67392kb

Test #6:

score: 0
Accepted
time: 597ms
memory: 67608kb

Test #7:

score: 0
Accepted
time: 597ms
memory: 66660kb

Test #8:

score: 0
Accepted
time: 586ms
memory: 67628kb

Test #9:

score: 0
Accepted
time: 551ms
memory: 102984kb

Test #10:

score: 0
Accepted
time: 601ms
memory: 67428kb

Test #11:

score: 0
Accepted
time: 583ms
memory: 66912kb

Test #12:

score: 0
Accepted
time: 590ms
memory: 67464kb

Test #13:

score: 0
Accepted
time: 515ms
memory: 102788kb

Test #14:

score: 0
Accepted
time: 505ms
memory: 102516kb

Test #15:

score: 0
Accepted
time: 244ms
memory: 39624kb

Test #16:

score: 0
Accepted
time: 520ms
memory: 45060kb

Test #17:

score: 0
Accepted
time: 509ms
memory: 45324kb

Test #18:

score: 0
Accepted
time: 524ms
memory: 44972kb

Test #19:

score: 0
Accepted
time: 578ms
memory: 67168kb

Test #20:

score: 0
Accepted
time: 589ms
memory: 80584kb

Test #21:

score: 0
Accepted
time: 584ms
memory: 80552kb

Test #22:

score: 0
Accepted
time: 568ms
memory: 80092kb

Test #23:

score: 0
Accepted
time: 612ms
memory: 80500kb

Test #24:

score: 0
Accepted
time: 597ms
memory: 80472kb

Test #25:

score: 0
Accepted
time: 589ms
memory: 81012kb

Test #26:

score: 0
Accepted
time: 594ms
memory: 79620kb

Test #27:

score: 0
Accepted
time: 593ms
memory: 74264kb