UVA 796 Critical Links(无向图求桥)

题目来源:

UVa Online Judge
https://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 N 1010
using namespace std;
struct node
{
    int x, y;
} no[N];
int dfn[N], low[N];
int f[N], Time, n;
vector<vector<int> >G;
void Init()
{
    G.clear();
    G.resize(n+1);
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(f, 0, sizeof(f));
    Time = 0;
}
void Tarjan(int u, int fa)
{
    int i, len, v;
    dfn[u] = low[u] = ++Time;
    f[u] = fa;
    len = G[u].size();
    for (i = 0; i < len; i++)
    {
        v = G[u][i];
        if (!dfn[v])
        {
            Tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if (v != fa) low[u] = min(low[u], dfn[v]);
    }
}
int cmp(node a, node b)
{
    if (a.x != b.x) return a.x < b.x;
    return a.y < b.y;
} //从小到大排序
int main ()
{
    int a, b, m, k, i, ni, mn;
    while (scanf("%d", &n) != EOF)
    {
        k = 0;
        Init();
        mn = n;
        while (mn--)
        {
            scanf("%d (%d)", &a, &m);
            while (m--)
            {
                scanf("%d", &b);
                G[a].push_back(b);
                G[b].push_back(a);
            }
        }
        for (i = 0; i < n; i++)
            if (!dfn[i]) Tarjan(i, -1); //可能出现多个连通图的现象,需要每次进行查找,一次查找后该连通图的节点的dfn都不为0,借此可以判断是否还有其他连通图
        for (i = 0; i < n; i++)
        {
            ni = f[i];
            if (ni != -1 && dfn[ni] < low[i]) //父节点为-1说明该点为根节点,与其父节点不可能存在桥
            {                                 //dfn[ni]==low[i]说明i还有其他路径到ni,则说明i-ni不属于桥
                no[k].x = i;
                no[k].y = ni;
                if (i > ni) swap(no[k].x, no[k].y);
                k++;
            }
        }
        sort(no, no+k, cmp);
        printf("%d critical links\n", k);
        for (i = 0; i < k; i++) printf("%d - %d\n", no[i].x, no[i].y);
        printf("\n");
    }
    return 0;
}
时间: 2024-08-23 01:29:33

UVA 796 Critical Links(无向图求桥)的相关文章

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 (求桥按序输出)

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(i

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(无向图的桥)

题目链接 题意: 给出一个无向图,按顺序输出桥 思路:求出所有的桥,然后按顺序输出即可 代码: #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <utility> #include <algorithm> using namespace std; const int MAXN = 10005; struct Edge

UVA796:Critical Links(输出桥)

Critical Links 题目链接:https://vjudge.net/problem/UVA-796 Description: In a computer network a link L, which interconnects two servers, is considered critical if there are at least two servers A and B such that all network interconnection paths between

UVA 315 Network(无向图求割点)

题目大意 A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two pl

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

UVA 318 Network(无向图求割点数)

Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect togethe