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. 版本 ------- 2017年最受开发者欢迎的10个Linux发行版

    1.Arch Linux Arch Linux在安装过程中提供了强大的可定制选择,支持你下载和安装自己所需的程序包.虽然这个选择对新手来说没有多大的帮助,但是它确实能够帮助那些使用Arch构建系统和存 ...

  2. UMI标签学习【转载】

    转自: https://club.1688.com/threadview/50123159.htm 简单介绍一下利用单分子标签(Unique Molecular Identifier,UMI)对残留噪 ...

  3. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  4. keras搭建深度学习模型的一些小tips

    定义模型两种方法:  1.sequential 类仅用于层的线性堆叠,这是目前最常用的网络架构 2.函数式API,用于层组成的有向无环图,让你可以构建任意形式的架构 from keras import ...

  5. JS的增删改查

    1.查 <script type="text/javascript"> /** * 查找 已经在html代码中存在的元素 */ /** * document.getEl ...

  6. React组件,React和生命周期

    笔记,具体可以看看这个博客: https://segmentfault.com/a/1190000004168886?utm_source=tag-newest react 的jsx document ...

  7. Code Blocks+gtest环境配置

    本文仅介绍Code::Blocks+gtest环境配置,gtest具体使用方法请参考: 玩转Google开源C++单元测试框架Google Test系列(gtest)(总) http://www.cn ...

  8. Vert.x

    Vert.x是一个基于JVM.轻量级.高性能的应用平台,非常适用于最新的移动端后台.互联网.企业应用架构.Vert.x基于全异步Java服务器Netty,并扩展出了很多有用的特性. 同时支持多种编程语 ...

  9. ES6学习笔记之map、set与数组、对象的对比

    ES6 ES5中的数据结构,主要是用Array和Object.在ES6中主要新增了Set和Map数据结构.到目前为止,常用的数据结构有四种Array.Object.Set.Map.下面话不多说了,来一 ...

  10. 【运维技术】Nexus私服安装配置常用问题

    maven私服安装配置 软件安装及基本配置 安装配置 # 安装jdk,参考其他教程 mkdir -p /app/nexus2 # 创建目录 wget https://download.sonatype ...