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 rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another.

Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way.

There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

Input

Line 1: Two space-separated integers: F and R

Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

Output

Line 1: A single integer that is the number of new paths that must be built.

Sample Input

7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7

Sample Output

2

Hint

Explanation of the sample:

One visualization of the paths is:

   1   2   3   +---+---+         |   |       |   | 6 +---+---+ 4      / 5     /     /  7 +

Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions.

   1   2   3   +---+---+     :   |   |   :   |   | 6 +---+---+ 4      / 5  :     /     :    /      : 7 + - - - - 

Check some of the routes: 
1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
Every pair of fields is, in fact, connected by two routes. 
It‘s possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.

  1 /*这是一个63分的代码,因为没有注意到题目中的重边问题,以后要注意有重边的图和没有重边的图的tarjan求桥的算法,是不同的*/
  2 #include<iostream>
  3 using namespace  std;
  4 #include<cstdio>
  5 #define N 5001
  6 #define R 10010
  7 #include<stack>
  8 #include<queue>
  9 #include<cstring>
 10 queue<int>que;
 11 bool qiao[R]={0},visit[N]={0},visit_edge[R<<1];
 12 struct Edge{
 13     int u,v,last;
 14 }edge[R*2];
 15 int head[N],du[N],f,r,father[N],dfn[N],low[N],topt=0,t=-1;
 16 int ans[N]={0};
 17 void add_edge(int u,int v)
 18 {
 19     ++t;
 20     edge[t].u=u;
 21     edge[t].v=v;
 22     edge[t].last=head[u];
 23     head[u]=t;
 24 }
 25 void input()
 26 {
 27     memset(head,-1,sizeof(head));
 28     int u,v;
 29     scanf("%d%d",&f,&r);
 30     for(int i=1;i<=r;++i)
 31     {
 32         scanf("%d%d",&u,&v);
 33         add_edge(u,v);
 34         add_edge(v,u);
 35     }
 36     r<<=1;
 37 }
 38 void tarjan(int u)
 39 {
 40     dfn[u]=low[u]=++topt;
 41     for(int l=head[u];l!=-1;l=edge[l].last)
 42     {
 43         int v=edge[l].v;
 44         if(!visit_edge[l]&&!visit_edge[l^1])
 45         {
 46           visit_edge[l]=true;
 47             if(!dfn[v])
 48           {
 49             tarjan(v);
 50             low[u]=min(low[u],low[v]);
 51             if(low[v]>dfn[u]) qiao[l]=true;
 52           }
 53           else low[u]=min(low[u],dfn[v]);
 54         }
 55     }
 56 }
 57 void suo_dian()
 58 {
 59     for(int i=1;i<=f;++i)
 60     {
 61         if(!visit[i])
 62         {
 63             ans[++ans[0]]=i;
 64             que.push(i);
 65             visit[i]=true;
 66             while(!que.empty())
 67             {
 68                 int x=que.front();
 69                 father[x]=i;
 70                 que.pop();
 71                 for(int l=head[x];l!=-1;l=edge[l].last)
 72                 {
 73                     if(qiao[l]||visit[edge[l].v]) continue;
 74                     que.push(edge[l].v);
 75                     visit[edge[l].v]=true;
 76                 }
 77             }
 78
 79         }
 80     }
 81 }
 82 void re_jiantu()
 83 {
 84     for(int l=0;l<=r;++l)
 85     {
 86         if(father[edge[l].u]!=father[edge[l].v])
 87         {
 88             du[father[edge[l].u]]++;
 89             du[father[edge[l].v]]++;
 90         }
 91     }
 92 }
 93 int main()
 94 {
 95     freopen("rpaths.in","r",stdin);
 96     freopen("rpaths.out","w",stdout);
 97     input();
 98     for(int i=1;i<=f;++i)
 99     {
100         if(!dfn[i])
101            tarjan(i);
102     }
103     suo_dian();
104     re_jiantu();
105     int sum=0;
106     for(int i=1;i<=ans[0];++i)
107       if(du[ans[i]]==2)
108         sum++;
109     printf("%d\n",(sum+1)/2);
110     fclose(stdin);fclose(stdout);
111     return 0;
112 }

