【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 (Java/Others)
Total Submission(s): 2036    Accepted Submission(s): 536


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

传送门:【HDU】3849 By Recognizing These Guys, We Find Social Networks Useful

题目分析:求出双连通分量后,标记桥,然后输出,注意如果图不连通输出0.(然后因为这题学了一下map的基本使用)

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
using namespace std ;

#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REPV( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define Clear( a , x ) memset ( a , x , sizeof a )
#define fi first
#define se second

typedef long long LL ;

const int MAXN = 10005 ;
const int MAXE = 100005 ;
const int MAXM = 20 ;

struct Edge {
	int v , n ;
	Edge ( int var = 0 , int next = 0 ) :
		v ( var ) , n ( next ) {}
} ;

struct Line {
	char s1[MAXM] , s2[MAXM] ;
	int flag ;
	void input () {
		scanf ( "%s%s" , s1 , s2 ) ;
		flag = 0 ;
	}
} ;

struct Member {
	char s[MAXM] ;
} ;

struct BCC {
	Edge edge[MAXE << 1] ;
	int adj[MAXN] , cntE ;
	int bcc[MAXN] , bcc_cnt ;
	int ins[MAXN] , S[MAXN] , top , dfs_clock ;
	int low[MAXN] , dfn[MAXN] ;
	
	void init () {
		top = cntE = dfs_clock = bcc_cnt = 0 ;
		Clear ( ins , 0 ) ;
		Clear ( dfn , 0 ) ;
		Clear ( adj , -1 ) ;
	}
	
	void addedge ( int u , int v ) {
		edge[cntE] = Edge ( v , adj[u] ) ;
		adj[u] = cntE ++ ;
		edge[cntE] = Edge ( u , adj[v] ) ;
		adj[v] = cntE ++ ;
	}
	
	void tarjan ( int u , int fa ) {
		low[u] = dfn[u] = ++ dfs_clock ;
		ins[u] = 1 ;
		S[top ++] = u ;
		int flag = 1 ;
		for ( int i = adj[u] ; ~i ; i = edge[i].n ) {
			int v = edge[i].v ;
			if ( v == fa && flag ) {
				flag = 0 ;
				continue ;
			}
			if ( !dfn[v] ) {
				tarjan ( v , u ) ;
				low[u] = min ( low[u] , low[v] ) ;
			}
			else if ( ins[v] )
				low[u] = min ( low[u] , dfn[v] ) ;
		}
		if ( low[u] == dfn[u] ) {
			++ bcc_cnt ;
			while ( 1 ) {
				int v = S[-- top] ;
				ins[v] = 0 ;
				bcc[v] = bcc_cnt ;
				if ( v == u )
					break ;
			}
		}
	}
	int find_bcc ( int n ) {
		tarjan ( 0 , -1 ) ;
		REP ( i , n )
			if ( !dfn[i] )
				return 0 ;
		return 1 ;
	}
} ;

BCC c ;
Line line[MAXE] , ans[MAXE] ;
int point , ans_cnt ;
map < string , int > mp1 ;

void init () {
	c.init () ;
	mp1.clear () ;
	point = 0 ;
	ans_cnt = 0 ;
}

int get ( char buf[] ) {
	map < string , int > :: iterator it = mp1.find ( buf ) ;
	if ( it != mp1.end () )
		return it -> se ;
	else {
		mp1.insert ( map < string , int > :: value_type ( buf , point ) ) ;
		return point ++ ;
	}
}

void work () {
	int n , m ;
	int u , v ;
	init () ;
	scanf ( "%d%d" , &n , &m ) ;
	REP ( i , m ) {
		line[i].input () ;
		u = get ( line[i].s1 ) ;
		v = get ( line[i].s2 ) ;
		c.addedge ( u , v ) ;
	}
	if ( !c.find_bcc ( n ) || c.bcc_cnt == 1 ) {
		printf ( "0\n" ) ;
		return ;
	}
	REP ( i , m ) {
		int u = mp1[line[i].s1] ;
		int v = mp1[line[i].s2] ;
		if ( c.bcc[u] != c.bcc[v] )
			++ ans_cnt , line[i].flag = 1 ;
	}
	printf ( "%d\n" , ans_cnt ) ;
	REP ( i , m )
		if ( line[i].flag )
			printf ( "%s %s\n" , line[i].s1 , line[i].s2 ) ;
}

int main () {
	int T ;
	scanf ( "%d" , &T ) ;
	while ( T -- )
		work () ;
	return 0 ;
}


你可能感兴趣的:(HDU)