HDU 4587 TWO NODES 枚举+割点

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4587

TWO NODES

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

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

题意

给你个图,问你去掉两个点之后能有最多多少连通块。

题解

先枚举其中一个点,然后在剩下的点中求割点,Tarjan的时候统计一下每个割点分割几个连通块,取个最大的割点,然后再dfs一次求连通块个数。

代码

#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#define MAX_N 5555
using namespace std;

vector<int> G[MAX_N];
bool vis[MAX_N];
int dfn[MAX_N],low[MAX_N],ind=0;

int cut[MAX_N];

int node;

void Tarjan(int u,int p){
    int child=0;
    dfn[u]=low[u]=++ind;
    vis[u]=1;
    for(int i=0;i<G[u].size();i++){
        int v=G[u][i];
        if(v==p||v==node)continue;
        if(!vis[v]){
            Tarjan(v,u);
            low[u]=min(low[v],low[u]);
            child++;
            if((p==-1&&child>1)||(p!=-1&&low[v]>=dfn[u]))
                cut[u]++;
        }
        else
            low[u]=min(dfn[v],low[u]);
    }
}

int n,m;

void init(){
    for(int i=0;i<=n;i++)G[i].clear();
    ind=0;
    memset(vis,0,sizeof(vis));
    memset(cut,0,sizeof(cut));
}

bool used[MAX_N];
int cu;
void dfs(int u,int p){
    if(u==p||used[u]||u==node||u==cu)return;
    used[u]=1;
    for(int i=0;i<G[u].size();i++)dfs(G[u][i],u);
}

int main(){
    while(scanf("%d%d",&n,&m)==2){
        int stab=1;
        init();
        int u,v;
        for(int i=0;i<m;i++) {
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        for(int i=0;i<n;i++){
            node=i;
            memset(vis,0,sizeof(vis));
            ind=0;
            memset(cut,0,sizeof(cut));
            for(int j=0;j<n;j++)
                if((!vis[j])&&j!=node)
                    Tarjan(j,-1);
            int maxC=0;
            for(int j=0;j<n;j++)
                if(j!=node&&cut[j]>=maxC){
                    cu=j;
                    maxC=cut[j];
                }
            int ans=0;
            memset(used,0,sizeof(used));
            for(int j=0;j<n;j++)
                if((!used[j])&&j!=node&&j!=cu){
                    dfs(j,-1);
                    ans++;
                }
            stab=max(stab,ans);
        }
        printf("%d\n",stab);
    }

    return 0;
}
时间: 2024-12-25 18:13:08

HDU 4587 TWO NODES 枚举+割点的相关文章

hdu 4587 判断孤立点+割点+ 删除点之后,剩下多少连通分量

做了很久...... 题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4587 先枚举删除的第一个点,第二个点就是找割点,没有割点当然也有答案 学到的: 1.图论硬套模板不太现实,比如这道题,我能想到孤立点是特殊情况,删除孤立点,连通分支个数会减少一,但是一直处理不好,最后按缩点的做法搞了, 判断是不是孤立点的方法: 就是先用一个数组scnt[i]=j,vv[j]++  表示点i在以j为祖先的联通分支里,而且每次都让vv[j]++,就使得vv[j

HDU 4587 TWO NODES 割点

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意: 删除两个点,使连通块的数目最大化 题解: 枚举删除第一个点,然后对删除了第一个点的图跑割点更新答案. 代码: #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> using namespace std; c

HDU 4587 TWO NODES(割两个点的最大连通分支数)

http://acm.hdu.edu.cn/showproblem.php?pid=4587 题意: 给一图,求割去两个点后所能形成的最大连通分支数. 思路: 对于这种情况,第一个只能枚举,然后在删除第一个点的前提下,用Tarjan算法求第二个割点的情况. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstrea

hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。

题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有的连通分量只有一个点,当舍去该点时候,连通分量-1: 复习求割点的好题! #include<iostream> #include<cstdio> #include<vector> using namespace std; int n,m; vector<vector&

HDU 3709 Balanced Number 枚举+数位DP

枚举支点之后数位DP,注意姿势 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list&g

HDOJ 题目4587 TWO NODES(双联通,割点,枚举)

TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1367    Accepted Submission(s): 410 Problem Description Suppose that G is an undirected graph, and the value of stab is defined as fo

HDU 4587 无向图的割点

TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1137    Accepted Submission(s): 333 Problem Description Suppose that G is an undirected graph, and the value of stab is defined as fol

hdu 4587(枚举+割顶)

TWO NODES Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 2354    Accepted Submission(s): 780 Problem Description Suppose that G is an undirected graph, and the value of stab is defined as fol

【HDOJ】4587 TWO NODES

Tarjan解无向图的割点和桥,参考白书. 1 /* 4587 */ 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 #include <cstdio> 6 #include <cstring> 7 #include <cstdlib> 8 using namespace std; 9 10 #define MAXN 5005 11 12 vecto