# poj 3177 双连通分量+缩点

143人阅读 评论(0)

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.

#include <iostream>
#include <algorithm>
#include <cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
const int MAX=10010;
int degree[MAX];//存储双连通分量的缩点的度
stack<int> s;
int n,m,cnt,k,scc;//scc为双连通分量的个数
struct Edge
{
int to,next;
Edge(){}
Edge(int x,int y):to(x),next(y){}
}edge[10*MAX];
{
edge[k].to=cv;
}
void tarjan(int u,int fa)
{
low[u]=dfn[u]=++cnt;
s.push(u);
is_stack[u]=1;
{
int v=edge[i].to;
if(i==(fa^1))
continue;
if(!dfn[v])
{
tarjan(v,i);
low[u]=min(low[u],low[v]);
}
else if(is_stack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
scc++;
while(1)
{
int v=s.top();
s.pop();
is_stack[v]=0;
belong[v]=scc;
if(v==u)
break;
}
}
}
int main()
{
while(cin>>n)
{
cin>>m;
k=cnt=scc=0;
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(is_stack,0,sizeof(is_stack));
memset(degree,0,sizeof(degree));
while(!s.empty())
s.pop();
for(int i=0;i<=m-1;i++)
{
int u,v;
scanf("%d%d",&u,&v);
}
for(int i=1;i<=n;i++)//点1~n
if(!dfn[i])
tarjan(1,-1);
//得到了belong数组的具体信息
// for(int i=1;i<=n;i++)
//   cout<<belong[i]<<" ";
for(int i=1;i<=n;i++)
{
{
int v=edge[j].to;
if(belong[i]!=belong[v])
degree[belong[i]]++;
}
}
int sum=0;
for(int i=1;i<=n;i++)
if(degree[i]==1)
sum+=1;
cout<<(sum+1)/2<<endl;
}
return 0;
}



个人资料
等级：
访问量： 11万+
积分： 3942
排名： 9702
赞助一下
如果您觉得我的文章对您有帮助的话，不妨小额赞助一下，激励我写出更多的好文章，谢谢大家！对于支持我的朋友们，我会根据情况随机选择出幸运用户，送出关于python，深度学习和tensorflow相关的书籍~~

以下是我的支付宝和微信账户