无向图求割点 UVA 315 Network

时间:2022-03-19 08:32:56

输入数据处理正确其余的就是套强联通的模板了

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define maxn 106
#define min(a,b) (a<b?a:b)
/*无向图求桥和割点*/
vector<int> G[maxn];
int Father[maxn], time, dfn[maxn], low[maxn], n;
bool cut[maxn]; void init()
{
memset(Father, , sizeof(Father));
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(cut, false, sizeof(cut));
time = ;
for(int i=; i<=n; i++)
G[i].clear();
}
void tarjan(int u,int father)
{
Father[u] = father;
low[u] = dfn[u] = time ++;
int len = G[u].size(), i, v; for(i=; i<len; i++)
{
v = G[u][i];
if(!low[v])
{
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(v != father)
{
low[u] = min(low[u], dfn[v]);
}
}
}
void solve()
{
int i, RootSons = , v, num = ;
tarjan(, );
for(i=; i<=n; i++)
{
v = Father[i];
if(v == )
RootSons ++;
else if(dfn[v] <= low[i])
cut[v] = true; }
if(RootSons > )
cut[] = true;
for(i=; i<=n; i++)
{
if(cut[i])
num ++;
}
printf("%d\n", num); } int main()
{
int a, b;
char ch;
while(scanf("%d",&n), n)
{
init();
while( scanf("%d",&a), a)
{
while( scanf("%d%c",&b,&ch) )
{
G[a].push_back(b);
G[b].push_back(a);
if(ch == '\n')
break;
}
} solve();
}
return ;
}