Hdu 4587 TWO NODES(割点)

本文介绍TWONODES图论问题的解决方法,通过枚举删除第一个点并求割点的方式,计算无向图中删除两点后最多可分成的连通组件数量。复杂度为O(n^2)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

TWO NODES

Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1178    Accepted Submission(s): 346


Problem 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
 

Source

题意:n点的无向图,问删除两个点以后,该图最多被分成多少个联通快?

题解:枚举删除的第一个点,对于剩下的图如果存在割点,那么删除的那个点一定是割点。同时在求割点的过程中,我们可以处理出删除这个割点可以把原来的联通快分成了几块。那么这个题就可以解决了。复杂度O(n^2)

详情见代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#define nn 5100
using namespace std;
typedef __int64 LL;
int n,m;
struct node
{
    int st,en,next;
}E[nn*2];
int p[nn],num;
void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}
void add(int st,int en)
{
    E[num].en=en;
    E[num].next=p[st];
    p[st]=num++;
}
bool use[nn];
int dfn[nn],low[nn],df;
int dp[nn];
void dfs(int id,int fa,int gen)
{
    dfn[id]=low[id]=++df;
    dp[id]=1;
    int son=0;
    int i,w;
    for(i=p[id];i+1;i=E[i].next)
    {
        w=E[i].en;
        if(use[w]||w==fa)
            continue;
        if(dfn[w]==-1)
        {
            if(id==gen)
                son++;
            dfs(w,id,gen);
            low[id]=min(low[id],low[w]);
            if(low[w]>=dfn[id]&&id!=gen)
            {
                dp[id]++;//统计该割点可以把原来的联通快分成几块
            }
        }
        else
        {
            low[id]=min(low[id],dfn[w]);
        }
    }
    if(id==gen)
    {
        dp[id]=son;//统计根节点可以把原来的联通快分成几块
    }
}
int main()
{
    int i,j;
    int u,v;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        memset(use,false,sizeof(use));
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            u++,v++;
            add(u,v);
            add(v,u);
        }
        int ans=0;
        for(i=1;i<=n;i++)
        {
            use[i]=true;
            memset(dfn,-1,sizeof(dfn));
            df=0;
            int ix=0;
            for(j=1;j<=n;j++)
            {
                if(!use[j]&&dfn[j]==-1)
                {
                    ix++;
                    dfs(j,j,j);
                }
            }
            for(j=1;j<=n;j++)
            {
                if(use[j])
                    continue;
                ans=max(ans,ix-1+dp[j]);
            }
            use[i]=false;
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值