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

题目大意:连通图,找图中割点,并计算切除该割点后,图中的连通分量个数。

解题思路

POJ的数据很弱。

Tarjan法求割点。

pre数组,记录这个点的dfs时间位置。

割点的条件是lowv>=pre[u], 即子点比父点先dfs,这时候父点就没有意义了,切掉父点连通分量数肯定会增加。

同时注意特判只有两个点的情况,这时候是不可能出现割点的。

求切除割点后的联通分量数:

从割点出发,把图dfs一遍,如果u=割点,那么对于每个子点v,block++

原理就是,切掉割点后,所有与其连接子点都要受到影响,统计第一次访问的子点v的block即可。

#include "cstdio"
#include "vector"
#include "string"
#include "iostream"
#include "cstring"
using namespace std;
#define maxn 1005
struct Edge
{
int next,to;
}e[maxn*];
int pre[maxn],dfs_clock,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])
{
child++;
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);
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int u,v,num=,no=;
bool flag=false;
memset(head,-,sizeof(head));
while(scanf("%d",&u)!=EOF)
{
if(!u)
{
if(!tol) break;
printf("Network #%d\n",++no);
for(int i=;i<=num;i++) if(!pre[i]) dfs(i,-);
bool flag=false;
for(int i=; i<=num; i++)
if(cut[i])
{
flag=true;
check(i,i);
printf(" SPF node %d leaves %d subnets\n",i,block);
memset(vis,,sizeof(vis));
block=;
}
if(!flag) cout<<" No SPF nodes"<<endl;
memset(pre,,sizeof(pre));
memset(cut,false,sizeof(cut));
memset(head,-,sizeof(head));
dfs_clock=;num=;tol=;
printf("\n");
}
else
{
scanf("%d",&v);num=max(num,max(u,v));
addedge(u,v);
addedge(v,u);
}
}
}
13424402 neopenx 1523 Accepted 216K 0MS C++ 2309B 2014-09-08 19:04:33

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

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

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

  2. poj 1523 割点 tarjan

    Description Consider the two networks shown below. Assuming that data moves around these networks on ...

  3. SPF POJ - 1523 割点+并查集

    题意: 问你这个图中哪个点是割点,如果把这个点去掉会有几个子网 代码: 1 //给你几个点,用着几个点形成了一个图.输入边形成的图,问你这个图中有多少个割点.每一个割点去掉后会形成几个强连通分量 2 ...

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

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

  5. poj 1523 SPF(双连通分量割点模板)

    题目链接:http://poj.org/problem?id=1523 题意:给出无向图的若干条边,求割点以及各个删掉其中一个割点后将图分为几块. 题目分析:割点用tarjan算法求出来,对于每个割点 ...

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

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

  7. zoj 1119 / poj 1523 SPF (典型例题 求割点 Tarjan 算法)

    poj : http://poj.org/problem?id=1523 如果无向图中一个点 u 为割点 则u 或者是具有两个及以上子女的深度优先生成树的根,或者虽然不是一个根,但是它有一个子女 w, ...

  8. POJ 1523 SPF tarjan求割点

                                                                   SPF Time Limit: 1000MS   Memory Limit ...

  9. POJ 1523 SPF(求割点)

    题目链接 题意 : 找出图中所有的割点,然后输出删掉他们之后还剩多少个连通分量. 思路 : v与u邻接,要么v是u的孩子,要么u是v的祖先,(u,v)构成一条回边. //poj1523 #includ ...

随机推荐

  1. 从零开始写一个武侠冒险游戏-8-用GPU提升性能(3)

    从零开始写一个武侠冒险游戏-8-用GPU提升性能(3) ----解决因绘制雷达图导致的帧速下降问题 作者:FreeBlues 修订记录 2016.06.23 初稿完成. 2016.08.07 增加对 ...

  2. Centos下samba共享打印机

    先说需求,公司有一台型号为HP LaserJet m1120 mfp的打印机,由于不是网络打印机使用起来十分不便,公司老大要求将这台打印机连在公司的内网linux服务器上(CentOS),然后配置sa ...

  3. poj2996 模拟

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3713   Accepted:  ...

  4. Linux 查看系统用户的登录日志

    查看用户登录系统的日志   有两类日志记录用户登录的行为,一是记录登录者的数据,一个是记录用户的登录时间   一,记录用户登录数据         /var/log/wtmp日志文件记录用户登录的数据 ...

  5. Linux MySQL 存储引擎详解

    MySQL常用的存储引擎为MyISAM.InnoDB.MEMORY.MERGE,其中InnoDB提供事务安全表,其他存储引擎都是非事务安全表. MyISAM是MySQL的默认存储引擎.MyISAM不支 ...

  6. simple_html_dom使用小结

    simple_html_dom使用小结 分类: PHP2012-08-31 14:24 3094人阅读 评论(0) 收藏 举报 htmlcallbackstringdivfunctionfile  1 ...

  7. 2014-08-07 SSDB 使用 rocksdb 引擎

    http://www.ideawu.net/blog/archives/824.html 为了满足各位对 Facebook 出品的 rocksdb 的爱好, SSDB 数据库也可以使用 rocksdb ...

  8. 基础知识《二》java的基本类型

    一.java基本数据类型 Java基本类型共有八种,基本类型可以分为三类,字符类型char,布尔类型boolean以及数值类型byte.short.int.long.float.double.数值类型 ...

  9. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  10. bootbox显示中文的按钮

    $("selector").on('click',function(){ bootbox.confirm({ title : "请确认", buttons: { ...