hdu 4587(一个图删除两个点后的最大联通分支数)

TWO NODES

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


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
题意: 给你一个图删除两个点后求最大联通分支个数。

思路: 暴力  删除每一个点,然后求割点及删除这个点后的联通分支的变化量。

代码: 

#include<bits/stdc++.h>

using namespace std;
const int N=5005;
const int M=10005;

struct Edge
{
    int to;
    int next;
    bool cut;
}edge[M];

int head[N],tot;
int low[N],dfn[N],Stack[N];
bool instack[N];
int index,top;
bool cut[N];
int sz[N];
int add_block[N];
int bridge;
int aim;

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

void tarjan(int u,int pre)
{
    int v;
    low[u]=dfn[u]=++index;
    Stack[top++]=u;
    instack[u]=1;
    int son=0;

    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(v==pre) continue;
        if(v==aim) continue;
        if(!dfn[v])
        {
            son++;
            tarjan(v,u);
            if(low[u]>low[v]) low[u]=low[v];

            if(low[v]>dfn[u])
            {
                bridge++;
                edge[i].cut=1;
                edge[i^1].cut=1;
            }

            if(u!=pre&&low[v]>=dfn[u]){
                cut[u]=1;
                add_block[u]++;
            }

        }
        else if(low[u]>dfn[v]){
            low[u]=dfn[v];
        }
    }
    if(u==pre&&son>1) cut[u]=1;
    if(u==pre) add_block[u]=son-1;
    instack[u]=0;
    top--;

}


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

void init2()
{
    memset(dfn,0,sizeof(dfn));
    memset(instack,0,sizeof(instack));
    memset(add_block,0,sizeof(add_block));
    memset(cut,0,sizeof(cut));
    index=top=0;
}

int n,m;
int u,v;

void solve()
{
    int cnt=0;
    int ans=0;
    for(int i=0;i<n;i++)
    {
        aim=i;
        init2();
        cnt=0;
        for(int j=0;j<n;j++)
        {
            if(i==j) continue;
            if(!dfn[j]){
                tarjan(j,j);
                cnt++;
            }
        }

        //cout<<"i : "<<i<<" cnt "<<cnt<<endl;
        for(int j=0;j<n;j++)
        {
            if(i==j) continue;
            ans=max(ans,cnt+add_block[j]);
        }
    }
    cout<<ans<<endl;
    return ;
}

int main()
{
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        init1();
        for(int i=1;i<=m;i++){
            scanf("%d %d",&u,&v);
            add(u,v);
            add(v,u);
        }

        solve();

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/80516274