1. 程式人生 > >hdu 4587 TWO NODES(tarjan演算法 刪兩個點求最多剩餘聯通分量)

hdu 4587 TWO NODES(tarjan演算法 刪兩個點求最多剩餘聯通分量)

TWO NODES

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


 

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

2013 ACM-ICPC南京賽區全國邀請賽——題目重現

 

 

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  6460 6459 6458 6457 6456 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn 5005
int pre[maxn];
int low[maxn];
vector<int>g[maxn];
int cut[maxn];
int dfsclock;
int tarjan(int u,int fa,int forbid)
{
    int lowu=pre[u]=++dfsclock;
    int child=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(v==fa||v==forbid)
            continue;
        if(!pre[v])
        {
            child++;
            int lowv=tarjan(v,u,forbid);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
            {
                cut[u]++;
            }
        }
        else
            lowu=min(lowu,pre[v]);
    }
    if(fa<0)
        cut[u]--;
    return low[u]=lowu;
}
int n,m;
int main()
{while(~scanf("%d%d",&n,&m))
{
    for(int i=0;i<n;i++)
        g[i].clear();
        for(int i=0;i<m;i++)


        {int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);

        }
        int ans=-100;
        for(int u=0;u<n;u++)
        {
            int sum=0;
            dfsclock=0;
            memset(pre,0,sizeof(pre));
            memset(cut,0,sizeof(cut));
            for(int v=0;v<n;v++)
                if(v!=u&&!pre[v])
            {
                sum++;
            tarjan(v,-1,u);
            }
            for(int v=0;v<n;v++)
                if(v!=u)
                ans=max(ans,sum+cut[v]);
        }
        printf("%d\n",ans);

}
return 0;

}