跳到正文

题解

[图论] 边三连通分量

Felix72约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月12日
篇幅
约 7 分钟
获赞
2
题目
P6658

版本

  1. v1当前

边三连通分量的定义是一个割掉任意两条边仍然联通的点集。

我们建出 DFS 树,考虑割掉边的方案:

  • 割一条树边,满足它是桥;
  • 割一条树边和一条非树边,满足这条非树边是覆盖这条树边的唯一非树边;
  • 割两条树边,满足这两条树边所对应的非树边集合一致。

我们使用异或哈希来求解这个问题。考虑构造一种方案,给每个节点一个哈希值。最后两个节点的哈希值相同即这两个节点在同一个边三连通分量中。

对于第一种情况,我们给每条割边对应的子树打一个随机数异或标记;

对于第二种情况,我们给满足这个限制的所有树边的子树打一个随机数异或标记;

对于第三种情况,考虑到满足条件的两条边一定是祖孙边,我们先用哈希维护最近的满足条件的边,再给两条边之间的连通块打一个随机数异或标记。给连通块打标记的方法可以拆解为先给深度更小的边对应子树打一个随机数异或标记,再给深度更大的边对应的子树打一个同样的异或标记容斥掉影响。

最后求出每个点的标记异或和即可。

C++
 /* NE_Cat 4.3 */
#include <bits/stdc++.h>
#define lowbit(x) ((x) & (-(x)))
using namespace std;

const int N = 500010;
typedef unsigned long long ull;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n, m; vector < int > edge[N], tr[N], extend[N];
bool vis[N], is_root[N]; int dep[N];

vector < int > tricc[N]; int tricc_n;
inline bool cmp(vector < int > u, vector < int > v) {return u.front() < v.front();}

inline void build(int pos)
{
	vis[pos] = true;
	for(int to : edge[pos])
	{
		if(!vis[to])
		{
			dep[to] = dep[pos] + 1;
			tr[pos].push_back(to);
			build(to);
		}
		else if(dep[to] > dep[pos]) extend[pos].push_back(to);
	}
}

const int P = 1e6 + 3;
struct Hash_Table
{
	vector < ull > state[P]; vector < int > con[P];
	bool vis[P]; queue < int > q;
	inline void refresh(int id) {if(!vis[id]) vis[id] = true, q.push(id);}
	inline void clear()
	{
		while(!q.empty())
		{
			int id = q.front(); q.pop();
			vis[id] = false;
			state[id].clear(); con[id].clear();
		}
	}
	inline void modify(ull num, int data)
	{
		int id = num % P; refresh(id);
		for(int i = 0; i < (int)state[id].size(); ++i)
			if(state[id][i] == num)
				{con[id][i] = data; return ;}
		state[id].push_back(num); con[id].push_back(data);
	}
	inline int get(ull num)
	{
		int id = num % P;
		for(int i = 0; i < (int)state[id].size(); ++i)
			if(state[id][i] == num)
				return con[id][i];
		return 0;
	}
}; Hash_Table mp;
ull tag[N], w[N]; int len[N];
struct node {ull num; int id;} nd[N];
inline bool cmp_node(node u, node v)
{
	if(u.num == v.num) return u.id < v.id;
	return u.num < v.num;
}
inline void work(int pos)
{
	for(int to : tr[pos])
	{
		work(to);
		tag[pos] ^= tag[to];
		len[pos] += len[to];
	}
	if(len[pos] <= 1) w[pos] ^= rnd();
	if(tag[pos] == 0) w[pos] ^= rnd();
	else
	{
		ull cur = tag[pos]; int x = mp.get(cur);
		if(x)
		{
			ull hash_num = rnd();
			w[x] ^= hash_num;
			w[pos] ^= hash_num;
		}
		mp.modify(cur, pos);
	}
}
inline void get(int pos, ull num)
{
	num ^= w[pos]; nd[pos].num = num;
	for(int to : tr[pos]) get(to, num);
}

int main()
{
//	freopen("text.in", "r", stdin);
//	freopen("prog.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1, x, y; i <= m; ++i)
	{
		cin >> x >> y;
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	for(int i = 1; i <= n; ++i) if(!vis[i]) is_root[i] = true, build(i);
//	cerr << "tree" << '\n';
//	for(int i = 1; i <= n; ++i)
//		for(int j : tr[i])
//			cerr << i << " " << j << '\n';
//	cerr << "extend" << '\n';
//	for(int i = 1; i <= n; ++i)
//		for(int j : extend[i])
//			cerr << i << " " << j << '\n';
	for(int i = 1; i <= n; ++i)
		for(int j : extend[i])
			{ull cur = rnd(); tag[j] ^= cur, tag[i] ^= cur; ++len[j], --len[i];}
	for(int i = 1; i <= n; ++i) nd[i].id = i;
	for(int i = 1; i <= n; ++i) if(is_root[i]) mp.clear(), work(i), get(i, 0);
	sort(nd + 1, nd + n + 1, cmp_node);
	for(int l = 1, r = 0; l <= n; ++l)
	{
		r = l; while(r < n && nd[r + 1].num == nd[r].num) ++r;
		++tricc_n; for(int i = l; i <= r; ++i) tricc[tricc_n].push_back(nd[i].id);
		l = r;
	}
	sort(tricc + 1, tricc + tricc_n + 1, cmp);
	cout << tricc_n << '\n';
	for(int i = 1; i <= tricc_n; ++i)
	{
		for(int x : tricc[i]) cout << x << " ";
		cout << '\n';
	}
	return 0;
}
/*

*/
原文