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

原创 2015年11月17日 17:00:03

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




版权声明:⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄hiahiahia 欢迎斧正 举报

相关文章推荐

HDU 3849 By Recognizing These Guys, We Find Social Networks Useful 求桥及其节点

点击打开链接 By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/...

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

题意:给你一个无向图(可能不连通,但是无自环,无重边),如果本图不连通,那么直接输出0。否则要你输出图中的每条桥边,要求按输入边的顺序输出。 思路:由于要按输入边的顺序输出,所以只能等DFS之后再输...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

hdu3849 By Recognizing These Guys, We Find Social Networks Useful

无向图求桥边数量,按照题目输入顺序输出桥边。 注意存的brig和边的对应关系。 #include #include #include #include #include #inc...

【HDU】3849 By Recognizing These Guys, We Find Social Networks Useful 双连通求桥

By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others)   ...

hdu 3849 By Recognizing These Guys, We Find Social Networks Useful(双连通求桥)

By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others)   ...

HDU 3849 By Recognizing These Guys, We Find Social Networks Useful

http://acm.hdu.edu.cn/showproblem.php?pid=3849#include using namespace std; #include #include #inclu...
  • ME4546
  • ME4546
  • 2011-07-18 18:22
  • 1087

BUPT 网络预选赛邀请赛 B题 B By Recognizing These Guys, We Find Social Networks Useful

题目连接:http://acm.bupt.edu.cn/onlinejudge/newoj/showProblem/show_problem.php?problem_id=197 B By Recog...

HDU - 4635 Strongly connected(求树的直径)(边双连通分量-有重边)

HDU - 4635 Strongly connected 题目大意: 给你一个无向的连通图,存在重边,然后问你加一条之后,最少的桥的边数。 先求出桥,然后再对重边特判后重新缩点构图,最后获得...

hdu4738 Caocao's Bridges(双连通分量割边/桥)

Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he...

hdu3394——Railway——————【判断桥和双连通分量】

/**    解题思路:                   本题中的冲突边是指形成环的边且边数大于点数,...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)