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, int f)
{
    dfn[u] = low[u] = res ++;
    for(int i = 0; i < vec[u].size(); ++ i)
    {
        int v = vec[u][i];
        if(dfn[v] == -1)
        {
            tarjan(v, u);
            low[u] = min(low[u], low[v]);
        }
        else if(f != v)
            low[u] = min(low[u], dfn[v]);

        if(dfn[u] < low[v])
        {
            if(u > v)
                edge[ans ++] = make_pair(v, u);
            else
                edge[ans ++] = make_pair(u, v);
        }
    }
}

void solve(int cases)
{
    for(int i = 0; i < N; ++ i)
        vec[i].clear();

    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; ++ i)
    {
        int a, b, c;
        scanf("%d (%d)", &a, &b);
        for(int j = 1; j <= b; ++ j)
        {
            scanf("%d", &c);
            vec[a].push_back(c);
        }
    }

    memset(dfn, -1, sizeof(dfn));
    memset(low, -1, sizeof(low));

    ans = res = 0;
    for(int i = 0; i < n; ++ i)
    {
        if(dfn[i] == -1)
            tarjan(i, -1);
    }
    sort(edge, edge+ans);
    printf("Case %d:\n", cases);
    printf("%d critical links\n", ans);
    for(int i=0; i<ans; ++ i)
        printf("%d - %d\n", edge[i].first, edge[i].second);
}

int main()
{
    int T;
    scanf("%d", &T);
    for(int i = 1; i <= T; ++ i)
        solve(i);
    return 0;
}

时间: 2024-10-28 05:47:36

Light OJ 1026 - Critical Links (图论-求割边, 桥)的相关文章

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 求桥

题目 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 —— (求割边(桥))

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

Light OJ 1028 Trailing Zeroes (I) 求n因子数

今天真机调试的时候莫名其妙遇到了这样的一个问题: This product type must be built using a provisioning profile, however no provisioning profile matching both the identity "iPhone Developer" and the bundle identifier..... 具体如下图所示: 十分蛋疼, 发现不管是从网上下的demo, 还是自己的过程.凡事真机测试的时候都

Light OJ 1054 Efficient Pseudo Code 求n^m的约数和

题目来源:Light OJ 1054 Efficient Pseudo Code 题意:求n的m次这个数的所有的约数和 思路:首先对于一个数n = p1^a1*p2^a2*p3^a3*-*pk^ak  约束和s = (p1^0+p1^1+p1^2+-p1^a1)(p2^0+p2^1+p2^2+-p2^a2)-(pk^0+pk^1+pk^2+-pk^ak) 然后就是先求素数表 分解因子 然后求p1^0+p1^1+p1^2+-p1^a1 这是一个以1开始的等比数列 就是(1-q^n)/(1-q) 最

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

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