码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3849(桥)

时间:2015-09-25 20:21:20      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:

By Recognizing These Guys, We Find Social Networks Useful



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
 
  1 #include<cstdio>
  2 #include<iostream>
  3 #include<vector>
  4 #include<cstring>
  5 #include<string>
  6 #include<stack>
  7 #include<algorithm>
  8 #include<map>
  9 using namespace std;
 10 
 11 struct Edge
 12 {
 13     int u,v;
 14 };
 15 
 16 const int N=10005;
 17 int dfn[N],low[N];
 18 int dfs_clock,m,n;
 19 map<string,int>ver;
 20 vector<int>G[N];
 21 vector<Edge>bridge;
 22 vector<Edge>edges;
 23 string guy[N];
 24 
 25 bool cmp(Edge a,Edge b)
 26 {
 27     if(a.u==b.u)
 28         return a.v<b.v;
 29     return a.u<b.u;
 30 }
 31 
 32 void init()
 33 {
 34     memset(low,0,sizeof(low));
 35     memset(dfn,0,sizeof(dfn));
 36     dfs_clock=0;
 37     edges.clear();
 38     bridge.clear();
 39     ver.clear();
 40     for(int i=0;i<n;i++)
 41         G[i].clear();
 42 }
 43 
 44 int tarjan(int u,int fa)
 45 {
 46     int lowu=dfn[u]=++dfs_clock;
 47     for(int i=0;i<G[u].size();i++)
 48     {
 49         int v=G[u][i];
 50         if(!dfn[v])
 51         {
 52             int lowv=tarjan(v,u);
 53             lowu=min(lowu,lowv);
 54         }
 55         else if(dfn[v]<dfn[u]&&v!=fa)
 56             lowu=min(dfn[v],lowu);
 57     }
 58     low[u]=lowu;
 59     return lowu;
 60 }
 61 
 62 int main()
 63 {
 64     int T;
 65     scanf("%d",&T);
 66     while(T--)
 67     {
 68         init();
 69         scanf("%d%d",&n,&m);
 70         int num=0;
 71         for(int i=0;i<m;i++)
 72         {
 73             string tmp1,tmp2;
 74             int u,v;
 75             cin>>tmp1>>tmp2;
 76             if(!ver.count(tmp1))
 77             {
 78                 ver[tmp1]=num;
 79                 guy[num]=tmp1;
 80                 num++;
 81             }
 82             if(!ver.count(tmp2))
 83             {
 84                 ver[tmp2]=num;
 85                 guy[num]=tmp2;
 86                 num++;
 87             }
 88             u=ver[tmp1],v=ver[tmp2];
 89             edges.push_back((Edge){u,v});
 90             G[u].push_back(v);
 91             G[v].push_back(u);
 92         }
 93         tarjan(0,-1);
 94         int j=0;
 95         for(j=0;j<n;j++)
 96             if(!dfn[j])break;
 97         if(j!=n)
 98             printf("0\n");
 99         else
100         {
101             vector<Edge>::iterator it;
102             for(it=edges.begin();it!=edges.end();it++)
103             {
104                 if(low[it->u]>dfn[it->v]||low[it->v]>dfn[it->u])
105                     bridge.push_back(*it);
106             }
107             printf("%d\n",bridge.size());
108             sort(bridge.begin(),bridge.end(),cmp);
109             vector<Edge>::iterator iter;
110             for(iter=bridge.begin();iter!=bridge.end();iter++)
111                 cout<<guy[iter->u]<<" "<<guy[iter->v]<<endl;
112         }
113     }
114     return 0;
115 }

 

HDU 3849(桥)

标签:

原文地址:http://www.cnblogs.com/homura/p/4839110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!