HDU4587 [TWO NODES] tarjan求无向图割顶

来源:互联网 发布:js获取自定义属性 编辑:程序博客网 时间:2024/06/06 11:38

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


题意:求删去无向图中两个节点后连通块的最大数量

solution:枚举第一个点,tarjan判第二个点。

#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int N = 5e3 + 7;struct Edge{    int nxt, to;}e[N<<1];int head[N], tot=0;void addeage(int u, int v){    e[++tot].nxt=head[u]; e[tot].to=v;    head[u]=tot;} int dfn[N], low[N], iscut[N], dnt=0, root;void dfs(int u, int fa, int del){    dfn[u]=low[u]=++dnt;    for ( int i=head[u]; i!=-1; i=e[i].nxt ){        int v=e[i].to;        if(  v==del || v==fa ) continue;        if( !dfn[v] ){            dfs(v, u, del); // 不访问del节点             if( dfn[u]<=low[v] ) iscut[u]++;              // 统计删去当前点后增加的连通块的个数             low[u]=min(low[u], low[v]);        } else {            low[u]=min(low[u], dfn[v]);        }    }}int n, m;int solve(){    int ret=0;    for ( int i=1; i<=n; i++ ){        for ( int j=1; j<=n; j++ ) dfn[j]=low[j]=0;        for ( int j=1; j<=n; j++ ) iscut[j]=1;        dnt=0;        int ans=0;        for ( int j=1; j<=n; j++ )             if( !dfn[j] && i!=j ){                iscut[j]=0;     //给根赋初值                 ans++;                dfs(j, 0, i);            }        for ( int j=1; j<=n; j++ )            if( i!=j ){                ret=max(ret,ans+iscut[j]-1);            }    }    return ret;} int main(){    while( ~scanf("%d%d", &n, &m ) ){        tot=0;        memset(head,-1,sizeof(head));        for ( int i=1; i<=m; i++ ){            int x, y;            scanf("%d%d", &x, &y );            addeage(x+1,y+1);            addeage(y+1,x+1);        }        printf("%d\n", solve() );    } }
原创粉丝点击