HDU 4587 (割点)

来源:互联网 发布:程序员怎么挣钱 编辑:程序博客网 时间:2024/06/05 17:43

TWO NODES

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


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 50 11 22 33 00 2
 

Sample Output
2
 


题意:求一个图去掉两个点之后的最多的联通分量数.

首先枚举一个去掉的点,然后求出所有的割点,对于所有的割点,他能够遍历到的点并

且没有访问过的点数就是删去他能够增加的联通分量数.

坑点:

注意树根和孤立节点的情况.

#include <iostream>#include <algorithm>#include <string.h>#include <stdio.h>#include <map>#include <vector>#include <stack>#include <queue>#include <math.h>#include <stdlib.h>using namespace std;const int maxn = 5111;const int maxm = 11111;typedef long long ll;int n, m;struct node {    int u, v, next;}edge[maxm];int head[maxn], cnt;void add_edge (int u, int v) {    edge[cnt].u = u, edge[cnt].v = v, edge[cnt].next = head[u], head[u] = cnt++;}int dfs_clock, pre[maxn], low[maxn], cur, ans;bool is_cut[maxn];int dfs (int u, int fa, int del) {    int lowu = pre[u] = ++dfs_clock;    int child = 0, curnum = 0;    for (int i = head[u]; i != -1; i = edge[i].next) {        int v = edge[i].v;        if (v == del)//删掉的点            continue;        if (!pre[v]) {            child++;            int lowv = dfs (v, u, del);            lowu = min (lowu, lowv);            if (lowv >= pre[u]) {                is_cut[u] = 1;                curnum++;            }        }        else if (pre[v] < pre[u] && v != fa) {            lowu = min (lowu, pre[v]);        }    }    if (fa < 0 && child == 1) {        is_cut[u] = 0;    }    if (child == 0 && fa < 0) {        ans = max (ans, cur-1);    }    if (is_cut[u])        ans = max (ans, cur+curnum-(fa<0));    low[u] = lowu;    return lowu;}int cc = 0;bool vis[maxn];void go (int u, int fa, int del) {    cc++;    vis[u] = 1;    for (int i = head[u]; i != -1; i = edge[i].next) {        int v = edge[i].v;        if (v == fa || vis[v] || v == del)            continue;        go (v, u, del);    }}int main () {    //freopen ("in.txt", "r", stdin);    while (scanf ("%d%d", &n, &m) == 2) {        memset (head, -1, sizeof head);        cnt = 0;        for (int i = 1; i <= m; i++) {            int u, v;            scanf ("%d%d", &u, &v);            add_edge (u, v);            add_edge (v, u);        }        ans = 0;        for (int i = 0; i < n; i++) {//枚举删掉的点            dfs_clock = 0;            memset (is_cut, 0, sizeof is_cut);            memset (pre, 0, sizeof pre);            int u;            cur = 0;            memset (vis, 0, sizeof vis);            bool ok = 0;            for (u = 0; u < n; u++) if (!vis[u] && u != i) {                cc = 0;                go (u, -1, i);                cur++;                if (cc > 1)                    ok = 1;            }            if (cur == n-1) {                ans = max (ans, n-2);                continue;            }            if (ok) {                ans = max (ans, cur);            }            for (u = 0; u < n; u++) if (!pre[u] && u != i) {                dfs (u, -1, i);//找到所有的割点            }        }        printf ("%d\n", ans);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族手机进水了开不了机怎么办 打发票机屏忘记了密码怎么办 魅族手机升级显示电量不足怎么办 魅蓝手机的图库没了怎么办 魅蓝5s屏幕碎了怎么办 魅族手机中间的home键失灵怎么办 魅族手机突然关机开不了机了怎么办 魅蓝3s卡顿怎么办视频 魅蓝3s手机太卡怎么办 红米4x外放破音怎么办 魅族手机刷机后内存变小了怎么办 手机用久了内存越来越小怎么办魅族 贴了车膜左右后视镜反光怎么办 贴手机膜时酒精没了怎么办 魅蓝e外屏幕碎了怎么办 贴的手机膜边缘翘起来着怎么办 手机后面贴的膜都是胶怎么办 车漆外面一层保护膜被划开了怎么办 美团骑手gps信号弱时怎么办 华为荣耀4x充电插口坏了怎么办 华为畅享5s开不了机怎么办 华为梅特10忘记账号密码怎么办 华为荣耀8充电接口坏了怎么办 华为手机锁屏了自动开屏怎么办 支付宝收货地址写错了怎么办 支付宝领海报地址写错了怎么办 手机总是弹出日历已停止运行怎么办 华为mate8忘了锁屏密码怎么办 红米4a打王者卡怎么办 苹果5s软件更新密码忘记了怎么办 华为荣耀P9进水了没有声音怎么办 华为荣耀7原相机不能拍照怎么办 华为手机触屏密码忘记了怎么办 华为荣耀10屏锁密码忘了怎么办 华为手机开机卡在开机界面怎么办 苹果6s国行不能用移动4g怎么办 苹果手机32g内存不够用怎么办 华为荣耀6x忘记了密码怎么办 百度粉色衣服被洗变色了怎么办 粉色衣服放进洗衣机洗变色了怎么办 红米note5a应用锁忘了怎么办