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

来源:互联网 发布:linux下touch命令 编辑:程序博客网 时间:2024/05/17 09:24

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


题意:求无向图的桥

solution:tarjan

#include <cstdio>#include <iostream>#include <cstring>#include <map>#include <algorithm>using namespace std;const int N = 1e4 + 7;const int M = 1e5 + 7; struct Edge{    int u, v, nxt;}e[M<<1];int head[N], tot=0;void addeage(int u, int v){    e[tot].u=u, e[tot].v=v; e[tot].nxt=head[u];    head[u]=tot++;}int dfn[M], low[M], id=0;int brg[M],ans=0;void dfs(int u, int fa){    dfn[u]=low[u]=++id;    for ( int i=head[u]; i!=-1; i=e[i].nxt ){        int v=e[i].v;        if( v==fa ) continue;        if( !dfn[v] ){            dfs(v, u);            if( dfn[u]<low[v] ) brg[++ans]=i;            low[u]=min(low[u], low[v]);        } else {            low[u]=min(low[u], dfn[v]);        }    }}map<string, int> mp1;map<int, string> mp2;int dnt=0; int HASH(string str){    if( mp1.count(str) ) return mp1[str];    mp1[str]=++dnt;    mp2[dnt]=str;    return dnt; }int cmp(int a, int b){    return a<b;}int main(){    int T, p;    scanf("%d", &T );    while( T-- ){        int n, m;        tot=id=dnt=0;        mp1.clear(), mp2.clear();        memset(head,-1,sizeof(head));        memset(dfn,0,sizeof(dfn));        memset(low,0,sizeof(low));         scanf("%d%d", &n, &m );        for ( int i=1; i<=m; i++ ){            string a, b;            //a.resize(20);        //  b.resize(20);            cin>>a>>b;            int t1=HASH(a), t2=HASH(b);            addeage(t1,t2), addeage(t2,t1);         }        ans=p=0;        dfs(1,0);        for ( int i=1; i<=n; i++ ) if( !dfn[i] ) { p=-1; break; }        if( p==-1 ){            puts("0");            continue;        }        printf("%d\n", ans );        sort(brg+1,brg+1+ans,cmp);        for ( int i=1; i<=ans; i++ ){            int j=brg[i];            if( j&1 ) j--;            cout<<mp2[e[j].u]<<" "<<mp2[e[j].v]<<"\n";          }    }    return 0;   }
阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 oppor手机忘记解锁图案怎么办 oppor手机忘记图案了怎么办 oppo手机屏幕锁忘了怎么办 三星手机屏幕锁忘了怎么办 手机主菜单坏了怎么办 索尼手机密码忘了怎么办 索尼笔记本密码忘了怎么办 索尼手机解锁密码忘了怎么办 索尼记录仪密码忘了怎么办 索尼z3手机忘记开机密码怎么办 索尼忘记锁屏密码怎么办 索尼手机忘记锁屏密码怎么办 忘了手机解锁图案怎么办 索尼手机忘记开机密码怎么办 索尼笔记本开机密码忘了怎么办 手机的开机密码忘了怎么办 联想手机开机密码忘了怎么办 红米note3忘记开机密码怎么办 小米2忘了密码怎么办 小米笔记本电脑忘记开机密码怎么办 小米笔记本忘记开机密码怎么办 小米手机儿童模式忘记密码怎么办 小米应用锁密码忘了怎么办 小米air密码忘了怎么办 小米4密码忘了怎么办 小米手机开机密码忘了怎么办? 小米笔记本电脑开机密码忘了怎么办 小米笔记本开机密码忘了怎么办 htc手机忘记解锁图案怎么办 红米手机屏幕锁定怎么解锁怎么办 小米5s有id怎么办 手机密码找不回来了怎么办? 手机密码图案忘了怎么办 手机屏幕图案锁忘了怎么办 捡到苹果7有id锁怎么办 魅族什么都忘了怎么办 海信电视百事通登陆失败怎么办 去哪儿换号了怎么办 ipan充不进去电怎么办 安卓数据线松了怎么办 索尼z5耳机掉漆怎么办