关闭

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

标签: tarjan双连通分量求桥hdu
659人阅读 评论(0) 收藏 举报
分类:

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
查看评论

hdu3849By Recognizing These Guys, We Find Social Networks Useful

By Recognizing These Guys, We Find Social Networks Useful Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K ...
  • imzxww
  • imzxww
  • 2018-01-29 19:21
  • 12

HDU - 3849 By Recognizing These Guys, We Find Social Networks Useful(桥)

题目大意:给你N个点,M条边,问哪几条边是桥解题思路:模版题#include <cstdio> #include <cstring> #include <map> #include <vector> #include <iostream> ...
  • L123012013048
  • L123012013048
  • 2015-09-08 23:42
  • 252

HDU 3849 Recognizing These Guys, We Find Social Networks Useful

Recognizing These Guys, We Find Social Networks UsefulDescriptionSocial Network is popular these days.The Network helps us know about those guys who w...
  • ywq12138
  • ywq12138
  • 2017-10-12 21:17
  • 138

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

HDU 3849 By Recognizing These Guys, We Find Social Networks Useful 题目链接 题意:说白了就是求一个无向图的桥 思路:字符串hash掉,然后双连通,要注意特判一下如果不是一个连通块,那么答案是0 代码: ...
  • u011217342
  • u011217342
  • 2014-10-23 14:22
  • 624

hdu3849 By Recognizing These Guys, We Find Social Networks Useful

无向图求桥边数量,按照题目输入顺序输出桥边。 注意存的brig和边的对应关系。 #include #include #include #include #include #include #include #include #include #include #def...
  • u011032846
  • u011032846
  • 2014-08-18 16:00
  • 874

Hdu 3859 By Recognizing These Guys, We Find Social Networks Useful

题目链接;http://acm.hdu.edu.cn/showproblem.php?pid=3849 无向图求割边(Tarjan): #include #include #include #include #include #include #include #include ...
  • niuox
  • niuox
  • 2013-08-20 21:35
  • 762

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 #include #include #include #define N 10005 #de
  • ME4546
  • ME4546
  • 2011-07-18 18:22
  • 1144

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

模型就是在一个双连通分量重找桥。 注意几点: 1.重边的处理 2.图不一定连同
  • u011281853
  • u011281853
  • 2014-06-29 19:22
  • 404

【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)    Memory Limit: 125536/65536 K ...
  • u013368721
  • u013368721
  • 2014-07-15 10:59
  • 884

HDU3849[By Recognizing These Guys, We Find Social Networks Useful ] tarjan求无向图的桥

HDU3849 tarjan 无向图的桥
  • qq_37321281
  • qq_37321281
  • 2017-08-24 15:43
  • 66
    碎碎念
    周小姐,你还想毕业找不到工作吗????
    个人资料
    • 访问:279662次
    • 积分:8805
    • 等级:
    • 排名:第2603名
    • 原创:608篇
    • 转载:25篇
    • 译文:0篇
    • 评论:38条
    友情链接