hdu3849By Recognizing These Guys, We Find Social Networks Useful【map+双连通分量求桥+扩栈代码】

Problem 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
 

Source

又是一个卡了好几天的题,一看到字符串与序号的相互转化就想到trie树……用map多好啊 ,回想了好久插入查找,结果发现结果输出也得要字符串形式的

剩下的就是求桥,没啥好说的


/**********
hdu3849
2015.11.15-2015.11.17
811MS 20368K 2317 B
**********/
#include<iostream>

#include<algorithm>
#include<string>
#include<cstdio>
#include<cstring>
#include<map>
#define N 10005
#define M_M 200005
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
int top,bcnt;
int stack[N],indx;
int dfn[N],low[N],cn;
map<string,int> M; //存地点对应的编号
map<int,string> MM; //存编号对应的地点
map<string,int> final; //存每条边的编号,很有用的!
struct node{
	int next,v;
	node(){};
	node(int a,int b){
		next=a,v=b;
	}
}E[M_M];
struct ans{
	string s;
	int ind;
}ret[M_M]; //存最后结果,ind拿来排序的时候用
int head[N],NE;
int n,m;
void init(){
	M.clear();
	MM.clear();
	final.clear();
	NE=0;bcnt=0;top=0;indx=0;cn=0;
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
}
void insert(int u,int v){
	E[NE]=node(head[u],v);
	head[u]=NE++;
}

void tarjan(int u,int pre){ //----------------------------------1
	dfn[u]=low[u]=++indx;
	stack[top++]=u;
	for(int i=head[u];i!=-1;i=E[i].next){
		int v=E[i].v;
		if(v==pre) continue;
		if(!dfn[v]){
			tarjan(v,u);
			if(low[v]<low[u])
				low[u]=low[v];
			if(low[v]>dfn[u]){               //满足割边要求
				ret[cn].s=MM[u]+' '+MM[v];
				if(!final[ret[cn].s]) //-----------------------2
					ret[cn].s=MM[v]+' '+MM[u];
				ret[cn].ind=final[ret[cn].s];
				cn++;
			}
		}
		else if(dfn[v]<low[u])
			low[u]=dfn[v];
	}
}
bool cmp(ans x,ans y){
	return x.ind<y.ind;
}
int bin[N];
int find(int x){
	if(bin[x]==x)
		return bin[x];
	return bin[x]=find(bin[x]);
}
bool merge(int x,int y){
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy){
		bin[fx]=fy;
		return true;
	}
	return false;
}
int main(void){
	//freopen("cin.txt","r",stdin);
	int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		init();
		int ind=1;
		for(int i=0;i<=n;i++)
			bin[i]=i;
		for(int i=1;i<=m;i++){
			string u,v;
			cin>>u>>v;
			if(!M[u]){
				M[u]=ind++;
				MM[ind-1]=u;
			}
			if(!M[v]){
				M[v]=ind++;
				MM[ind-1]=v;
			}
			final[u+' '+v]=i;
			int k1=M[u],k2=M[v];
			insert(k1,k2);
			insert(k2,k1);
			if(merge(k1,k2))
				bcnt++;
		}
		if(bcnt!=n-1){
			printf("0\n");
			continue;
		}
		tarjan(1,-1);
		sort(ret,ret+cn,cmp);
		printf("%d\n",cn);
		for(int i=0;i<cn;i++)
			cout<<ret[i].s<<endl;
	}
}
扩栈代码原理:http://www.cnblogs.com/lixinkun/archive/2012/04/01/2428343.html




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值