poj 3352 求 边-双连通分量

【题意】 给出一张无向连通图,求至少连几条边可以变成边双连通图

【思路】求出边-双连通分量,缩点就成了一棵树,求这棵树里的出度为1 的点num  结果是(num-1)/2;

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stack>
 5 #include<vector>
 6 using namespace std;
 7 int pre[1002],low[1002],n,adj[1002],num,c,du[1002];
 8 struct E
 9 {
10     int to;
11     int next;
12 } edge[500000];
13
14 void add(int a,int b)
15 {
16     edge[num].to=b;
17     edge[num].next=adj[a];
18     adj[a]=num++;
19 }
20
21 int dfs(int u,int fa)
22 {
23     int i,v,lowv;
24     pre[u]=low[u]=c++;
25     for(i=adj[u]; i!=-1; i=edge[i].next)
26     {
27         int v;
28         v=edge[i].to;
29         if(v!=fa&&pre[v]<pre[u])
30         {
31             if(!pre[v])
32             {
33                 lowv=dfs(v,u);
34                 low[u]=min(low[u],lowv);
35             }
36             else if(pre[v]&&v!=fa)
37                 low[u]=min(low[u],pre[v]);
38         }
39     }
40     return low[u];
41 }
42
43 int main()
44 {
45     int a,b,m,i,v;
46     while(~scanf("%d%d",&n,&m))
47     {
48         memset(adj,-1,sizeof(adj));
49         num=0;
50         while(m--)
51         {
52             scanf("%d%d",&a,&b);
53             add(a,b);
54             add(b,a);
55         }
56         c=1;
57         memset(pre,0,sizeof(pre));
58         for(int i=1; i<=n; i++)
59             if(pre[i]==0)
60                 dfs(i,-1);
61         memset(du,0,sizeof(du));
62         for(int u=1; u<=n; u++)
63         {
64             for(i=adj[u]; i!=-1; i=edge[i].next)
65             {
66                 int v;
67                 v=edge[i].to;
68                 if(low[u]!=low[v])
69                 {
70                     du[low[u]]++;
71                     du[low[v]]++;
72                 }
73             }
74         }
75         int ans=0;
76         for(int i=0; i<=n; i++)
77             if(du[i]/2==1)
78                 ans++;
79         printf("%d\n",(ans+1)/2);
80     }
81     return 0;
82 }

poj 3352 求 边-双连通分量

时间: 2024-08-08 20:34:31

poj 3352 求 边-双连通分量的相关文章

Road Construction POJ - 3352 (边双连通分量)

Road Construction POJ - 3352 题意:一个无向图(无重边),问至少还要加多少边使得去掉任意一条边后任意两点仍可互达. 无向图的边双连通分量(无重边) 先用一次dfs标记出割边,然后dfs标记出各联通分量 再根据割边,缩点重新建图,生成一颗树 则答案就是(叶子树+1)/2. 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring

POJ 3352 无向图的双连通分量

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9426   Accepted: 4675 Description It's almost summer time, and that means that it's almost summer construction time! This year, the good people who are in charge of the ro

bzoj1123 BLO tarjan求点双连通分量

填坑--链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1123 题意:问切断第i个点之后多少对点不再联通. 就是个求割点同时计算出双连通分量大小嘛-- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=10000

POJ 3177 - Redundant Paths - 双连通分量

题目大意: 给定一个N个点,M条边的无向连通图(可能有重边),要求让任意两点间都有两条或以上的路径,且这些路径没有公共边.问至少需要加多少条边? N<=5e3,M<=1e4. 求双连通分量并缩点.详见:https://www.cnblogs.com/frog112111/p/3367039.html 注意由于本题数据允许重边(尽管讨论区有人吐槽数据太水),DFS时判断割边的条件应为low[to] > dfn[cur],且该边的重数为1. 实现的时候用了并查集来维护属于同一双联通分量的点,

【POJ3352】Road Construction tarjan求边-双连通分量,裸题模板题

转载请注明出处:http://blog.csdn.net/vmurder/article/details/42671851 其实我就是觉得原创的访问量比未授权盗版多有点不爽233... 裸题只给模板. tarjan可以实现. 太水不发题解. 代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 1010 #define M 202

Redundant Paths POJ - 3177(边—双连通分量)

题意: 在图中加边 看最少能通过加多少条边把 图变成边-双连通分量 解析: 先做一次dfs,不同的连通分量的low是不同的  注意重边 缩点 统计度为1的点  那么需要加的边为(ret+1)/2 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include

Tarjan求点双连通分量

概述 在一个无向图中,若任意两点间至少存在两条"点不重复"的路径,则说这个图是点双连通的(简称双连通,biconnected) 在一个无向图中,点双连通的极大子图称为点双连通分量(简称双连通分量,Biconnected Component,BCC) 性质 任意两点间至少存在两条点不重复的路径等价于图中删去任意一个点都不会改变图的连通性,即BCC中无割点 若BCC间有公共点,则公共点为原图的割点 无向连通图中割点一定属于至少两个BCC,非割点只属于一个BCC 算法 在Tarjan过程中维

poj 3177 &amp; 3352 【无向图双连通分量Tarjan】

题目:poj 3177 & 3352 题意:大概意思就是给你一个无向图,让你添加最少的边,让所有点都双连通. 分析:双连通的定义就是任意两个点至少有两条路可达. 其实做法跟添加最少边强连通一样,先对图中已经双连通的缩点,然后重新编号. 这就是著名的Tanjan算法. 通过搜索的思想对所有存在环的边遍相同的号 如果要让所有的点双连通,那么对于缩点后的图中如果度数为 1 的话,这些边肯定要连接到其他的点才能双连通,而题目要求添加最少,所以连接到其他度数也为 1 的点是最优的,那么答案就是(odd+1

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

POJ 3177 Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12598   Accepted: 5330 Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the re