【lightoj-1026】Critical Links(桥)

题意:

给出无向图,求桥的模板题。

#include <bits/stdc++.h>
using namespace std;
const int N = 10004;
int dfn[N], low[N];//时间戳;low[i]以i为根子树的最小祖先的时间戳
bool vis[N];
vector<int>V[N];
int n, num, c;
pair<int, int>P[N];
void dfs(int s, int f)
{
    low[s] = dfn[s] = ++num;
    for(unsigned int i = 0; i < V[s].size(); i++)
    {
        int v = V[s][i];
        if(!dfn[v])//未访问过(s-v为树边)
        {
            dfs(v, s);//dfs完更新出low[v]
            low[s] = min(low[s], low[v]);
            if(low[v] > dfn[s])//割边不加=
                P[++c].first = min(v, s), P[c].second = max(v, s);
        }
        else if(v != f) low[s] = min(low[s], dfn[v]);//回边且不是父子
    }
}
int main()
{
    int t, n, cas = 0, m, a, b;
    cin>>t;
    while(t--)
    {
        memset(dfn, 0, sizeof dfn);
        memset(low, 0, sizeof low);
        memset(vis, 0, sizeof vis);
        scanf("%d", &n);
        for(int i = 0 ; i < n; i++) V[i].clear();
        num = 0, c = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d (%d)", &a, &m);
            while(m--)
            {
                scanf("%d", &b);
                V[a].push_back(b);
                V[b].push_back(a);
            }
        }
        for(int i = 0; i < n; i++)
        {
            if(!vis[i])
                dfs(i, i);
        }
        sort(P+1, P+1+c);
        printf("Case %d:\n%d critical links\n", ++cas, c);
        for(int i = 1; i <= c; i++)
            printf("%d - %d\n", P[i].first, P[i].second);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lesroad/p/8747237.html

时间: 2024-10-10 20:29:58

【lightoj-1026】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]

Light OJ - 1026 - Critical Links(图论-Tarjan算法求无向图的桥数) - 带详细注释

 原题链接   无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 也可以先用Tajan()进行dfs算出所有点 的low和dfn值,并记录dfs过程中每个 点的父节点:然后再把所有点遍历一遍, 看其low和dfn,满足dfn[ fa ]<low[ i ](0<i<=n, i 的 father为fa) -- 则桥为fa-i. 找桥的时候,要注意看有没有重边:有重边,则不是桥. 另外,本题的题意及测试样例中没有重边,所以不用考虑重边. 带详细注释的题解: #include<s

Light OJ 1026 - Critical Links (图论-求割边, 桥)

题目大意:双向联通图, 现在求减少任意一边使图的联通性改变,按照起点从小到大列出所有这样的边 解题思路:双向边模版题 tarjan算法 代码如下: #include<bits/stdc++.h> using namespace std; const int N = 100003; vector<int>vec[N]; pair<int, int>edge[N]; int dfn[N], low[N]; int res, ans; void tarjan(int u, i

[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

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

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

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

C - Critical Links - uva 796(求桥)

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