题目链接http://poj.org/problem?id=2117

题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大。

解题思路

一个大陷阱,m可以等于0,这时候要特判,结果就是n-1。

同时出题者脑子秀逗了,也不给C的范围。我开了两倍点大小RE了,于是怒开了五倍点大小才A了。

本题不是连通图,需要先计算原始图中的连通分量。方法就是dfs染色。

然后dfs求割点。

之后枚举割点,由于是非连通图,所以连通分量数=原始分量数+block-1。

-1的原因是,每次相当于对其中一个连通分量计算,加上新的block之后,所以要减-1。

#include "cstdio"
#include "cstring"
#include "vector"
using namespace std;
#define maxn 10005
struct Edge
{
int to,next;
}e[maxn*];
int dfs_clock,pre[maxn],block,head[maxn],tol;
bool cut[maxn],vis[maxn];
void addedge(int u,int v)
{
e[tol].to=v;
e[tol].next=head[u];
head[u]=tol++;
}
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].to;
if(!pre[v])
{
int lowv=dfs(v,u);
lowu=min(lowu,lowv);
if(lowv>=pre[u]) cut[u]=true;
}
else if(pre[v]<pre[u]&&v!=fa) lowu=min(lowu,pre[v]);
}
if(fa<&&child==) cut[u]=false;
return lowu;
}
void check(int u,int fa)
{
vis[u]=true;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].to;
if(!vis[v])
{
if(u==fa) block++;
check(v,fa);
}
}
}
void link(int u) //判断初始连通分量
{
vis[u]=true;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].to;
if(!vis[v]) link(v);
}
}
int main()
{
//freopen("in.txt","r",stdin);
int n,m,u,v;
while(scanf("%d%d",&n,&m)&&n)
{
memset(head,-,sizeof(head));
memset(pre,,sizeof(pre));
memset(cut,false,sizeof(cut));
dfs_clock=;tol=;
if(m==) printf("%d\n",n-); //特判
else
{
for(int i=; i<=m; i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
int res=,tt=;
for(int i=; i<n; i++) if(!vis[i]) {tt++;link(i);}
memset(vis,false,sizeof(vis));
for(int i=; i<n; i++) if(!pre[i]) dfs(i,-);
res=tt;
for(int i=; i<n; i++)
{
if(cut[i])
{
check(i,i);
if(tt) res=max(res,block+tt-);
block=;
memset(vis,false,sizeof(vis));
}
}
printf("%d\n",res);
}
}
}
2905876 neopenx POJ 2117 Accepted 732 2422 C++ 2129  

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

  1. poj 2117(割点的应用)

    题目链接:http://poj.org/problem?id=2117 思路:题目的意思是要求对于给定的无向图,删除某个顶点后,求最大的连通分量数.显然我们只有删掉割点后,连通分支数才会增加,因此我们 ...

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

    题目链接:http://poj.org/problem?id=1523 题目大意:连通图,找图中割点,并计算切除该割点后,图中的连通分量个数. 解题思路: POJ的数据很弱. Tarjan法求割点. ...

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

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

  4. POJ 2117 Electricity(割点求连通分量)

    http://poj.org/problem?id=2117 题意:求删除图中任意一个顶点后的最大连通分量数. 思路: 求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的. 这道题目我wa了 ...

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

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

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

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

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

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

  8. poj 1144(割点)

    题目链接:http://poj.org/problem?id=1144 题意:给出一个无向图,求关键节点的个数. 分析:双连通分量Tarjan算法直接求割点就行了,裸的模板题. AC代码: #incl ...

  9. poj 3177 边连通分量

    思路: dfs求出所有点的low值,然后对每个连通分量进行缩点,可以通过low来进行缩点.虽然在同一连通分量里可能存在不同的low值,但这并不影响缩点.将每个连通分量缩为一个点后,只要求出这个缩点后的 ...

随机推荐

  1. Linux文件与目录常用命令

    目录常用命令: cd:切换目录 pwd:显示当前目录 mkdir:新建一个目录 rmdir:删除一个空的目录 ## cd 命令几种常用方法: cd ~username 切换到用户username的主文 ...

  2. Firemonkey的旁门左道[六]

    转载:http://blog.csdn.net/qustdong/article/details/9992033 今天还是讲讲和图形有关的事情,这次的难度再增加些,不是直接改源代码了, 而是通过RTT ...

  3. RemObjects SDK Source For Delphi XE7

    原文:http://blog.csdn.net/tht2009/article/details/39545545 1.目前官网最新版本是RemObjects SDK for Delphi and al ...

  4. 细微之处:比较两种CSS清除浮动的兼容

    http://www.cnblogs.com/bienfantaisie/archive/2011/05/27/2059597.html 清除浮动是连续浮动元素之后的必备工作,在工作中我做到需要清除浮 ...

  5. iOS7中都Bar的透明问题

    /* New behavior on iOS 7. Default is YES. You may force an opaque background by setting the property ...

  6. 重温CSS之背景、文本样式

    CSS背景样式: 背景色:background-color属性,设置元素的背景色,如:div {background:blue;}--设置所有div元素的背景为蓝色: 背景图像:background- ...

  7. July 31st, Week 32nd Sunday, 2016

    If you wept for the missing sunset, you would miss all the shining stars. 如果你为错过夕阳而哭泣,那你有可能也会错过灿烂的星空 ...

  8. weblogic 安装和部署项目(原创)

    1.下载weblogic(含破解文件,土豪请支持正版,谢谢!) 2.安装如下图: 3.新建domain 4.打开weblogic Console 5.开始部署项目 6.部署成功

  9. 数字型 、String字符串转换

    Java代码 收藏代码 String str = "1,2,3,4,5,6" public int[] StringtoInt(String str) { int ret[] = ...

  10. 二、JavaScript语言--JS基础--JavaScript入门篇

    1.如何插入JS 使用<script>标签在HTML网页中插入JavaScript代码.注意, <script>标签要成对出现,并把JavaScript代码写在<scri ...