Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11047 | Accepted: 4725 |
Description
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
Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.
Output
Sample Input
7 7
1 2
2 3
3 4
2 5
4 5
5 6
5 7
Sample Output
2
Hint
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<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;
}