UVA 796 - Critical Links (求桥按序输出)

tanjar求图中的桥,然后排序输出。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rev(i,a,b) for(int i=(a);i>=(b);i--)
#define clr(a,x) memset(a,x,sizeof a)
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;

const int eps=0.00000001;
const int maxn=10005;
const int maxm=200006;

int first[maxn],low[maxn],dfn[maxn],fa[maxn],if_add[maxn];
int nex[maxm],u[maxm],v[maxm],w[maxm];
int belong[maxn],stck[maxn];
int n,m,ecnt,indx,bridge,block,top,ecntt;
bool ins[maxn],vcut[maxn],ecut[maxm];

void tarjan(int s,int pre)
{
	dfn[s]=low[s]=++indx;
	stck[top++]=s;
	ins[s]=1;
	int son=0;
	for(int e=first[s];~e;e=nex[e])
    {
        if(v[e]==pre)continue;
        if(!dfn[v[e]])
        {
            son++;
            tarjan(v[e],s);
            low[s]=min(low[s],low[v[e]]);
            if(low[v[e]]>dfn[s])
            {
                bridge++;
                ecut[e]=1;
                ecut[e^1]=1;
            }
            if(s!=pre&&low[v[e]]>=dfn[s])
            {
                vcut[s]=1;
                if_add[s]++;
            }
        }
        else if(ins[v[e]])low[s]=min(low[s],dfn[v[e]]);
    }
    if(s==pre&&son>1)vcut[s]=1,if_add[s]=son-1;
    if(low[s]==dfn[s])
    {
        int vv;
        block++;
        do
        {
            vv=stck[--top];
            ins[vv]=0;
            belong[vv]=block;
        }while(vv!=s);
    }
}
void add_(int a,int b,int c)
{
	u[ecnt]=a;
	v[ecnt]=b;
	w[ecnt]=c;
	ecut[ecnt]=0;
	nex[ecnt]=first[a];
	first[a]=ecnt++;
}
void solve()
{
    clr(dfn,0);
    clr(if_add,0);
    clr(vcut,0);
    indx=bridge=block=top=0;
    for(int i=1;i<=n;i++)
        if(!dfn[i])tarjan(i,i);
}

struct node
{
    int a,b;
}da[maxm];
bool cmp(const node &c,const node &d)
{
    if(c.a==d.a)return c.b<d.b;
    return c.a<d.a;
}
int main()
{
    int a,b,c,cas=1;
    while(~scanf("%d",&n))
    {
        clr(first,-1);ecnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%*c%*c%d%*c)",&a,&c);a++;
            while(c--)
            {
                scanf("%d",&b);b++;
                if(a!=b)add_(a,b,0),add_(b,a,0);
            }
        }
        solve();
        printf("%d critical links\n",bridge);
        int ans=0;
        for(int i=0;i<ecnt;i++)
            if(ecut[i]&&u[i]<v[i])da[ans].a=u[i]-1,da[ans++].b=v[i]-1;
        sort(da,da+ans,cmp);
        for(int i=0;i<ans;i++)
            printf("%d - %d\n",da[i].a,da[i].b);
        printf("\n");
    }
}
时间: 2025-01-02 17:41:08

UVA 796 - Critical Links (求桥按序输出)的相关文章

Light OJ 1026 Critical Links 求桥

题目 Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3]

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 796 Critical Links(无向图求桥)

题目来源: UVa Online Judgehttps://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求一个连通图中必不可少的路径: #include<stdio.h> #include<algorithm> #include<vector> #include<string.h> #define

UVA 796 Critical Links —— (求割边(桥))

和求割点类似,只要把>=改成>即可.这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可. 总感觉图论要很仔细地想啊- -一不小心就弄混了.. 另外从这题发现,代码还是写成模块化比较好,比如solve一个函数,init一个函数等等,这样可以避免很多东西忘记写,比方说dfn或者G的清空等等.. 代码如下: 1 #include <stdio.h> 2 #include <stack> 3 #include <alg

UVA 796 Critical Links

#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include &l

[UVA796]Critical Links(割边, 桥)

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 求桥的数量,也就是割边的数量.输入有点小坑,左右括号外必须得有个空格才行,起初以为是转义的问题. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗┛┃\○/ 5 ┓┏┓┏┓┃ / 6 ┛┗┛┗┛┃ノ) 7 ┓┏┓┏┓┃ 8

C - Critical Links - uva 796(求桥)

题意:有一些网络通过一些线路连接,求关键的连接,也就是桥,如果删除这个链接那么会产生两个子树 分析:注意一下图不是连通图即可 ******************************************************************* #include<stdio.h>#include<string.h>#include<stack>#include<algorithm>using namespace std; const int 

Uva 796 求桥

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737 /*** Uva 796 求桥 题目要求:输出题目中所有的桥,按其所连接的点从小到大的顺序输出 解题思路:tarjan算法,所有树枝边都是桥(dfn[u]<low[v]),利用vector存储一下就可以了 */ #include <stdio.h> #include &

UVA796 - Critical Links(Tarjan求桥)

In a computer network a link L, which interconnects two servers, is considered critical if there are atleast two servers A and B such that all network interconnection paths between A and B pass through L.Removing a critical link generates two disjoin