QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#423811#2214. Link Cut DigraphluanmengleiAC ✓207ms80280kbC++172.5kb2024-05-28 17:10:532024-05-28 17:10:53

Judging History

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

  • [2024-05-28 17:10:53]
  • 评测
  • 测评结果:AC
  • 用时:207ms
  • 内存:80280kb
  • [2024-05-28 17:10:53]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

namespace SOL {

using i64 = long long;
void debug(const char *msg, ...) {
#ifdef CLESIP
    va_list arg;
    static char pbString[512];
    va_start(arg,msg);
    vsprintf(pbString,msg,arg);
    cerr << "[DEBUG] " << pbString << "\n";
    va_end(arg);
#endif    
}
template<typename T, typename L>
bool chkmax(T &x, L y) { if (x < y) return x = y, true; return false; }
template<typename T, typename L>
bool chkmin(T &x, L y) { if (y < x) return x = y, true; return false; }

const int N = 3e5 + 10;
int n, m, dfn[N], low[N], scc[N], st[N], tp, cnt, tot, c[N], sze[N];
i64 ans;
bool in[N];
vector<int> G[N], s[N];
array<int, 2> edge[N];

void tarjan(int x) {
	dfn[x] = low[x] = ++ cnt, in[x] = 1, st[++ tp] = x;
	for (int y : G[x]) {
		if (!dfn[y]) {
			tarjan(y);
			chkmin(low[x], low[y]);
		} else if (in[y]) {
			chkmin(low[x], dfn[y]);
		}
	}
	if (low[x] == dfn[x]) {
		int p = 0; ++ tot;
		do {
			p = st[tp --];
			in[p] = 0, scc[p] = tot;
		} while (p != x);
	}
}

void solve(int l, int r, vector<array<int, 3>> e) {
	if (l == r) {
		for (auto [x, y, id] : e)
			s[l].push_back(id);
		return;
	}
	int mid = (l + r) >> 1;
	for (auto [x, y, id] : e) {
		dfn[x] = dfn[y] = in[x] = in[y] = 0;
		G[x].clear(), G[y].clear();
	}
	tp = cnt = tot = 0;
	for (auto [x, y, id] : e)
		if (id <= mid)
			G[x].push_back(y);
	for (auto [x, y, id] : e) {
		if (!dfn[x])
			tarjan(x);
		if (!dfn[y])
			tarjan(y);
	}
	vector<array<int, 3>> nl, nr;
	for (auto [x, y, id] : e) {
		if (scc[x] == scc[y]) {
			nl.push_back({ x, y, id });
		} else {
			nr.push_back({ scc[x], scc[y], id });
		}
	}
	solve(l, mid, nl);
	solve(mid + 1, r, nr);
}

int find(int x) { return c[x] == x ? x : c[x] = find(c[x]); }

void merge(int x, int y) {
	x = find(x), y = find(y);
	if (x == y)
		return;
	ans -= 1ll * sze[x] * (sze[x] - 1) / 2;
	ans -= 1ll * sze[y] * (sze[y] - 1) / 2;
	c[x] = y, sze[y] += sze[x];
	ans += 1ll * sze[y] * (sze[y] - 1) / 2;
}

void __solve() {
	cin >> n >> m;
	vector<array<int, 3>> vec;
	for (int i = 1, x, y; i <= m; i ++) {
		cin >> x >> y;
		edge[i] = { x, y };
		vec.push_back({ x, y, i });
	}
	solve(1, m + 1, vec);
	for (int i = 1; i <= n; i ++)
		c[i] = i, sze[i] = 1;
	ans = 0;
	for (int i = 1; i <= m; i ++) {
		for (int id : s[i])
			merge(edge[id][0], edge[id][1]);
		cout << ans << "\n";
	}
}

}
int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	SOL::__solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 200ms
memory: 73288kb

Test #2:

score: 0
Accepted
time: 187ms
memory: 72668kb

Test #3:

score: 0
Accepted
time: 200ms
memory: 73004kb

Test #4:

score: 0
Accepted
time: 179ms
memory: 78184kb

Test #5:

score: 0
Accepted
time: 196ms
memory: 69892kb

Test #6:

score: 0
Accepted
time: 204ms
memory: 71240kb

Test #7:

score: 0
Accepted
time: 196ms
memory: 71184kb

Test #8:

score: 0
Accepted
time: 193ms
memory: 72072kb

Test #9:

score: 0
Accepted
time: 166ms
memory: 80280kb

Test #10:

score: 0
Accepted
time: 207ms
memory: 72084kb

Test #11:

score: 0
Accepted
time: 191ms
memory: 71448kb

Test #12:

score: 0
Accepted
time: 189ms
memory: 71652kb

Test #13:

score: 0
Accepted
time: 175ms
memory: 80252kb

Test #14:

score: 0
Accepted
time: 180ms
memory: 77784kb

Test #15:

score: 0
Accepted
time: 111ms
memory: 63192kb

Test #16:

score: 0
Accepted
time: 190ms
memory: 60292kb

Test #17:

score: 0
Accepted
time: 185ms
memory: 59924kb

Test #18:

score: 0
Accepted
time: 178ms
memory: 59112kb

Test #19:

score: 0
Accepted
time: 203ms
memory: 71800kb

Test #20:

score: 0
Accepted
time: 189ms
memory: 73692kb

Test #21:

score: 0
Accepted
time: 188ms
memory: 71480kb

Test #22:

score: 0
Accepted
time: 203ms
memory: 75248kb

Test #23:

score: 0
Accepted
time: 172ms
memory: 74428kb

Test #24:

score: 0
Accepted
time: 189ms
memory: 74920kb

Test #25:

score: 0
Accepted
time: 172ms
memory: 74516kb

Test #26:

score: 0
Accepted
time: 191ms
memory: 73112kb

Test #27:

score: 0
Accepted
time: 186ms
memory: 69120kb