Electricity
POJ - 2117
Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them supplying a small area that surrounds it. This organization brings a lot of problems - it often happens that there is not enough power in one area, while there is a large surplus in the rest of the country.
ACM++ has therefore decided to connect the networks of some of the plants together. At least in the first stage, there is no need to connect all plants to a single network, but on the other hand it may pay up to create redundant connections on critical places - i.e. the network may contain cycles. Various plans for the connections were proposed, and the complicated phase of evaluation of them has begun.
One of the criteria that has to be taken into account is the reliability of the created network. To evaluate it, we assume that the worst event that can happen is a malfunction in one of the joining points at the power plants, which might cause the network to split into several parts. While each of these parts could still work, each of them would have to cope with the problems, so it is essential to minimize the number of parts into which the network will split due to removal of one of the joining points.
Your task is to write a software that would help evaluating this risk. Your program is given a description of the network, and it should determine the maximum number of non-connected parts from that the network may consist after removal of one of the joining points (not counting the removed joining point itself).
Input
The input consists of several instances.
The first line of each instance contains two integers 1 <= P <= 10 000 and C >= 0 separated by a single space. P is the number of power plants. The power plants have assigned integers between 0 and P - 1. C is the number of connections. The following C lines of the instance describe the connections. Each of the lines contains two integers 0 <= p1, p2 < P separated by a single space, meaning that plants with numbers p1 and p2 are connected. Each connection is described exactly once and there is at most one connection between every two plants.
The instances follow each other immediately, without any separator. The input is terminated by a line containing two zeros.
Output
The output consists of several lines. The i-th line of the output corresponds to the i-th input instance. Each line of the output consists of a single integer C. C is the maximum number of the connected parts of the network that can be obtained by removing one of the joining points at power plants in the instance.
Sample Input
3 3
0 1
0 2
2 1
4 2
0 1
2 3
3 1
1 0
0 0
Sample Output
1
2
2
题意:求去除一点后,形成的连通分支数的最大值。无向图可能不连通(使最多的网络不能跟原路线相连)
分析:
饶齐大佬写得好,本人增添了自己的理解。
这是割点的定义,我们dfs求割点u的时候,其实是把u的每一个子结点都进行判断,满足一个,我们就可以设一个cut[u]++,即可,注意说的定义,建议模拟一下,注意这里的cnt[i]是增加了多少连通分支)
注意:如果i是根且其儿子只有1个,虽然i不是割点,cut[i]依然=1。如果i节点非割点,那么cut[i]=0。如果i是割点,那么cut[i]就是i被删除后将割出来的儿子数目。
然后我们求出了每个点的cut[i]值,即i点被删除,会有cut[i]个儿子树被割出来。
如果i是dfs树的非根节点,那么cut[i]== 切除i之后增加的连通分量数目。
如果i是dfs树的根节点,那么cut[i]-1才是切除i之后增加的连通分量数目(其实这是一个无向图,谁都可以当根,所以一定cut[i]-1)。
如果原始cut[i]=0,表示i是孤立的一点,此时cut[i]-1=-1.
如果原始cut[i]=1,表示i为根且有一个儿子,此时cut[i]-1=0.
如果原始cut[i]>=2,表示i为根且分割了>=2个儿子,此时cut[i]-1>=1.
(以上含义需仔细体会验证)
本题的无向图可能不连通。我们只需要考虑在同一个连通分量时,一个割点到底能把图割成几部分即可(原本连通分量好求,代码一个for循环就解决了)。
#include<cstdio>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<iomanip>
#include<numeric>
#include<cctype>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
using namespace std;
const int maxn=10000+10;
int n,m;
vector<int> G[maxn];
int cut[maxn];
int low[maxn];
int pre[maxn];
int dfs_clock;
int tarjan(int u,int fa)
{
int lowu= pre[u]=++dfs_clock;
// cout<<u<<" "<<pre[u]<<endl;
for(int i=0;i<G[u].size();i++)
{
int v=G[u][i];
if(!pre[v])
{
int lowv=tarjan(v,u);
lowu=min(lowv,lowu);
//cout<<v<<"**"<<lowv<<endl;
if(lowv>=pre[u])
{
cut[u]++;
//cout<<u<<" "<<cut[u]<<endl;
}
}
else if(pre[v]<pre[u] && v!=fa)
lowu = min(lowu,pre[v]);
}
return low[u]=lowu;
}
int main()
{
while(scanf("%d%d",&n,&m)==2&&n)
{
dfs_clock=0;
memset(cut,0,sizeof(cut));
memset(pre,0,sizeof(pre));
for(int i=0;i<n;i++) G[i].clear();
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
int sum=0;//计数原本连通分量数目
int max_cut=-10000;
for(int i=0;i<n;i++)
if(!pre[i])
{
sum++;
tarjan(i,-1);
//cout<<i<<" "<<cut[i]<<endl;
cut[i]--;
}
for(int i=0;i<n;i++)
max_cut=max(max_cut,cut[i]);
printf("%d\n",sum+max_cut);
}
return 0;
}