POJ 2117 Electricity(无向图割点) && HDU 4587 TWO NODES

36 篇文章 4 订阅


Electricity
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5676 Accepted: 1854

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

Source


这位大神说的很好

题意:

        给你一个无向图(不一定连通),现在问你从该图中删除任意一个顶点之后,该无向图所具有的连通分量(联通块)数目最大是多少?

分析:

        本题与前一题不一样,前一题是要你判定哪些点是割点。但是这题需要你求出割点到底能割出几个连通分量。本题的无向图可能不连通。我们只需要考虑在同一个连通分量时,一个割点到底能把图割成几部分即可。

        在dfs的时候,我们用cut[i]==X表示在dfs树中当i节点被删除时,i节点的X个儿子被切割开来(可以认为cut[i]是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]=0,表示i是孤立的一点,此时cut[i]-1=-1.

        如果原始cut[i]=1,表示i为根且有一个儿子,此时cut[i]-1=0.

        如果原始cut[i]>=2,表示i为根且分割了>=2个儿子,此时cut[i]-1>=1.

        (以上含义需仔细体会验证)

其实就是如果不是根节点的话, 他增加儿子个联通分量, 因为他的爸爸等于去掉的她自己了, 如果是根节点的话, 那就只能再拿个儿子顶替自己。。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 5e4 + 5;
int n, m, low[maxn], dfn[maxn], id[maxn], scc_cnt, dfs_cnt;
int cut[maxn];
vector<int> v[maxn];
void init()
{
    memset(low, 0, sizeof(low));
    memset(id, 0, sizeof(id));
    memset(dfn, 0, sizeof(dfn));
    memset(cut, 0, sizeof(cut));
    scc_cnt = dfs_cnt = 0;
    for(int i = 0; i < maxn; i++)
        v[i].clear();
}
void tarjan(int x, int f)
{
    int son = 0;
    dfn[x] = low[x] = ++dfs_cnt;
    for(int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        if(!dfn[to])
        {
            son++;
            tarjan(to, x);
            low[x] = min(low[x], low[to]);
            if(low[to] >= dfn[x])
                cut[x]++;
        }
        else if(dfn[to] < dfn[x] && to != f)
            low[x] = min(low[x], dfn[to]);
    }
    if(f < 0 && son == 1) //根节点 只有一个儿子不是割点,割点不会多余
        cut[x] = 0;
    else if(f < 0 && son > 1)  //是根节点, 而且儿子大于1, 其实只是多出来 儿子减一个联通块, 少的那一个其实就是顶替了根节点
        cut[x] = son-1;
}
int main()
{
    while(~scanf("%d%d", &n, &m), n+m)
    {
        init();
        int x, y;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d", &x, &y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        int cnt = 0, ans = 0;
        for(int i = 0; i < n; i++)
            if(!dfn[i])
                tarjan(i, -1), cnt++;  //求有几个联通块 也就是联通分量
        for(int i = 0; i < n; i++)
        {
            if(ans < cut[i]+cnt)
                ans = cut[i]+cnt;
        }
        if(!m)
            printf("%d\n", n-1);
        else
            printf("%d\n", ans);
    }
    return 0;
}
/*
7 9
0 1
1 2
0 2
0 3
0 4
3 4
0 5
0 6
5 6

5 6
0 1
0 2
0 3
0 4
1 2
3 4

7 8
0 1
0 2
0 3
0 4
1 2
3 4
3 5
3 6
*/


题目大意:给定一个图,去掉其中哪两点后能得到最多的连通块,是多少 
解题思路:枚举去掉第一个点,然后利用求割点的模板就可以求出去掉另一点后的答案,取最多即可 ,应该根据上一题改编 的吧

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 5e4 + 5;
int n, m, low[maxn], dfn[maxn], id[maxn], scc_cnt, dfs_cnt;
int cut[maxn];
vector<int> v[maxn];
void init()
{
    memset(low, 0, sizeof(low));
    memset(id, 0, sizeof(id));
    memset(dfn, 0, sizeof(dfn));
    memset(cut, 0, sizeof(cut));
    scc_cnt = dfs_cnt = 0;
}
void tarjan(int x, int f, int bad)
{
    int son = 0;
    dfn[x] = low[x] = ++dfs_cnt;
    for(int i = 0; i < v[x].size(); i++)
    {
        int to = v[x][i];
        if(to == bad) continue;
        if(!dfn[to])
        {
            son++;
            tarjan(to, x, bad);
            low[x] = min(low[x], low[to]);
            if(low[to] >= dfn[x])
                cut[x]++;
        }
        else if(dfn[to] < dfn[x] && to != f)
            low[x] = min(low[x], dfn[to]);
    }
    if(f < 0 && son == 1) //根节点 只有一个儿子不是割点,割点不会多余
        cut[x] = 0;
    else if(f < 0 && son > 1)  //是根节点, 而且儿子大于1, 其实只是多出来 儿子减一个联通块, 少的那一个其实就是顶替了根节点
        cut[x] = son-1;
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 0; i < maxn; i++)
            v[i].clear();
        int x, y;
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d", &x, &y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        int ans = 0, cnt = 0;
        for(int bad = 0; bad < n; bad++)
        {
            init();
            cnt = 0;
            for(int i = 0; i < n; i++)
            {
                if(bad == i) continue;
                if(!dfn[i])
                    tarjan(i, -1, bad), cnt++;  //求有几个联通块 也就是联通分量
            }
            for(int i = 0; i < n; i++)
            {
                if(bad == i) continue;
                ans = max(ans, cut[i]+cnt);
            }
        }
        if(!m)
            printf("%d\n", n-2);
        else
            printf("%d\n", ans);
    }
    return 0;
}
/*
7 9
0 1
1 2
0 2
0 3
0 4
3 4
0 5
0 6
5 6

5 6
0 1
0 2
0 3
0 4
1 2
3 4

7 8
0 1
0 2
0 3
0 4
1 2
3 4
3 5
3 6
*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值