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

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

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.

题意:F 个牧场,R条路将这F个牧场连接起来,但是有些牧场互相之间只 可以通过一个 固定的路径到达,现在要求添加路径,使任意两个牧场之间有两条以上路径(之间没有桥)问最少添加多少条边
题解:求出所有ebc  缩点后,求出所有度数为1的点sum,  (sum+1)/2即为结果      因为所有点都间接或者直接连在一起,度数为1的点,证明其不可能是双连通,此时需要将这些独立的双连通分量连接起来
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
#include<algorithm>
#include<vector>
#define MAX 20010
#define INF 0x7fffff
using namespace std;
struct node
{
int beg,end,next;
}edge[MAX];
int head[MAX],ans,bridge;
int low[MAX],dfn[MAX],in[MAX];
int dfsclock,ebccnt;
int instack[MAX],ebcno[MAX];
vector<int>newmap[MAX];
stack<int>s;
int n,m;
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
add(b,a);
}
}
void tarjan(int u,int fa)
{
int v;
instack[u]=1;
s.push(u);
low[u]=dfn[u]=++dfsclock;
bool flag=true;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(flag&&v==fa)
{
flag=false;
continue;
}
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
bridge++;
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
ebccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
ebcno[v]=ebccnt;
if(v==u)
break;
}
}
}
void find()
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(ebcno,0,sizeof(ebcno));
dfsclock=ebccnt=bridge=0;
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-1);
}
}
void suodian()
{
int u,v;
memset(in,0,sizeof(in));
for(int i=0;i<ans;i+=2)
{
u=ebcno[edge[i].beg];
v=ebcno[edge[i].end];
if(u!=v)
{
newmap[u].push_back(v);
newmap[v].push_back(u);
in[u]++;
in[v]++;
}
}
}
void solve()
{
int i,j,sum=0;;
for(i=1;i<=ebccnt;i++)
{
if(in[i]==1)
sum++;
}
printf("%d\n",(sum+1)/2);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
find();
suodian();
solve();
}
return 0;
}

  

上一篇:is 和 == 的区别,utf和gbk的转换,join用法


下一篇:关于spark standalone模式下的executor问题