Codeforces 962F Simple Cycles Edges (连通分量 求割点)

本文介绍了一种算法,用于确定无向图中哪些边恰好属于一个简单环。通过使用Tarjan算法找到割点和连通块,若连通块内的顶点数等于边数,则该连通块形成一个环。最后,输出属于唯一简单环的边的索引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Simple Cycles Edges
You are given an undirected graph, consisting of n vertices and m edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).

A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn’t allow to visit a vertex more than once in a cycle.

Determine the edges, which belong to exactly on one simple cycle.

Input
The first line contain two integers n and m (1≤n≤100000, 0≤m≤min(n⋅(n−1)/2,100000)) — the number of vertices and the number of edges.

Each of the following m lines contain two integers u and v (1≤u,v≤n, u≠v) — the description of the edges.

Output
In the first line print the number of edges, which belong to exactly one simple cycle.

In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.

Examples
Input
3 3
1 2
2 3
3 1
Output
3
1 2 3
Input
6 7
2 3
3 4
4 2
1 2
1 5
5 6
6 1
Output
6
1 2 3 5 6 7
Input
5 6
1 2
2 3
2 4
4 3
2 5
5 3
Output
0

一开始用set想直接去重排序,超时
找割点,求连通块,如果连通块的点的数量等于边的数量,就是一个环

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<map>
#include<set>
#include<math.h>
#include<vector>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
#define mem(a, b) memset(a, b, sizeof(a))
#define N 100001
using namespace std;
int n, m;
int head[N], ver[N*2], Next[N*2];
int low[N], dfn[N], tot, num, root, tt, top, blo[2*N];
int st[N*3];
vector<int>edge[N], ans, node[N]; 
void add(int x, int y) {//邻接表 
	ver[++tot]=y, Next[tot]=head[x], head[x]=tot;
} 

void tarjan(int x, int f) {
	dfn[x]=low[x]=++num;
	for(int i=head[x]; i; i=Next[i]) {
		int y=ver[i];
		if(!dfn[y]) {
			st[++top]=i>>1, st[++top]=x, st[++top]=y;
			tarjan(y, x);
			low[x]=min(low[x], low[y]);
			
			if(low[y]>=dfn[x]) {//割点 
				tt++;
				while(1) {
					int t1=st[top--], t2=st[top--];
					if(blo[t1]!=tt){//判断这个点是不是已经加入过了 
						node[tt].push_back(t1);//加入两个连接的点 
						blo[t1]=tt;
					}
					if(blo[t2]!=tt){
						node[tt].push_back(t2); 
						blo[t2]=tt;
					}
					edge[tt].push_back(st[top--]);//加入连接两个点的边 
					if(t1==y && t2==x) break;
				}
			}	
		}
		else if(dfn[y]<dfn[x] && y!=f) {//y不是x的父节点,且y的时间戳小于x 
			st[++top]=i>>1, st[++top]=x, st[++top]=y;
			low[x]=min(low[x],dfn[y]);
		}
	}
}

int main() {
	cin>>n>>m;
	tot=1;
	for(int i=1; i<=m; i++) {
		int x, y;
		scanf("%d%d",&x, &y);
		if(x==y) continue;
		add(x, y), add(y, x);
	}
	for(int i=1; i<=n; i++) 
		if(!dfn[i]) tarjan(i, 0);	

	for (int i = 1; i <= tt; i++) {//连通块的点数等于边数,是环 
		
    	if (edge[i].size() == node[i].size()) {
      		for(int j=0; j<edge[i].size(); j++)
      			ans.push_back(edge[i][j]);
 	   	}
  	}
	sort(ans.begin(), ans.end());//排序
  	printf("%d\n", (int)ans.size());
  	for (int i=0; i<ans.size(); i++) 
	  	printf("%d ", ans[i]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值