Hdu 4587 TWO NODES(割点)

来源:互联网 发布:佳都新太科技软件测试 编辑:程序博客网 时间:2024/05/12 14:52

题目链接

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 ofstab 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 ofstab.
 

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 ofstab.
 

Sample Input
4 50 11 22 33 00 2
 

Sample Output
2
 

Source
2013 ACM-ICPC南京赛区全国邀请赛——题目重现 

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

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

详情见代码:

#include<cstdio>#include<iostream>#include<algorithm>#include<string.h>#define nn 5100using 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;}


0 0