HDU 3849(桥)

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 }
时间: 2024-11-06 12:48:20

HDU 3849(桥)的相关文章

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

HDU 3849 By Recognizing These Guys, We Find Social Networks Useful 题目链接 题意:说白了就是求一个无向图的桥 思路:字符串hash掉,然后双连通.要注意特判一下假设不是一个连通块.那么答案是0 代码: #include <cstdio> #include <cstring> #include <string> #include <vector> #include <map> us

HDU 3849 无向图求桥

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): 2563    Accepted Submission(s): 671 Problem Description Social Network is popular these

HDU 3849 无向图的割边

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 Description Social Network is popular these

无向图求割顶与桥

无向图求割顶与桥 对于无向图G,如果删除某个点u后,连通分量数目增加,称u为图的关节点或割顶.对于连通图,割顶就是删除之后使图不再连通的点.如果删除边(u,v)一条边,就可以让连通图变成不连通的,那么边(u,v)是桥. 具体的概念和定义比较多,在刘汝佳<<训练指南>>P312-314页都有详细的介绍. 下面来写求无向图割顶和桥的DFS函数.我们令pre[i]表示第一次访问i点的时间戳,令low[i]表示i节点及其后代所能连回(通过反向边)的最早祖先的pre值. 下面的dfs函数返回

HDU 4738 无向图求桥

使用tarjan算法求桥,模板题,但是... 1.有重边 2.不一定连通 3.没有人守桥至少要派一个人去 http://acm.hdu.edu.cn/showproblem.php?pid=4738 这种题挺好的,可以锻炼人的耐性和心理承受能力... #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <vector> us

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and

Hdu 4738 Caocao&#39;s Bridges (连通图+桥)

题目链接: Hdu 4738 Caocao's Bridges 题目描述: 有n个岛屿,m个桥,问是否可以去掉一个花费最小的桥,使得岛屿边的不连通? 解题思路: 去掉一个边使得岛屿不连通,那么去掉的这个边一定是一个桥,所以我们只需要求出来所有的桥,然后比较每个桥的花费,选取最小的那个就好. 看起来很简单的样子哦!但是这个题目有很多的细节: A:题目中有重边,以后写Tarjan还是清一色判断重边吧.(除非题目特别要求) B:m个桥有可能连通不了这n个桥,这个时候不需要花费. C:当最小花费桥的花费