QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#423809#2214. Link Cut DigraphluanmengleiWA 195ms70768kbC++172.5kb2024-05-28 17:08:572024-05-28 17:08:58

Judging History

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

  • [2024-05-28 17:08:58]
  • 评测
  • 测评结果:WA
  • 用时:195ms
  • 内存:70768kb
  • [2024-05-28 17:08:57]
  • 提交

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, 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: 0
Wrong Answer
time: 195ms
memory: 70768kb