HDU3849-By Recognizing These Guys, We Find Social Networks Useful(无向图映射map求桥)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdz20172133/article/details/86591613

Recognizing These Guys, We Find Social Networks Useful
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

4 4 
saerdna aswmtjdsj 
aswmtjdsj mabodx 
mabodx biribiri 
aswmtjdsj biribiri

Sample Output

saerdna aswmtjdsj

题意:

题意:给你一张无向图,判断桥边的数量,按照题目输入顺序输出桥边。

分析:

关键关系就是桥,把字符用map映射一下。

///tarjan算法求无向图(可有重边)的桥、边双连通分量并缩点
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int SIZE = 100010;
int head[SIZE], ver[SIZE * 2], Next[SIZE * 2];
///head[i]=x表示以i起点的数组下标,ver[x]表示以i起点的终点编号
///next[x]=y下一个表示以i起点的数组下标 ,ver[y]表示另一个以i起点的终点编号
int dfn[SIZE], low[SIZE];
///dfn表示时间戳
///low表示追溯值,c[x]表示结点x属于边连通分量的编号
int n, m, tot,num;   
vector<int>bridge; ///是否是桥

void add(int x, int y) {
	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}
void tarjan(int x, int in_edge) {
	dfn[x] = low[x] = ++num;
	for (int i = head[x]; i; i = Next[i]) {///遍历每一个结点
		int y = ver[i];
		if (!dfn[y]) {
			tarjan(y, i);
			low[x] = min(low[x], low[y]);
			if (low[y] > dfn[x]) ///找到桥
				{
					bridge.push_back(i);
				}
		}
		else if (i != (in_edge ^ 1)) ///利用异或性质解决重边
                                     ///防止x结点到父亲结点
			low[x] = min(low[x], dfn[y]);
	}
}
string hash1[SIZE];
map<string,int> mp;
int main() {
	int t;
	scanf("%d",&t);
	while(t--)
    {
	int n,m;
	scanf("%d%d",&n,&m);
	
	mp.clear();
	bridge.clear();
	tot = 1;
	num=0;
	memset(head,0,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	int cnt=1;  
	 
	for (int i = 1; i <= m; i++) {
		string x, y;
		cin>>x>>y;
		if(mp[x]==0)
		{
			hash1[cnt]=x;
		 	mp[x]=cnt++;
		}
		if(mp[y]==0)
		{
			hash1[cnt]=y;
			mp[y]=cnt++;
		}
		
		add(mp[x], mp[y]);
		add(mp[y], mp[x]);
	}
	 tarjan(1, 0);
	 int i=1;
	for (i = 1; i <= n; i++) ///判断图是否连通
		if (dfn[i]==0)
			break;
	if(i<=n)   
	{
	 printf("0\n");
	  continue;
	}
	
	int len=bridge.size();
	printf("%d\n",len);
	sort(bridge.begin(),bridge.end());
	for(int j=0;j<len;j++)
	{
		int i=bridge[j];
		if(i&1) i=i^1; //奇数的话,
		///ver[奇数]保存的为起点,ver[偶数]保存的为终点
		cout<<hash1[ver[i^1]]<<" "<<hash1[ver[i]]<<endl;
	}
    }
 
	return 0;
}
 
/*4
4 4
dff fff
dff ggg
fff ggg
ggg xxx*/

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/86591613
we