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
2011 Invitational Contest Host by BUPT 

又是一个卡了好几天的题,一看到字符串与序号的相互转化就想到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



==========================================================================================================================

刚刚才查到两个其中的成绩,java和地信的,地信倒是还行,给了80挺满足的,java啊,82啊……完完全全就是整天嘚瑟的恶果T^T,还总觉得自己敲了3w行代码多么多么了不起,又提前自学了,可以作为不听课的资本,还是上次的数据结构不够痛啊,早就应该想到会有这个结果,还想13周的两科差不多复习复习就行了呢,也算是给最近懈怠的不行不行的自己一个警钟吧,这么看来,仅仅是期中就扣了总成绩的5分也未必是坏事(刚想扯自己开发比理论好,还是歇菜吧,估计那些分都扣最后一个题上了吧,不然会是判断选择错了7个?),嗯,反正对自己来说,编程的课能考这点分也算是特别悲剧的一件事了,还整天yangdaoerzheng的呢(是哪四个字我不知道)上周知道90分以上居然有6个、最高98,已经觉得够伤心的了,刚刚发现自己都不在这六个更是伤心欲绝了大哭从今往后要好好做人了委屈再一再二不能再三再四了……


你可能感兴趣的:(HDU,Tarjan,双连通分量求桥)