uva796(求桥数目)

传送门:Critical Links

题意:给出一个无向图,按顺序输出桥。

分析:模板题,求出桥后排个序输出。

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 100010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
struct edge
{
    int v,next;
    edge(){}
    edge(int v,int next):v(v),next(next){}
}e[N<<1];
struct bridge
{
    int u,v;
    bridge(){}
    bridge(int u,int v):u(u),v(v){}
    bool operator<(const bridge &a)const{
        if(u==a.u)return v<a.v;
        return u<a.u;
    }
}b[N<<1];
int n,step,top,tot,num;
int head[N],dfn[N],low[N],Stack[N];
bool instack[N];
map<int,int>mp;
void init()
{
    tot=0;step=0;top=0;num=0;
    FILL(head,-1);FILL(dfn,0);
    FILL(low,0);FILL(instack,false);
    mp.clear();
}
void addedge(int u,int v)
{
    e[tot]=edge(v,head[u]);
    head[u]=tot++;
}
bool isHash(int u,int v)
{
    if(mp[u*N+v])return 1;
    if(mp[v*N+u])return 1;
    mp[u*N+v]=mp[v*N+u]=1;
    return 0;
}
void tarjan(int u,int fa)
{
    dfn[u]=low[u]=++step;
    Stack[top++]=u;
    instack[u]=true;
    for(int i=head[u];~i;i=e[i].next)
    {
        int v=e[i].v;
        if(v==fa)continue;
        if(!dfn[v])
        {
            tarjan(v,u);
            if(low[u]>low[v])low[u]=low[v];
            //桥:一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS[u]<Low[v]
            if(low[v]>dfn[u])
            {
                b[num++]=bridge(min(u,v),max(u,v));
            }
        }
        else if(low[u]>dfn[v])
        {
            low[u]=dfn[v];
        }
    }
    instack[u]=false;
    top--;
}
void solve()
{
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i,i);
    sort(b,b+num);
    printf("%d critical links\n",num);
    for(int i=0;i<num;i++)
        printf("%d - %d\n",b[i].u,b[i].v);
    puts("");
}
int main()
{
    int u,v;
    while(scanf("%d",&n)>0)
    {
        init();
        for(int i=1;i<=n;i++)
        {
            int t,u,v;
            scanf("%d (%d)",&u,&t);
            while(t--)
            {
                scanf("%d",&v);
                if(!isHash(u,v))
                {
                    addedge(u,v);
                    addedge(v,u);
                }
            }
        }
        solve();
    }
}

时间: 2024-08-25 00:16:43

uva796(求桥数目)的相关文章

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re

UVA 796 - Critical Links【求桥】

link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 题意: 求桥的数目及边,要求输出边的点的次序由小到大 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include

UVA 315【求割点数目】

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 题意:求割点数目 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #includ

POJ 3694——Network——————【连通图,LCA求桥】

Network Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3694 Description A network administrator manages a large network. The network consists of N computers and M links between pairs of compute

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

Tarjan求桥和割点

//Tarjan 求桥和割点 Tarjan(u,fa) { DFN[u]=LoW[u]=++time; Cu=grey; for each e=(u,v) { Tarjan(v,u); if(Cv=white) { low[u]=min(low[u],low[v]); }else { low[u]=min(low[u],DFN[v]); } } }

tarjan求桥、割顶

若low[v]>dfn[u],则(u,v)为割边.但是实际处理时我们并不这样判断,因为有的图上可能有重边,这样不好处理.我们记录每条边的标号(一条无向边拆成的两条有向边标号相同),记录每个点的父亲到它的边的标号,如果边(u,v)是v的父亲边,就不能用dfn[u]更新low[v].这样如果遍历完v的所有子节点后,发现low[v]=dfn[v],说明u的父亲边(u,v)为割边. void tarjan(int x) { vis[x]=1; dfn[x]=low[x]=++num; for(int i

hdoj 4738 Caocao&#39;s Bridges【双连通分量求桥】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3000    Accepted Submission(s): 953 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. Bu