hdu3849 By Recognizing These Guys, We Find Social Networks Useful

无向图求桥边数量,按照题目输入顺序输出桥边。

注意存的brig和边的对应关系。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:16777216")
#define eps 1e-6
#define ll long long
using namespace std;
#define M 200003
#define N 10003

int head[N],num,dfn[N],low[N],n,m,idx,brig[M<<1],bum;
struct edge
{
    int st,ed,next;
}E[M<<1];

void addedge(int x,int y)
{
    E[num].st=x;
    E[num].ed=y;
    E[num].next=head[x];
    head[x]=num++;
}

void tarjan(int u,int father)
{
    int i,v;
    low[u]=dfn[u]=idx++;
    for(i=head[u];i!=-1;i=E[i].next)
    {
        v=E[i].ed;
        if(v==father)continue;
        if(dfn[v]==-1)
        {
            tarjan(v,u);
            low[u]=low[u]>low[v]?low[v]:low[u];
            if(low[v]>dfn[u])//存第bum个桥对应边的编号
                brig[bum++]=i;
        }
        else low[u]=low[u]>dfn[v]?dfn[v]:low[u];
    }
}

void init()
{
    memset(dfn,-1,sizeof dfn);
    memset(head,-1,sizeof head);
    num=bum=idx=0;
}

string hash[N];
string s1,s2;

bool cmp(int a,int b)
{
    return a<b;
}

int main()
{
    int icy,i,cnt;
    scanf("%d",&icy);
    while(icy--)
    {
        scanf("%d%d",&n,&m);
        init();
        map<string,int> mp;
        cnt=1;
        for(i=0;i<m;i++)
        {
            cin>>s1>>s2;
            if(!mp[s1])
                hash[cnt]=s1,mp[s1]=cnt++;
            if(!mp[s2])
                hash[cnt]=s2,mp[s2]=cnt++;
            addedge(mp[s1],mp[s2]);
            addedge(mp[s2],mp[s1]);
        }
        tarjan(1,-1);
        for(i=1;i<=n;i++)//判断图不联通
           if(dfn[i]==-1) break;
        if(i<=n)
        {
            printf("0\n");
            continue;
        }
        printf("%d\n",bum);
        sort(brig,brig+bum,cmp);
        for(int j=0;j<bum;j++)
        {
            i=brig[j];
            if(i&1) i--;
            cout<<hash[E[i].st]<<' '<<hash[E[i].ed]<<endl;
        }
    }
    return 0;
}

hdu3849 By Recognizing These Guys, We Find Social Networks Useful

时间: 2024-10-11 16:14:46

hdu3849 By Recognizing These Guys, We Find Social Networks Useful的相关文章

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

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

Recommendations in LBSN Social Networks(Notes)

Recommendations in LBSN Social Networks Section 2 Concepts of LBSN Social Networks: new social structure made up of individuals connected by the interdependency derived from their locations in the physical world as well as location-tagged media conte

Social networks and health: Communicable but not infectious

Harvard Men’s Health Watch Poet and pastor John Donne famously proclaimed “No man is an island.” It was true in his day, and because society has become increasingly complex and interdependent over the ensuing 400 years, it’s certainly true today. Stu

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

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.

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?