Electricity
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 4597 |
|
Accepted: 1515 |
Description
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
题意:给你一个N个点和M条边的无向图,让你求处去掉一个点后图中最多的BCC数目。
思路:由于图可能存在孤立点,所以我们先要求出原图 分成了几块,然后就是tarjan求出去掉割点增加的BCC,最后找最大的就行了。
今天阿欢告诉我,我的无向图点连通模板有点问题。研究一下午,终于KO了。
无向图割点有关 模板 : 点我
AC代码:
#include
#include
#include
#include
#include
#define MAXN 10000+10
#define MAXM 200000+10
using namespace std;
struct Edge
{
int from, to, next;
}edge[MAXM];
int head[MAXN], edgenum;//存储指针
int add_bcc[MAXN];//去掉该点增加的bcc数目
int dfn[MAXN];//该点的深度优先数
int low[MAXN];//从该点或它的子孙出发 通过回边可以到达的最低深度优先数
bool iscut[MAXN];//该点是否为割点
int dfs_clock;//时间戳
int num;//图可能存在 独立点 需要先计算出图中的块数
void init()
{
edgenum = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v)
{
Edge E1 = {u, v, head[u]};
edge[edgenum] = E1;
head[u] = edgenum++;
Edge E2 = {v, u, head[v]};//无向图需要反向建边
edge[edgenum] = E2;
head[v] = edgenum++;
}
void getMap(int m)
{
int a, b;
while(m--)
{
scanf("%d%d", &a, &b);
a++, b++;
addEdge(a, b);
}
}
void tarjan(int u, int fa)//u在DFS树中的父节点是fa
{
low[u] = dfn[u] = ++dfs_clock;
int child = 0;//记录子节点数目
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
int v = E.to;
if(!dfn[v])
{
child++;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u])//割点 先不考虑根节点 最后再考虑
{
iscut[u] = true;
add_bcc[u]++;//增加一个BCC
}
}
else if(dfn[v] < dfn[u] && v != fa)
low[u] = min(low[u], dfn[v]);//反向边更新
}
//对根节点进行再次判断
if(fa < 0 && child < 2) iscut[u] = false, add_bcc[u] = 0;//根节点不是割点
if(fa < 0 && child > 1) iscut[u] = true, add_bcc[u] = child - 1;//根节点是割点 更新add_bcc的值
}
void find_cut(int l, int r)
{
memset(add_bcc, 0, sizeof(add_bcc));
memset(iscut, 0, sizeof(iscut));
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
dfs_clock = num = 0;
for(int i = l; i <= r; i++)
if(!dfn[i]) tarjan(i, -1), num++;// 计算 图分成多少块
}
void solve(int l, int r)
{
int ans = 0;
for(int i = l; i <= r; i++)
ans = max(ans, add_bcc[i]);
printf("%d\n", ans + num);
}
int main()
{
int N, M;
while(scanf("%d%d", &N, &M), N||M)
{
if(M == 0)
{
printf("%d\n", N-1);
continue;
}
init();
getMap(M);
find_cut(1, N);//找割点 和 BCC
solve(1, N);
}
return 0;
}