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

  和求割点类似,只要把>=改成>即可。这里想解释一下的是,无向图没有重边,怎么可以使得low[v]=dfn[u]呢?只要它们之间再来一个点即可。

  总感觉图论要很仔细地想啊- -一不小心就弄混了。。

  另外从这题发现,代码还是写成模块化比较好,比如solve一个函数,init一个函数等等,这样可以避免很多东西忘记写,比方说dfn或者G的清空等等。。

  代码如下:

  1 #include <stdio.h>
  2 #include <stack>
  3 #include <algorithm>
  4 #include <string.h>
  5 #include <vector>
  6 using namespace std;
  7
  8 const int N = 100+5;
  9
 10 stack<int> S;
 11 int scc_cnt;
 12 int dfs_clock;
 13 int dfn[N];
 14 int low[N];
 15 int iscut[N];
 16 vector<int> G[N];
 17 int n;
 18
 19 struct bridge
 20 {
 21     int u,v;
 22     void clear()
 23     {
 24         if(this->u > this->v) swap(this->u,this->v);
 25     }
 26     bool operator < (const bridge & A) const
 27     {
 28         return u==A.u ? v<A.v : u<A.u;
 29     }
 30 };
 31 vector<bridge> ans;
 32
 33 void dfs(int u,int fa)
 34 {
 35     dfn[u]=low[u]=++dfs_clock;
 36     for(int i=0;i<G[u].size();i++)
 37     {
 38         int v = G[u][i];
 39         if(!dfn[v])
 40         {
 41             dfs(v,u);
 42             low[u]=min(low[u],low[v]);
 43             if(low[v]>dfn[u])
 44             {
 45                 bridge bri = (bridge){u,v};
 46                 bri.clear();
 47                 ans.push_back(bri);
 48             }
 49         }
 50         else if(dfn[v]<dfn[u] && v!=fa)
 51         {
 52             low[u]=min(low[u],dfn[v]);
 53         }
 54     }
 55 }
 56
 57 void init()
 58 {
 59     for(int i=0;i<n;i++) G[i].clear();
 60     memset(dfn,0,sizeof(dfn));
 61     dfs_clock=0;
 62     ans.clear();
 63 }
 64
 65 void solve()
 66 {
 67     for(int i=0;i<n;i++)
 68     {
 69         if(!dfn[i]) dfs(i,-1);
 70     }
 71
 72     sort(ans.begin(),ans.end());
 73     printf("%d critical links\n",ans.size());
 74     for(int i=0;i<ans.size();i++)
 75     {
 76         printf("%d - %d\n",ans[i].u,ans[i].v);
 77     }
 78     puts("");
 79 }
 80
 81 int main()
 82 {
 83     while(scanf("%d",&n)==1)
 84     {
 85         init();
 86
 87         for(int i=1;i<=n;i++)
 88         {
 89             int u,num;
 90             scanf("%d (%d)",&u,&num);
 91
 92             while(num--)
 93             {
 94                 int v;
 95                 scanf("%d",&v);
 96                 G[u].push_back(v);
 97                 G[v].push_back(u);
 98             }
 99         }
100         solve();
101     }
102     return 0;
103 }
时间: 2024-10-10 10:23:29

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

题目来源: 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

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

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]

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

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

C - Critical Links - uva 796(求桥)

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

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