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): 2319 accepted submission (s): 603
Problem descriptionsocial 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 canceled 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 shoshould 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.
Inputthe 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.
Outputin 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 input14 4 saerdna aswmtjdsjaswmtjdsj mabodxmabodx biribiriaswmtjdsj biribiri
Sample output1saerdna aswmtjdsj question: give you n, m represents N points of a graph, M edge. Below M behavior X, Y indicates that there is an edge between x and y, and the cut edge is obtained. Two pitfall points: If the graph is not connected, 0 is output; if multiple cut edges are output in input order, and the order of X and Y is the same as that of input (pitfall ...) Idea: I won't say much about edge Cutting. It is available on the Internet. Complaining... In my last submit, a single version is only AC .. I didn't seem to have said that the output is 0... It indicates that it is speechless .. Code:
1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <iostream> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 using namespace std; 11 #define N 10005 12 13 struct node{ 14 int x, y, id, id1, id2; 15 }ans[N]; 16 17 struct mem{ 18 int y, id, id1, id2; 19 }; 20 21 vector<mem>ve[N]; 22 map<string,int>ma; 23 map<int,string>mb; 24 int low[N], dfn[N]; 25 int visited[N]; 26 int n, m, dfn_clock; 27 int nu; 28 29 void init(){ 30 for(int i=0;i<=n;i++) ve[i].clear(); 31 ma.clear();mb.clear(); 32 memset(dfn,-1,sizeof(dfn)); 33 memset(visited,0,sizeof(visited)); 34 } 35 36 void dfs(int u,int fa){ 37 int i, j, k, v; 38 node p; 39 mem q; 40 //printf("1111111111\n"); 41 low[u]=dfn[u]=dfn_clock++; 42 43 visited[u]=1; 44 for(i=0;i<ve[u].size();i++){ 45 q=ve[u][i]; 46 if(q.y==fa) continue; 47 // printf("%d\n",v); 48 if(!visited[q.y]){ 49 dfs(q.y,u); 50 low[u]=min(low[u],low[q.y]); 51 if(low[q.y]>dfn[u]) { 52 p.x=u;p.y=q.y;p.id=q.id;p.id1=q.id1;p.id2=q.id2; 53 ans[nu++]=p; 54 } 55 } 56 else low[u]=min(low[u],dfn[q.y]); 57 58 } 59 } 60 61 bool cmp(node a,node b){ 62 return a.id<b.id; 63 } 64 65 main() 66 { 67 int t, i, j, k, id; 68 char s1[200], s2[200]; 69 mem p; 70 cin>>t; 71 while(t--){ 72 scanf("%d %d",&n,&m); 73 init();k=1;id=1; 74 while(m--){ 75 scanf("%s%s",s1,s2); 76 if(ma[s1]==0) ma[s1]=k,mb[k]=s1,++k; 77 if(ma[s2]==0) ma[s2]=k,mb[k]=s2,++k; 78 int x=ma[s1], y=ma[s2]; 79 p.y=ma[s2];p.id=id++;p.id1=1;p.id2=2; 80 ve[x].push_back(p); 81 p.y=ma[s1];p.id=id++;p.id1=2;p.id2=1; 82 ve[y].push_back(p); 83 } 84 85 dfn_clock=nu=0; 86 dfs(1,-1); 87 int f=1; 88 for(i=1;i<=n;i++) { 89 if(dfn[i]==-1){ 90 break; 91 } 92 } 93 if(i<=n){ 94 printf("0\n");continue; 95 } 96 sort(ans,ans+nu,cmp); 97 printf("%d\n",nu); 98 for(i=0;i<nu;i++){ 99 if(ans[i].id1<ans[i].id2)100 cout<<mb[ans[i].x]<<" "<<mb[ans[i].y]<<endl;101 else cout<<mb[ans[i].y]<<" "<<mb[ans[i].x]<<endl;102 }103 }104 }
Cutting edge of HDU 3849 undirected graph