http://poj.org/problem?id=2117

题意:
求删除图中任意一个顶点后的最大连通分量数。

思路:

求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的。

这道题目我wa了很多发,主要是我忘了根结点的连通分量数得减1。

为什么呢?因为如果我们用cut[]来记录每个结点对应的连通分量数的话,最后的答案还需要加1。

比如2结点,我们计算所得的cut[2]=3,因为它只计算了它的子树的情况,但是父亲结点并没有计算进去,所以最后需要+1,这个1也就是父亲结点方向也会产生一个连通分量,这是肯定的,因为父亲结点方向就这么一条边。那根结点是没有父亲结点的,所以根结点需要-1。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
int tot;
int sum;
int ans;
int dfs_clock;
int iscut[maxn];
int pre[maxn];
int head[maxn]; struct node
{
int v;
int next;
}e[*maxn]; void addEdge(int u, int v)
{
e[tot].v=v;
e[tot].next=head[u];
head[u]=tot++;
} int dfs(int u, int fa)
{
int lowu=pre[u]=++dfs_clock;
int child=;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(!pre[v])
{
child++;
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u]) iscut[u]++;
}
else if(pre[v]<pre[u] && v!=fa)
{
lowu=min(lowu,pre[v]);
}
}
if(fa< && child>=) iscut[u]--; //这儿很重要,根结点必须-1
return lowu;
} void solve()
{
sum=;
ans=;
memset(pre,,sizeof(pre));
memset(iscut,,sizeof(iscut));
for(int i=;i<n;i++)
{
if(!pre[i])
{
sum++;
dfs_clock=;
dfs(i,-);
}
ans=max(ans,iscut[i]);
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{
if(n== && m==) break;
if(m==) {printf("%d\n",n-);continue;}
tot=;
memset(head,-,sizeof(head));
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v);
addEdge(v,u);
}
solve();
printf("%d\n",sum+ans);
}
return ;
}

POJ 2117 Electricity(割点求连通分量)的更多相关文章

  1. poj 2117 Electricity(tarjan求割点删掉之后的连通块数)

    题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根 ...

  2. POJ 1523 SPF (割点,连通分量)

    题意:给出一个网络(不一定连通),求所有的割点,以及割点可以切分出多少个连通分量. 思路:很多种情况. (1)如果给的图已经不是连通图,直接“  No SPF nodes”. (2)求所有割点应该不难 ...

  3. POJ 2117 Electricity 双联通分量 割点

    http://poj.org/problem?id=2117 这个妹妹我竟然到现在才见过,我真是太菜了~~~ 求去掉一个点后图中最多有多少个连通块.(原图可以本身就有多个连通块) 首先设点i去掉后它的 ...

  4. poj 2117 Electricity【点双连通求删除点后最多的bcc数】

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4727   Accepted: 1561 Descr ...

  5. POJ—— 2117 Electricity

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5620   Accepted: 1838 Descr ...

  6. poj 2117 Electricity

    /* Tarjan求割点 */ #include<iostream> #include<cstdio> #include<cstring> #include< ...

  7. poj 2117 去掉割点可以分得的联通图的个数模板

    #include<stdio.h> #include<string.h> #define N 11000 /* 去掉一个割点后,询问可以分得的联通图的个数 */ struct ...

  8. Electricity POJ - 2117 + SPF POJ - 1523 去除割点后求强连通分量个数问题

    Electricity POJ - 2117 题目描述 Blackouts and Dark Nights (also known as ACM++) is a company that provid ...

  9. POJ 2117 (割点+连通分量)

    题目链接: http://poj.org/problem?id=2117 题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大. 解题思路: 一个大陷阱,m可以等于0,这 ...

随机推荐

  1. 腾讯云的云数据库MYSQL配置

    腾讯云的云数据库MYSQL配置

  2. MySQL数据库运维课程

    MySQL数据库运维课程 http://www.dataguru.cn/article-4834-1.html?union_site=comm100 课程大纲 第一课:机器选型.系统规划 第二课:安装 ...

  3. [vue]vue条件渲染v-if(template)和自定义指令directives

    条件渲染: v-if/template <div id="app"> <h1>v-show: display: none</h1> <di ...

  4. [LeetCode] 832. Flipping an Image_Easy

    Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resu ...

  5. Postman + newman + jenkins 的API自动化测试应用

    一.环境配置 Postman postman 的具体使用可以参考另外一篇文章:postman 做接口测试之学习笔记 Newman 第一步,安装nodejs. 第二步,在nodejs命令行安装newma ...

  6. 当输入域失去焦点 (blur) 时改变其颜色

    $("input").blur(function(){ $("input").css("background-color","#D ...

  7. mysql主从数据库不同步的2种解决方法 (转载)

    今天发现Mysql的主从数据库没有同步 先上Master库: mysql>show processlist; 查看下进程是否Sleep太多.发现很正常. show master status; ...

  8. Python - matplotlib 数据可视化

    在许多实际问题中,经常要对给出的数据进行可视化,便于观察. 今天专门针对Python中的数据可视化模块--matplotlib这块内容系统的整理,方便查找使用. 本文来自于对<利用python进 ...

  9. UNIX历史

    一.Multics计划 1965年,AT&T贝尔电话实验室.通用电气公司.麻省理工学院MAC课题组一起联合开发一个称为Multics的新操作系统. Multics 系统的目标是要向大的用户团体 ...

  10. source的简单操作

    source五部曲 git工作流:建立新功能 更改文件后,提交 点击git工作流完成新功能 点击推送 点击拉取