题意:求桥

思路:求桥的条件是:(u,v)是父子边时 low[v]>dfn[u]

所以我们要解决的问题是怎么判断u,v是父子边(也叫树枝边)。我们在进行dfs的时候,要加入一个fa表示当前进行搜索的点的父节点。v=edge[v].v,如果dfn[v]==0即没访问过,那么肯定是父子边;如果v已经被访问过,我们就要做出筛选,只有v!=fa才进行low[u]=min(low[u],dfn[v]),因为v==fa时,(u,v)变成了返祖边,这时候low[u]被刷新成为fa的dfn,但是low是通过父子边所能找到的最早节点,固要舍去这种情况。

无向连通图的割点、桥

code:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=1e5+5;
const ll INF=1e5+5;
using namespace std;
int n,m,cnt,ecnt,num;
int dfn[N],low[N],head[N];
vector<int> g[N];
struct Edge{
int u,v,next;
}edge[N*10];
struct Ans{
int u,v;
}ans[N*10];
int cmp(Ans a,Ans b){
if(a.u<b.u) return 1;
else if(a.u==b.u && a.v<b.v) return 1;
return 0;
}
void add(int u,int v){
edge[num].u=u;
edge[num].v=v;
edge[num].next=head[u]; //表头
head[u]=num++;
} void tarjan(int x,int fa){ //fa是x的父节点
dfn[x]=low[x]=++cnt;
for(int i=head[x];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
tarjan(v,x);
low[x]=min(low[x],low[v]);
if(low[v]>dfn[x]){
int a,b;
a=x,b=v;
if(a>b) swap(a,b);
ans[ecnt].u=a,ans[ecnt].v=b;
ecnt++;
}
}
else if(v!=fa){
low[x]=min(low[x],dfn[v]);
} }
}
void init(){
cnt=ecnt=num=0;
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
}
int main(){
int t,T,a,b,n;
while(~scanf("%d",&t)){
init();
T=t;
while(T--){
scanf("%d (%d)",&a,&n);
while(n--){
scanf("%d",&b);
add(a,b);
}
}
for(int i=0;i<t;i++){
if(!dfn[i]) tarjan(i,i);
}
sort(ans,ans+ecnt,cmp);
cout<<ecnt<<" critical links"<<endl;
for(int i=0;i<ecnt;i++){
printf("%d - %d\n",ans[i].u,ans[i].v);
}
cout<<endl;
}
return 0;
}

这是在加边的时候判断是否重边

code2:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=1e5+5;
const ll INF=1e5+5;
using namespace std;
int n,m,cnt,ecnt,num;
int dfn[N],low[N],head[N];
vector<int> g[N];
struct Edge{
int u,v,next;
int flag;
}edge[N*10];
struct Ans{
int u,v;
}ans[N*10];
int cmp(Ans a,Ans b){
if(a.u<b.u) return 1;
else if(a.u==b.u && a.v<b.v) return 1;
return 0;
}
void add(int u,int v){
for(int i=head[u];i!=-1;i=edge[i].next){
if(edge[i].v==v){ //说明不是桥
edge[i].flag++;
edge[num].u=u;
edge[num].v=v;
edge[num].flag=1;
edge[num].next=head[u]; //表头
head[u]=num++;
return;
}
}
edge[num].u=u;
edge[num].v=v;
edge[num].flag=0;
edge[num].next=head[u]; //表头
head[u]=num++;
} void tarjan(int x,int fa){ //fa是x的父节点
dfn[x]=low[x]=++cnt;
for(int i=head[x];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(v==fa) continue;
if(!dfn[v]){
tarjan(v,x);
low[x]=min(low[x],low[v]);
if(low[v]>dfn[x] && edge[i].flag==0){
int a,b;
a=x,b=v;
if(a>b) swap(a,b);
ans[ecnt].u=a,ans[ecnt].v=b;
ecnt++;
}
}
else{
low[x]=min(low[x],dfn[v]);
} }
}
void init(){
cnt=ecnt=num=0;
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
}
int main(){
int t,T,a,b,n;
while(~scanf("%d",&t)){
init();
T=t;
while(T--){
scanf("%d (%d)",&a,&n);
while(n--){
scanf("%d",&b);
add(a,b);    //这里不能加add(b,a),在读到b时会加这条边,否则在下一次add(b,a)会判重边
}
}
for(int i=0;i<t;i++){
if(!dfn[i]) tarjan(i,i);
}
sort(ans,ans+ecnt,cmp);
cout<<ecnt<<" critical links"<<endl;
for(int i=0;i<ecnt;i++){
printf("%d - %d\n",ans[i].u,ans[i].v);
}
cout<<endl;
}
return 0;
}

