题目链接:http://poj.org/problem?id=3177

边双连通问题,与点双连通还是有区别的!!!

题意是给你一个图(本来是连通的),问你需要加多少边,使任意两点间,都有两条边不重复的路径;

先将所有的边双连通分量看做一个点,此时的图就变成了一棵树,则题目变成了在树种添一些边,使任意两点间有两条不重复的路径,答案为(叶子节点数+1)/ 2 ;

#include "stdio.h"   //poj 3177 边双连通问题
#include "string.h" #define N 5050
#define M 10100 struct node
{
int x,y;
bool visit;
int next;
}edge[2*M];
int idx,head[N]; void Init()
{
idx = 0;
memset(head,-1,sizeof(head));
} void Add(int x,int y)
{
edge[idx].x = x;
edge[idx].y = y;
edge[idx].visit = false;
edge[idx].next = head[x];
head[x] = idx++;
} int time;
int low[N],dfn[N];
inline int MIN(int a,int b){ return a<b?a:b; } int st[M],num; //记录哪些点为桥
int stackk[2*M],top; //模拟栈(本题栈中存的是点,不是边) int countt; //记录有多少个双连通分量
int n,m;
bool mark[N];
int belong[N];
int du[N]; void lian_tong(int x)
{
int t;
while(1)
{
t = stackk[top];
top--;
belong[t] = countt;
if(t==x) break;
}
countt++;
} void DFS(int x)
{
int i,y;
stackk[++top] = x;
low[x] = dfn[x] = ++time;
for(i=head[x]; i!=-1; i=edge[i].next)
{
y = edge[i].y;
if(edge[i].visit) continue;
edge[i].visit = edge[i^1].visit = true;
if(!dfn[y])
{
DFS(y);
low[x] = MIN(low[x],low[y]);
if(low[y]>dfn[x])
st[num++] = i; //记录桥(两边双连通分量必定由桥相连)
}
else
low[x] = MIN(low[x],dfn[y]);
}
if(dfn[x]==low[x])
lian_tong(x); //标记当前边双连通分量
} int main()
{
int i;
int x,y;
while(scanf("%d %d",&n,&m)!=EOF)
{
Init();
for(i=0; i<m; ++i)
{
scanf("%d %d",&x,&y);
Add(x,y);
Add(y,x);
}
countt = 0; //统计边双连通分量的个数
num = 0; //统计桥的条数
top = 0; //栈
time = 0;
memset(dfn,0,sizeof(dfn));
for(i=1; i<=n; ++i) belong[i] = i;
DFS(1);
memset(du,0,sizeof(du));
for(i=0;i<num; ++i) //遍历每一个桥,统计每个边双连通分量的度
{
du[ belong[edge[st[i]].x] ] ++;
du[ belong[edge[st[i]].y] ] ++;
}
int ans = 0;
for(i=0; i<countt; ++i)
if(du[i]==1) ans++; //统计缩点后所形成的树种的叶子节点个数(度为1)
printf("%d\n",(ans+1)/2);
}
return 0;
}

poj 3177 Redundant Paths的更多相关文章

  1. tarjan算法求桥双连通分量 POJ 3177 Redundant Paths

    POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accept ...

  2. POJ 3177 Redundant Paths POJ 3352 Road Construction(双连接)

    POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的.一份代码能交.给定一个连通无向图,问加几条边能使得图变成一个双连通图 ...

  3. POJ 3177 Redundant Paths(边双连通的构造)

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13717   Accepted: 5824 ...

  4. POJ 3177——Redundant Paths——————【加边形成边双连通图】

    Redundant Paths Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  5. [双连通分量] POJ 3177 Redundant Paths

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 ...

  6. POJ 3177 Redundant Paths POJ 3352 Road Construction

    这两题是一样的,代码完全一样. 就是给了一个连通图,问加多少条边可以变成边双连通. 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话,把双连通子图收缩为一个点,形成一颗树 ...

  7. poj 3177 Redundant Paths【求最少添加多少条边可以使图变成双连通图】【缩点后求入度为1的点个数】

    Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11047   Accepted: 4725 ...

  8. POJ - 3177 Redundant Paths(边双连通分支)(模板)

    1.给定一个连通的无向图G,至少要添加几条边,才能使其变为双连通图. 2. 3. //边双连通分支 /* 去掉桥,其余的连通分支就是边双连通分支了.一个有桥的连通图要变成边双连通图的话, 把双连通子图 ...

  9. POJ 3177 Redundant Paths(强连通分量)

    题目链接:http://poj.org/problem?id=3177 题目大意是一个无向图给你n个点m条边,让你求出最少加多少条边 可以让任意两个点相通两条及以上的路线(每条路线点可以重复,但是每条 ...

随机推荐

  1. asp.net mvc中包含webapi时,token失效产生302的解决方案

    public void ConfigureAuth(IAppBuilder app) { app.UseCookieAuthentication(new CookieAuthenticationOpt ...

  2. onclick事件与onserverclick事件

    1.这里仅对web控件而言,onclick事件执行的是客户端中的代码, <%@ Page Language="C#" AutoEventWireup="true&q ...

  3. sencha gridpanel 单元格编辑

    { xtype: 'gridpanel', region: 'north', height: 150, title: 'My Grid Panel', store: 'A_Test_Store', c ...

  4. XMLHelper.cs

    http://yunpan.cn/Q7czcYTwE8qkc  提取码 545c using System; using System.Linq; using System.Xml.Linq; usi ...

  5. csharp: InvokeHelper

    Entity Framework https://entityframework.codeplex.com/ Enterprise Library https://entlib.codeplex.co ...

  6. POSTMAN and HTTPie to test APIs

    http://blog.mashape.com/postman-httpie-test-apis/ We love working with APIs at Mashape, and we love ...

  7. 基于bootstrap的图片轮播效果展示

    <!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8&q ...

  8. KMP--Cyclic Nacklace

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/D Description CC always be ...

  9. [moka同学代码]PHP初级知识:上传文件源码

    1.目录结构

  10. PHP学习笔记:对命名空间(namespace)学习资料的翻译

    Name collisions means: you create a function named db_connect, and somebody elses code that you use ...