| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9118 | Accepted: 3925 |
Description
Input
Output
Sample Input
7 7 1 2 2 3 3 4 2 5 4 5 5 6 5 7
Sample Output
2
Hint
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: Source
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
using namespace std;
vector<int>map[5005];
int low[5005],dfn[5005],stack[5005],sn,deep,k,node[5005],vist[5005];
int MIN(int a,int b){ return a>b?b:a; }
void DFS(int p,int fath)
{
int u,m;
stack[++sn]=p; vist[p]=1;
deep++; low[p]=dfn[p]=deep;
m=map[p].size();
for(int j=0;j<m;j++)
{
u=map[p][j];
if(fath==u)continue;
if(vist[u]==0)
{
DFS(u,p);
low[p]=MIN(low[p],low[u]);
}
else if(vist[u]==1)
low[p]=MIN(low[p],dfn[u]);
}
if(low[p]==dfn[p])
{
k++;
while(stack[sn]!=p){
node[stack[sn]]=k; vist[stack[sn--]]=2;
}
node[stack[sn]]=k; vist[stack[sn--]]=2;
}
}
int answer(int n)
{
int d[5005]={0},tj;
sn=deep=k=0;
DFS(1,-1);
for(int i=1;i<=n;i++)
for(int j=0;j<map[i].size();j++)
{
tj=map[i][j];
if(node[i]!=node[tj])
d[node[i]]++;
}
int ans=0;
for(int i=1;i<=k;i++)
if(d[i]==1)
ans++;
ans++;
return ans/2;
}
int vv[5005][5005];
int main()
{
int n,m,a,b;
while(scanf("%d%d",&n,&m)==2)
{
for(int i=1;i<=n;i++)
{
map[i].clear(); vist[i]=0;
for(int j=1;j<=n;j++)
vv[i][j]=0;
}
while(m--)
{
scanf("%d%d",&a,&b);
if(vv[a][b]==0)//去掉重边,以防在计算度时得出错误答案
{
map[a].push_back(b);
map[b].push_back(a);
vv[a][b]=vv[b][a]=1;
}
}
printf("%d\n",answer(n));
}
}
POJ3177Redundant Paths(边的双连通性,用vector时一定要去掉重边),布布扣,bubuko.com
POJ3177Redundant Paths(边的双连通性,用vector时一定要去掉重边)
原文地址:http://blog.csdn.net/u010372095/article/details/38537859