HDU 3849 By Recognizing…(求无向图的桥数目)

本文介绍了一种用于检测图中桥边的有效算法,并提供了两种实现方式,一种使用了字符串映射,另一种则更为高效。通过对无向图进行深度优先搜索(DFS),算法能够找出所有关键的连接边,即桥边,这些边一旦移除会导致图变得不连通。

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

题意:给你一个无向图(可能不连通,但是无自环,无重边),如果本图不连通,那么直接输出0。否则要你输出图中的每条桥边,要求按输入边的顺序输出。

思路:由于要按输入边的顺序输出,所以只能等DFS之后再输出。

           对于边(u,v)来说,只要low[u]>pre[v]或者low[v]>pre[u],那么就是桥

Trick:由于有字符串输入,我用的map+string映射,超时了几次...后来把v!=fa这个判断提前了之后995MS险过...


/* 995MS..用的string+map*/
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 10005
#define maxm 100000+10
#define LL long long
int cas=1,T;
int n,m;
int sum = 0;
int dfs_clock;            //时钟,每访问一个结点增1
vector<int>G[maxn];       //图
int pre[maxn];            //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问
int low[maxn];            //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值
//bool iscut[maxn];         //标记i结点是不是一个割点
//int cut[maxn];            //切割这个结点后的儿子数
//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥
//初始调用dfs(root,-1)
int dfs(int u,int fa)
{
	int lowu=pre[u]=++dfs_clock;
	int child = 0;                //子结点数目
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i];
		if (v==fa)
			continue;
		if (!pre[v])
		{
			child++;              //未访问过的结点才能算是u的孩子
			int lowv = dfs(v,u);
			lowu = min(lowu,lowv);
			/*if (lowv >=pre[u])
			{
				iscut[u]=1;           //u是割点
				cut[u]++;
				if (lowv > pre[u])       //(u,v)边时桥
			//		printf("qiao")
			}*/
		}
		else/* if (pre[v] <pre[u] && v!=fa) */ //v!=fa确保了(u,v)是从u到v的反向边
		{
			lowu = min(lowu,pre[v]);
		}
	}
	return low[u]=lowu;
}

struct Edge
{
    string u;
	string v;
	bool flag;
	
}e[maxm];
void init()
{
	dfs_clock = 0;
	sum=0;
	memset(pre,0,sizeof(pre));
//	memset(iscut,0,sizeof(iscut));
//	memset(cut,0,sizeof(cut));
	for (int i = 0;i<=n;i++)
		G[i].clear();
	
}
map<string,int> mapp;

int main()
{
	//freopen("in","r",stdin);
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		init();
        mapp.clear();
		int id = 0;
		for (int i = 0;i<m;i++)
		{
			cin >> e[i].u >> e[i].v;
			e[i].flag=0;
            if (!mapp.count(e[i].u))
			{
				mapp[e[i].u]=id++;
			}
			if (!mapp.count(e[i].v))
			{
				mapp[e[i].v]=id++;
			}
            int uu = mapp[e[i].u];
			int vv = mapp[e[i].v];
			G[uu].push_back(vv);
			G[vv].push_back(uu);
		}
		dfs(0,-1);
		int flag = 1;
		for (int i = 0;i<n;i++)
			if (!pre[i])
			{
				flag = 0;
				break;
			}
		if (!flag)
			puts("0");
		else
		{
			int ans = 0;
			for (int i = 0;i<m;i++)
			{
				int uu = mapp[e[i].u];
				int vv = mapp[e[i].v];
				if (low[uu]>pre[vv] || low[vv]>pre[uu])
				{
					e[i].flag=1;
					ans++;
				}
			}
			printf("%d\n",ans);
			for (int i = 0;i<m;i++)
				if (e[i].flag)
					cout << e[i].u << " " << e[i].v << endl;
		}
	}
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


