POJ2117割点连通分量
Posted hesorchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2117割点连通分量相关的知识,希望对你有一定的参考价值。
题目
答案为 连通分量数量+ MAX(tot-isroot)
代码
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <map>
using namespace std;
const int N = 100005;
vector<int> edge[N];
int dfn[N], low[N], ct, res;
void tarjan(int u, bool isroot)
{
dfn[u] = low[u] = ++ct;
int tot = 0; //删除该点后的连通分量数量即为tot-isroot
for (int i = 0; i < edge[u].size(); i++)
{
int v = edge[u][i];
if (!dfn[v])
{
tarjan(v, 0);
low[u] = min(low[u], low[v]);
tot += low[v] >= dfn[u];
}
else
low[u] = min(low[u], dfn[v]);
}
res = max(res, tot - isroot);
}
int main()
{
int n, m;
while (cin >> n >> m)
{
if (n == 0 && m == 0)
return 0;
for (int i = 0; i <= n; i++)
edge[i].clear();
memset(dfn, 0, sizeof dfn);
memset(low, 0, sizeof low);
ct = 0;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
++u, ++v;
edge[u].push_back(v);
edge[v].push_back(u);
}
int ans = 0, maxx = 0;
for (int i = 1; i <= n; i++)
{
if (!dfn[i])
{
ans++;
res = 0;
tarjan(i, 1);
maxx = max(maxx, res);
}
}
ans += maxx;
if (ans == n)
ans--;
cout << ans << endl;
}
return 0;
}
以上是关于POJ2117割点连通分量的主要内容,如果未能解决你的问题,请参考以下文章
poj 2117 Electricity(tarjan求割点删掉之后的连通块数)
Road Construction POJ - 3352 边双连通分量