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

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

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

  代码如下:

 #include <stdio.h>
#include <stack>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std; const int N = +; stack<int> S;
int scc_cnt;
int dfs_clock;
int dfn[N];
int low[N];
int iscut[N];
vector<int> G[N];
int n; struct bridge
{
int u,v;
void clear()
{
if(this->u > this->v) swap(this->u,this->v);
}
bool operator < (const bridge & A) const
{
return u==A.u ? v<A.v : u<A.u;
}
};
vector<bridge> ans; void dfs(int u,int fa)
{
dfn[u]=low[u]=++dfs_clock;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!dfn[v])
{
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>dfn[u])
{
bridge bri = (bridge){u,v};
bri.clear();
ans.push_back(bri);
}
}
else if(dfn[v]<dfn[u] && v!=fa)
{
low[u]=min(low[u],dfn[v]);
}
}
} void init()
{
for(int i=;i<n;i++) G[i].clear();
memset(dfn,,sizeof(dfn));
dfs_clock=;
ans.clear();
} void solve()
{
for(int i=;i<n;i++)
{
if(!dfn[i]) dfs(i,-);
} sort(ans.begin(),ans.end());
printf("%d critical links\n",ans.size());
for(int i=;i<ans.size();i++)
{
printf("%d - %d\n",ans[i].u,ans[i].v);
}
puts("");
} int main()
{
while(scanf("%d",&n)==)
{
init(); for(int i=;i<=n;i++)
{
int u,num;
scanf("%d (%d)",&u,&num); while(num--)
{
int v;
scanf("%d",&v);
G[u].push_back(v);
G[v].push_back(u);
}
}
solve();
}
return ;
}

UVA 796 Critical Links —— (求割边(桥))的更多相关文章

  1. Uva 796 Critical Links (割边+排序)

    题目链接: Uva 796 Critical Links 题目描述: 题目中给出一个有可能不连通的无向图,求出这个图的桥,并且把桥按照起点升序输出(还有啊,还有啊,每个桥的起点要比终点靠前啊),这个题 ...

  2. uva 796 Critical Links(无向图求桥)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. UVA 796 Critical Links(Tarjan求桥)

    题目是PDF就没截图了 这题似乎没有重边,若有重边的话这两点任意一条边都不是桥,跟求割点类似的原理 代码: #include <stdio.h> #include <bits/std ...

  4. UVA 796 - Critical Links (求桥)

    Critical Links  In a computer network a link L, which interconnects two servers, is considered criti ...

  5. UVA 796 Critical Links(无向图求桥)

    题目大意:给你一个网络要求这里面的桥. 输入数据: n 个点 点的编号  (与这个点相连的点的个数m)  依次是m个点的   输入到文件结束. 桥输出的时候需要排序   知识汇总: 桥:   无向连通 ...

  6. UVA 796 Critical Links(模板题)(无向图求桥)

    <题目链接> 题目大意: 无向连通图求桥,并将桥按顺序输出. 解题分析: 无向图求桥的模板题,下面用了kuangbin的模板. #include <cstdio> #inclu ...

  7. UVA 796 Critical Links (tarjan算法求割边)

    这是在kuangbin的题目里看到的,不得不吐槽一下,题目中居然没给出数据范围,还是我自己猜的-本来是一道挺裸的题,但是我wa了好多次,原因就是这里面有两个坑点,1重边特判,2输出时左边必须比右边小. ...

  8. UVA 796 - Critical Links 无向图字典序输出桥

    题目:传送门 题意:给你一个无向图,你需要找出里面的桥,并把所有桥按字典序输出 这一道题就是用无向图求桥的模板就可以了. 我一直错就是因为我在输入路径的时候少考虑一点 错误代码+原因: 1 #incl ...

  9. Uva 796 Critical Links 找桥

    这个题很简单,但是输入有毒,用字符串的我一直RE 然后换成这样瞬间AC #include <stdio.h> #include <string.h> #include < ...

随机推荐

  1. 怎样初始化XMLHttpRequest实例对象xhr

    xhr.open() 接收5个参数, 用于初始化一个http请求, 它接收5个参数: 1. method: 请求类型; 2. url: 请求的url; 3. async: 是否为异步, 默认为true ...

  2. Servlet获取JSP中的汉字乱码问题解决方案

    1.String customerName=request.getParameter("customer_name");这样会出现乱码 解决方案很简单: String custom ...

  3. python 的常见排序算法实现

    python 的常见排序算法实现 参考以下链接:https://www.cnblogs.com/shiluoliming/p/6740585.html 算法(Algorithm)是指解题方案的准确而完 ...

  4. PHP如何通过URL访问,获得新的URL 两种方法

    1.1 $url = 'http://passport.drcloud.cn/api/logon.asp?id=1&ru=http://203.158.158.122/store/thirdL ...

  5. vue 父子组件数据的双向绑定大法

    官方文档说明 所有的 prop 都使得其父子 prop 之间形成了一个 单向下行绑定 父级 prop 的更新会向下流动到子组件中,但是反过来则不行 2.3.0+ 新增 .sync 修饰符 以 upda ...

  6. Javascript中的事件二

    <!------------------示例代码一---------------------><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  7. 07 Redis存储Session

    django-redis-sessions 官方文档:https://pypi.org/project/django-redis-sessions/ dango-redis 官方文档:http://n ...

  8. react快速上手二(使用JSX语法)

    前提: 下载依赖,配置 cnpm i babel-preset-react -D JSX语法的本质: 还是以 React.createElement 的形式来实现的,并没有直接把 用户写的 HTML代 ...

  9. PHP 获取数组指定值的位置或下标

    <?php     //定义一个数组     $array = array(0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');     // ...

  10. Go语言根据数据表自动生成model以及controller代码

    手写model的用法请参考: https://www.jianshu.com/p/f5784b8c00d0 这里仅说明自动生成model文件的过程 bee generate appcode -tabl ...