题意:求桥

思路:求桥的条件是:(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. 异步通信----WebSocket

    什么是WebSocket? WebSocket API是下一代客户端-服务器的异步通信方法.该通信取代了单个的TCP套接字,使用ws或wss协议,可用于任意的客户端和服务器程序.WebSocket目前 ...

  2. virtualenvwrapper 的安装和使用(转)

    原文:http://www.cnblogs.com/asmer-stone/p/5470144.html virtualenvwrapper是用来管理virtualenv的扩展包,用着很方便. 1. ...

  3. Shell初学(一)hello world

    精简: 1.创建:可以使用 vi/vim 命令来创建文件如: test.sh   ,扩展名并不影响脚本执行,写什么都可以. 2.hello_world: #!/bin/bash            ...

  4. application实例

    application详解及实例 application对象用来在多个程序或者是多个用户之间共享数据,用户使用的所有application对象都是一样的,这与session对象不同.服务器一旦启动,就 ...

  5. Y2K Accounting Bug(poj2586)

    题意: 有一个公司由于某个病毒使公司赢亏数据丢失,但该公司每月的 赢亏是一个定数,要么一个月赢利s,要么一月亏d.现在ACM只知道该公司每五个月有一个赢亏报表,而且每次报表赢利情况都为亏.在一年中这样 ...

  6. [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  7. Mantle 与Injection

    本来Injection可以本地打补丁实时修改代码,但是不知道Mantle的数据类为何不能打补丁,可能因为Mantle利用了很多运行时的技术吧.

  8. spark shuffle原理

    1.spark中窄依赖的时候不需要shuffle,只有宽依赖的时候需要shuffle,mapreduce中map到reduce必须经过shuffle 2.spark中的shuffle fetch的时候 ...

  9. PHP中header('content-type:text/html;charset="utf-8')和error_reporting()的作用

    1.header PHP文件插入header("Content-type: text/html; charset=utf-8");相当于页面里面的<meta http-equ ...

  10. C:\WINDOWS\system32\drivers\etc\hosts 文件的作用

    先来看一看C:\WINDOWS\system32\drivers\etc\hosts 系统原来的hosts文件(未经过改动) 打开原来的hosts文件,查看原来的内容 host是一个没有扩展名的系统文 ...