正确代码及模板:

 1 #define N 5011
 2 #include<iostream>
 3 using namespace std;
 4 #define M 10010
 5 #include<cstdio>
 6 #include<cstring>
 7 struct Gra{
 8     int n,m,ans,head[N],topt,dfn[N],low[N],t,cnt[N];
 9     bool visit[M<<1];
10     struct Edge{
11         int v,last;
12     }edge[M<<1];
13     void init(int f,int r)
14     {/*初始化不要在上面,上面只是声明,不是变量*/
15         ans=0,topt=0,t=-1;
16         n=f;m=r;
17         memset(head,-1,sizeof(head));
18         memset(dfn,0,sizeof(dfn));
19         memset(low,0,sizeof(low));
20         memset(cnt,0,sizeof(cnt));
21         memset(visit,false,sizeof(visit));
22     }
23     void add_edge(int x,int y)
24     {
25         ++t;
26         edge[t].v=y;
27         edge[t].last=head[x];
28             head[x]=t;
29     }
30     void tarjan(int u)
31     {
32         dfn[u]=low[u]=++topt;
33         for(int l=head[u];l!=-1;l=edge[l].last)
34         {
35             if(visit[l]) continue;
36             visit[l]=visit[l^1]=true;/*找到无向边拆成的另一条边*/
37             int v=edge[l].v;
38             if(!dfn[v])
39             {
40                 tarjan(v);
41                 low[u]=min(low[v],low[u]);
42             }
43             else low[u]=min(low[u],dfn[v]);/*多次返祖*/
44         }
45     }
46     void start()
47     {
48         for(int i=1;i<=n;++i)
49           if(!dfn[i])
50             tarjan(i);
51         for(int i=1;i<=n;++i)/*处理缩点以后的图*/
52           for(int l=head[i];l!=-1;l=edge[l].last)
53           {
54               int v=edge[l].v;
55               if(low[i]!=low[v])
56                  cnt[low[v]]++;
57                        /*low[x]!=low[y]说明从low[y]回不到low[x],那么low[x]--low[y]是一条桥,因为tarjan中多次返祖*/
58           }
59         for(int i=1;i<=n;++i)
60             if(cnt[i]==1) ans++;/*统计度数是1的叶子节点的数目*/
61         printf("%d\n",(ans+1)>>1);
62     }
63 }G;
64 int main()
65 {
66     int n,m;
67     scanf("%d%d",&n,&m);
68     G.init(n,m);
69     int x,y;
70     for(int i=1;i<=m;++i)
71     {
72         scanf("%d%d",&x,&y);
73         G.add_edge(x,y);
74         G.add_edge(y,x);
75     }
76     G.start();
77     return 0;
78 }
时间: 2024-12-08 15:31:12

tarjan算法求桥双连通分量 POJ 3177 Redundant Paths的相关文章

[双连通分量] POJ 3177 Redundant Paths

Redundant Paths Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13712   Accepted: 5821 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 rest of the

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点)

POJ 3352 Road Construction POJ 3177 Redundant Paths(边双连通图 Tarjan+缩点) ACM 题目地址: POJ 3352 Road Construction POJ 3177 Redundant Paths 题意: 问要添加几条边才能使所给无向图图变成边双连通图. 分析: 边连通度:使无向图G不连通的最少删边数量为其边连通度. 边双连通图:边连通度大于1的无向图. 首先缩点,让图变成一个DAG. 现在问题转化为:在树中至少添加多少条边能使图变

POJ 3177 Redundant Paths(Tarjan)

题目链接 题意 : 一个无向连通图,最少添加几条边使其成为一个边连通分量 . 思路 :先用Tarjan缩点,缩点之后的图一定是一棵树,边连通度为1.然后找到所有叶子节点,即度数为1的节点的个数leaf,最后要添加的边的条数就是(leaf+1)/2 : 1 // 3177 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 7 using

POJ 3177 Redundant Paths POJ 3352 Road Construction(双连通)

POJ 3177 Redundant Paths POJ 3352 Road Construction 题目链接 题意:两题一样的,一份代码能交,给定一个连通无向图,问加几条边能使得图变成一个双连通图 思路:先求双连通,缩点后,计算入度为1的个数,然后(个数 + 1) / 2 就是答案(这题由于是只有一个连通块所以可以这么搞,如果有多个,就不能这样搞了) 代码: #include <cstdio> #include <cstring> #include <algorithm&

poj 3177 Redundant Paths

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 rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forc

POJ 3177 Redundant Paths (桥,边双连通分量,有重边)

题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看3352的题解http://www.cnblogs.com/xcw0754/p/4619594.html. 其实与3352不同的就是重边出现了怎么办?假如出现的重边刚好是桥呢? 首先要知道,[割点]可以将两个[点双连通分量]隔开来,因为仅一个[点双连通分量]中肯定无割点,那么每两个点对都同时处于若干

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

[题目链接] http://poj.org/problem?id=3177 [题目大意] 给出一张图,问增加几条边,使得整张图构成双连通分量 [题解] 首先我们对图进行双连通分量缩点, 那么问题就转化为给出一棵树,加边使得其成为边双连通分量的最小边数, 只要从叶节点连一条边到任意节点,那么就可以使得这个叶节点加入到双连通分量中, 那么优先叶节点和叶节点连接,所以其答案为(叶节点+1)/2 [代码] #include <cstdio> #include <algorithm> #in

POJ 3177 Redundant Paths 边双连通分量+缩点

题目链接: poj3177 题意: 给出一张连通图,为了让任意两点都有两条通路(不能重边,可以重点),至少需要加多少条边 题解思路: 分析:在同一个边双连通分量中,任意两点都有至少两条独立路可达,所以同一个边双连通分量里的所有点可以看做同一个点. 缩点后,新图是一棵树,树的边就是原无向图桥. 现在问题转化为:在树中至少添加多少条边能使图变为双连通图. 结论:添加边数=(树中度为1的节点数+1)/2 具体方法为,首先把两个最近公共祖先最远的两个叶节点之间连接一条边,这样可以把这两个点到祖先的路径上

Poj 3352 Road Construction &amp; Poj 3177 Redundant Paths(边双连通分量+缩点)

Road Construction Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9465   Accepted: 4699 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