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. XMind思维导图自定义图标/图片

  2. PHPExcel使用-使用PHPExcel导出文件

    导出步骤: 1. 新建一个excel表格 ------------> 实例化PHPExcel类 2. 创建sheet(内置表)-------------> ( 1>. createS ...

  3. glide从入门到精通使用

    介绍 不论是开发Java还是你正在学习的Golang,都会遇到依赖管理问题.Java有牛逼轰轰的Maven和Gradle. Golang亦有godep.govendor.glide.gvt.gopac ...

  4. Python一键安装全部依赖包

    requirements.txt用来记录项目所有的依赖包和版本号,只需要一个简单的pip命令就能完成. pip freeze >requirements.txt 然后就可以用 pip insta ...

  5. windows 环境下安装elasticsearch ,ik,head,marvel

    elasticsearch  自带的中分分词器将会使中文分成一个一个的单词,需要安装ik分词等,ik分词分为 ik_smart(粗粒度分词),ik_max_word(细粒度分词)两种模式. 1:首先安 ...

  6. Selenium定位元素-Xpath的使用方法

    工具 Xpath的练习建议下载火狐浏览器,下载插件Firebug.Firepath. 由于最新版火狐不支持Firebug等扩展工具了,所以需要下载49版以下的版本安装https://ftp.mozil ...

  7. PHP 接收筛选项包含0的select下拉菜单的处理

    这种情况下,PHP的判断方法如下: $where = "1=1"; if ($get['status'] !== '' && $get['status'] !== ...

  8. spring boot 自定义过滤器链

    spring boot 会按照order值的大小,从大到小的顺序来依次过滤. 贴下代码: package com.osp.ucenter; import org.springframework.boo ...

  9. [LeetCode] 844. Backspace String Compare_Easy tag: Stack **Two pointers

    Given two strings S and T, return if they are equal when both are typed into empty text editors. # m ...

  10. [LeetCode] 561. Array Partition I_Easy tag: Sort

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...