hdu 3849 求无向图的桥

#include<cstdio>
#include<cstring>
#include<map>
#define MIN(x,y) ((x)>(y)?(y):(x))
using namespace std;
int n,m; 
struct node
{
	char str[20];
	bool operator<(const node &b)const
	{
		return strcmp(str,b.str)<0;
	}
};
map<node,int>has;
struct edge
{
	node u,v;
	bool flag;
}b[201000];
struct node1
{
	int to,next;
}e[201000];
int head[201000],cnt;
int pre[10200],d[10200];
void add_edge(int from,int to)
{
	e[cnt].to=to;
	e[cnt].next=head[from];
	head[from]=cnt++;
//	printf("%d %d %d\n",e[cnt-1].to,e[cnt-1].next,head[from]); 
}
void dfs(int u,int v,int depth)
{
	pre[u]=d[u]=depth;
//	printf("in %d\n",u); 
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		
		int to=e[i].to;
//		printf("u=%d to=%d\n",u,to);
		if(to==v)
		continue;
		if(!d[to])
		{
			dfs(to,u,depth+1);
			pre[u]=MIN(pre[u],pre[to]);
		}
		else
		pre[u]=MIN(pre[u],d[to]);
	}
} 
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		has.clear();
		memset(d,0,sizeof(d));
		memset(head,-1,sizeof(head));
		cnt=0;
		int tot=0;
		for(int i=0;i<m;i++)
		{
			b[i].flag=0;
			scanf("%s%s",b[i].u.str,b[i].v.str);
			if(has.find(b[i].u)==has.end())
			has[b[i].u]=++tot;
			if(has.find(b[i].v)==has.end())
			has[b[i].v]=++tot;
			int x=has[b[i].u],y=has[b[i].v];
//			printf("x=%d y=%d\n",x,y);
			add_edge(x,y);
			add_edge(y,x);
		}
//		printf("cnt=%d\n",cnt); 
		dfs(1,-1,1);
//		for(int i=1;i<=n;i++)
//		printf("%d ",d[i]);
//		printf("\n");
//		for(int i=1;i<=n;i++)
//		printf("%d ",pre[i]);
//		printf("\n");
		int sym=0;
		for(int i=1;i<=n;i++)
		if(!d[i])
		{
			sym=1;
//			printf("here\n");
			break;
		}
		if(sym)
		printf("0\n");
		else
		{
			int res=0;
			for(int i=0;i<m;i++)
			{
				int u=has[b[i].u],v=has[b[i].v];
				if(pre[u]>d[v]||pre[v]>d[u])
				{
					b[i].flag=1;
					res++;
				}
			}
			printf("%d\n",res);
			for(int i=0;i<m;i++)
			if(b[i].flag)
			{
				printf("%s %s\n",b[i].u.str,b[i].v.str);
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值