HDU4587 TWO NODES

TWO NODES

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


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

题意

一张图,要求去掉两个点之后连通分量最多,求这个最大值。

题解

直接暴力枚举第一个去掉的点,在剩下的图上跑tarjan算法就好(代码比较丑。。内存突破天际。。时间突破天际。。我还是太弱了。。。)

代码

#include <cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int E[5005][5005];
int Enum[5005];
int dfn[5005];
int low[5005];
int son[5005];
int N,M;
int u,v;
int T;
int del;
int root;
void dfs(int x,int fa)
{
    low[x]=dfn[x]=T++;
    int cnt=0;
    for(int i=0;i<Enum[x];i++)
    {
        if(E[x][i]==fa||E[x][i]==del)
            continue;
        else if(dfn[E[x][i]])
            low[x]=min(low[x],dfn[E[x][i]]);
        else if(!dfn[E[x][i]])
        {
            dfs(E[x][i],x);
            cnt++;
            if(x!=root&&low[E[x][i]]>=dfn[x])
                son[x]++;
            low[x]=min(low[x],low[E[x][i]]);
        }
    }
    if(x==root)
        son[x]=cnt-1;
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        int ans=0;
        memset(Enum,0,sizeof Enum);
        for(int i=0;i<M;i++)
        {
            scanf("%d%d",&u,&v);
            E[u][Enum[u]++]=v;
            E[v][Enum[v]++]=u;
        }
        for(del=0;del<N;del++)
        {
            T=1;
            int temp=0;
            memset(dfn,0,sizeof dfn);
            memset(low,0,sizeof low);
            memset(son,0,sizeof son);
            for(int i=0;i<N;i++)
            {
                if(i!=del&&dfn[i]==0)
                {
                    root=i;
                    dfs(i,-1);
                    temp++;
                }
            }
            for(int i=0;i<N;i++)
            {
                if(i==del)
                    continue;
                ans=max(ans,temp+son[i]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值