UVA796 Critical Links(求桥) 题解的更多相关文章

  1. [UVA796]Critical Links(割边, 桥)

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

  2. UVA796 - Critical Links(Tarjan求桥)

    In a computer network a link L, which interconnects two servers, is considered critical if there are ...

  3. UVA796 Critical Links —— 割边(桥)

    题目链接:https://vjudge.net/problem/UVA-796 In a computer network a link L, which interconnects two serv ...

  4. uva-796.critical links(连通图的桥)

    本题大意:求出一个无向图的桥的个数并且按照顺序输出所有桥. 本题思路:注意判重就行了,就是一个桥的裸题. 判重思路目前知道的有两种,第一种是哈希判重,第二种和邻接矩阵的优化一样,就是只存图的上半角或者 ...

  5. Uva 796 Critical Links 找桥

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

  6. Uva796 Critical Links

    用tarjan缩点 然后用dfn[u] < low[v]缩点并且保存起来 在sort一遍输出 #include<stdio.h> #include<string.h> # ...

  7. UVA796:Critical Links(输出桥)

    Critical Links 题目链接:https://vjudge.net/problem/UVA-796 Description: In a computer network a link L, ...

  8. uva 796 C - Critical Links(tarjan求桥)

    题目链接:https://vjudge.net/contest/67418#problem/C 题意:求出桥的个数并且按顺序输出 题解:所谓桥就是去掉这条边后连通块增加,套用一下模版就行. #incl ...

  9. UVA 796 Critical Links(Tarjan求桥)

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

随机推荐

  1. 一个JS Class的“增删改查”

    function AA (){ var r={}; this.get = function(key){ return r[key]; } this.put = function(key,x){ r[k ...

  2. 洛谷P3527 MET-Meteors [POI2011] 整体二分

    正解:整体二分 解题报告: 传送门! 还有个双倍经验!(明明是一样的题目为什么你们一个紫一个黑啊喂! 这题首先要想到可以二分嘛,然后看到多组询问肯定就整体二分鸭 那就是基本套路啊,发现是区间修改单点查 ...

  3. TSNE数据降维学习【转载】

    转自:https://blog.csdn.net/u012162613/article/details/45920827 https://www.jianshu.com/p/d6e7083d7d61 ...

  4. Log4net 日志传到 graylog监控

    graylog是java的一个日志监控插件.存储用的是mongoDB,效率还是挺高的.不过嘛,文档太少了,安装和配置都很不容易. 官网:http://www.graylog.org/ 在graylog ...

  5. Android adb.exe程序启动不起来处理方法

    经常遇到 Please ensure that adb is correctly located at 'D:\java\sdk\platform-tools\adb.exe' and can be ...

  6. 软件包管理:rpm命令管理-校验和文件提取

    校验主要用于判断文件是否做了更改 修改标志: 会用-V,会看输出结果即可. 当有误操作,比如删了某一个文件,只需知道他属于哪一个rpm包,可用提取找回覆盖就行.并不把整个rpm包安装,而是提取其中的某 ...

  7. c# 获取某个进程的CPU使用百分百(类似任务管理器中显示CPU)

    using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...

  8. RVDS编译器

    不同ARM的体系结构,使用的ARM RealView编译工具时的异同点. 1)ARM体系结构v6K首次引入对4个CPU的MPCore处理器支持,高效的多重处理系统与单CPU系统相比,功耗更低,散热量更 ...

  9. yii的url写法

    Yii 各种url地址写法 echo Url::home(); 生成入口地址/yii2test/frontend/web/index.php: echo  Url::base();生成入口文件夹地址: ...

  10. minicom的安装和tftp的安装

    1.minicom 的安装 在弹出的窗口中选择“Serial port setup”进行配置.配置完之后选择“Save setup as dfl”保存.最后选择“Exit from Minicom”退 ...