HDU 4587 TWO NODES 无向图的割点

题目描述:

Description
Suppose that G is an undirected graph, and the value of stab is defined as follows:

这里写图片描述
Among the expression,G -i, -j is the remainder after removing node i, node j and all edges that are directly relevant to the previous two nodes. cntCompent is the number of connected components of X independently.
Thus, given a certain undirected graph G, you are supposed to calculating the value of stab.

Input
The input will contain the description of several graphs. For each graph, the description consist of an integer N for the number of nodes, an integer M for the number of edges, and M pairs of integers for edges (3<=N,M<=5000).
Please note that the endpoints of edge is marked in the range of [0,N-1], and input cases ends with EOF.

Output
For each graph in the input, you should output the value of stab.

Sample Input

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

Sample Output

2 

题目分析:

n个点,m条边组成的无向图,去掉两个点,求最多的连通分支数。
用Tarjan所有的点跑一边,求出最大割点就行了。
用的是kuangbin的模板。

代码如下:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>


using namespace std;
const int MAXN=5010;
const int MAXM=50010;

struct EDGE
{
    int to,next;
}edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],stack[MAXN];
int index,top;
bool instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];

void init()
{
    memset(head,-1,sizeof(head));
    tot=0;
}

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}


void tarjan(int u,int pre,int subindex)
{
    int v;
    low[u]=dfn[u]=++index;
    stack[top++]=u;
    instack[u]=true;
    int son=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        v=edge[i].to;
        if (v==pre) continue;
        if (v==subindex) continue;
        if (!dfn[v])
        {
            son++;
            tarjan(v,u,subindex);
            low[u]=min(low[u],low[v]);
            if (u!=pre && low[v]>=dfn[u])
            {
                cut[u]=true;
                add_block[u]++;
            }
        }
        else low[u]=min(low[u],dfn[v]);
    }
    if (u==pre && son>1) cut[u]=true;
    if (u==pre) add_block[u]=son-1;
    instack[u]=false;
    top--;
}

int solve(int n,int subindex)
{
    memset(dfn,0,sizeof(dfn));
    memset(instack,false,sizeof(instack));
    memset(add_block,0,sizeof(add_block));
    memset(cut,false,sizeof(cut));
    index=top=0;
    int cnt=0;
    int ans=0;
    for(int i=0; i<n; i++)
        if (!dfn[i] && i!=subindex)
        {
            tarjan(i,i,subindex);
            cnt++;
        }
    for(int i=0; i<n; i++)
    {
        if (i!=subindex)
        {
            ans=max(ans,cnt+add_block[i]);
        }
    }
    return ans;
}
int n,m;
int main()
{
    while(scanf("%d%d",&n,&m)!=-1 && (n||m))
    {
        init();
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            addedge(u,v);
            addedge(v,u);
        }
        int ans=0;
        for(int i=0; i<n; i++)
        {
            ans=max(ans,solve(n,i));
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值