/*  300MS  */
#include<cstdio>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
const int maxn=10000+10;
const int maxm=100000+10;
int n,m;
struct node
{
    char s[20];
    bool operator <(const node& rhs)const
    {
        return strcmp(s,rhs.s)<0;
    }
};
map<node,int> mp;//将字符串node映射成节点编号
struct Edge
{
    node u,v;
    bool flag;//标记该边是不是桥
}e[maxm];
vector<int> G[maxn];
int pre[maxn],low[maxn];
int dfs_clock;
void tarjan(int u,int fa)
{
    low[u]=pre[u]=++dfs_clock;
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(v==fa) continue;
        if(!pre[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
        }
        else low[u] = min(low[u],pre[v]);
    }
}
int main()
{
    int T; scanf("%d",&T);
    while(T--)
    {
        int id=0;
        scanf("%d%d",&n,&m);
        mp.clear();
        dfs_clock=0;
        memset(pre,0,sizeof(pre));
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=0;i<m;i++)
        {
            e[i].flag=false;
            scanf("%s%s",e[i].u.s,e[i].v.s);
            if(mp.find(e[i].u)==mp.end()) mp[e[i].u]= ++id;
            if(mp.find(e[i].v)==mp.end()) mp[e[i].v]= ++id;
            int x = mp[e[i].u], y=mp[e[i].v];
            G[x].push_back(y);
            G[y].push_back(x);
        }
        tarjan(1,-1);
        bool ok=true;//判断是否连通图
        for(int i=1;i<=n;i++)if(!pre[i])
        {
            ok=false;
            break;
        }
        if(!ok) printf("0\n");
        else
        {
            int ans=0;//计数桥总数
            for(int i=0;i<m;i++)
            {
                int u=mp[e[i].u], v=mp[e[i].v];
                if(low[u]>pre[v]||low[v]>pre[u]) e[i].flag=true,ans++;
            }
            printf("%d\n",ans);
            for(int i=0;i<m;i++)if(e[i].flag)
                printf("%s %s\n",e[i].u.s,e[i].v.s);
        }
    }
    return 0;
}

Description

Social Network is popular these days.The Network helps us know about those guys who we are following intensely and makes us keep up our pace with the trend of modern times. 
But how? 
By what method can we know the infomation we wanna?In some websites,maybe Renren,based on social network,we mostly get the infomation by some relations with those "popular leaders".It seems that they know every lately news and are always online.They are alway publishing breaking news and by our relations with them we are informed of "almost everything". 
(Aha,"almost everything",what an impulsive society!) 
Now,it's time to know what our problem is.We want to know which are the key relations make us related with other ones in the social network. 
Well,what is the so-called key relation? 
It means if the relation is cancelled or does not exist anymore,we will permanently lose the relations with some guys in the social network.Apparently,we don't wanna lose relations with those guys.We must know which are these key relations so that we can maintain these relations better. 
We will give you a relation description map and you should find the key relations in it. 
We all know that the relation bewteen two guys is mutual,because this relation description map doesn't describe the relations in twitter or google+.For example,in the situation of this problem,if I know you,you know me,too. 
 

Input

The input is a relation description map. 
In the first line,an integer t,represents the number of cases(t <= 5). 
In the second line,an integer n,represents the number of guys(1 <= n <= 10000) and an integer m,represents the number of relations between those guys(0 <= m <= 100000). 
From the second to the (m + 1)the line,in each line,there are two strings A and B(1 <= length[a],length[b] <= 15,assuming that only lowercase letters exist). 
We guanrantee that in the relation description map,no one has relations with himself(herself),and there won't be identical relations(namely,if "aaa bbb" has already exists in one line,in the following lines,there won't be any more "aaa bbb" or "bbb aaa"). 
We won't guarantee that all these guys have relations with each other(no matter directly or indirectly),so of course,maybe there are no key relations in the relation description map. 
 

Output

In the first line,output an integer n,represents the number of key relations in the relation description map. 
From the second line to the (n + 1)th line,output these key relations according to the order and format of the input. 
 

Sample Input

1 4 4 saerdna aswmtjdsj aswmtjdsj mabodx mabodx biribiri aswmtjdsj biribiri
 

Sample Output

1 saerdna aswmtjdsj
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值