题目要求割顶集,并且还要求出去掉割顶之后剩下的图连通数目。

tarjan算法求出割顶后直接枚举就可以了吧。

一开始想到利用iscut[u]的次数也就是点u被判定为割顶的次数求连通分量数,还有利用与结点u相连的点的bccno不同的编号来判定,都是不行的,具体原因自己想清楚比较好。

以后就不会犯这样的错误了。

代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define esp 1e-6
#define pi acos(-1.0)
#define pb push_back
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mp(a, b) make_pair((a), (b))
#define in freopen("in.txt", "r", stdin);
#define out freopen("out.txt", "w", stdout);
#define print(a) printf("%d\n",(a));
#define bug puts("********))))))");
#define stop system("pause");
#define Rep(i, c) for(__typeof(c.end()) i = c.begin(); i != c.end(); i++)
#define inf 0x0f0f0f0f using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii,int> VII;
typedef vector<int>:: iterator IT; const int maxn = ;
int pre[maxn], iscut[maxn], vis[maxn], low[maxn], bccno[maxn], dfs_clock, bcc_cnt;
VI g[maxn], bcc[maxn];
int flag;
struct edge
{
int u, v;
edge(int u, int v):u(u), v(v) {}
};
stack<edge> S;
int dfs(int u, int fa)
{
int lowu = pre[u] = ++dfs_clock;
int child = ;
for(int i = ; i < g[u].size(); i++)
{
int v = g[u][i];
edge e = edge(u, v);
if(!pre[v])
{
S.push(e);
child++;
int lowv = dfs(v, u);
lowu = min(lowu, lowv);
if(lowv >= pre[u])
{
bcc_cnt++;
bcc[bcc_cnt].clear();
iscut[u] = ;
for(;;)
{
edge x = S.top();
S.pop();
if(bccno[x.u] != bcc_cnt)
{
bccno[x.u] = bcc_cnt;
bcc[bcc_cnt].pb(x.u);
}
if(bccno[x.v] != bcc_cnt)
{
bccno[x.v] = bcc_cnt;
bcc[bcc_cnt].pb(x.v);
}
if(x.u == u && x.v == v) break;
}
}
}
else if(pre[v] < pre[u] && v != fa)
{
S.push(e);
lowu = min(lowu, pre[v]);
}
}
if(child == && fa < )
{
iscut[u] = ;
}
return low[u] = lowu;
} void find_bcc(int n)
{
memset(pre, , sizeof(pre));
memset(iscut, , sizeof(iscut));
memset(bccno, , sizeof(bccno)); dfs_clock = bcc_cnt = ;
for(int i = ; i <= n; i++)
if(!pre[i])
dfs(i, -);
}
void dfs1(int u)
{
vis[u] = ;
for(int i = ; i < g[u].size(); i++)
{
int v = g[u][i];
if(!vis[v])
{
dfs1(v);
}
}
}
void init(void)
{
for(int i = ; i < maxn; i++)
g[i].clear();
}
int main(void)
{
int n;
int u, v;
int t = ;
while(scanf("%d", &u), u)
{
init();
flag = ;
n = ;
scanf("%d", &v);
g[u].pb(v);
g[v].pb(u);
n = max(max(u, v), n);
while(scanf("%d", &u), u)
{
scanf("%d", &v);
g[u].pb(v);
g[v].pb(u);
n = max(max(u, v), n);
}
find_bcc(n);
if(t) puts("");
t++;
printf("Network #%d\n", t);
for(int u = ; u <= ; u++)
if(iscut[u])
{
flag = ;
memset(vis, , sizeof(vis));
int cnt = ;
vis[u] = ;
for(int i = ; i < g[u].size(); i++)
{
int v = g[u][i];
if(!vis[v])
cnt++, dfs1(v);
}
printf(" SPF node %d leaves %d subnets\n", u, cnt);
}
if(!flag)
{
puts(" No SPF nodes");
}
}
return ;
}

POJ - 1523 SPF的更多相关文章

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

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

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

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

  3. POJ 1523 SPF tarjan求割点

                                                                   SPF Time Limit: 1000MS   Memory Limit ...

  4. POJ 1523 SPF(寻找关节点)

                                                                         SPF Time Limit: 1000MS   Memory ...

  5. POJ 1523 SPF(求割点)

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

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

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

  7. zoj 1119 /poj 1523 SPF

    题目描述:考虑图8.9中的两个网络,假定网络中的数据只在有线路直接连接的2个结点之间以点对点的方式传输.一个结点出现故障,比如图(a)所示的网络中结点3出现故障,将会阻止其他某些结点之间的通信.结点1 ...

  8. poj 1523 SPF【点双连通求去掉割点后bcc个数】

    SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7246   Accepted: 3302 Description C ...

  9. POJ 1523 SPF (去掉割点能形成联通块的个数)

    思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一 ...

随机推荐

  1. 读jQuery官方文档:jQuery对象

    jQuery对象 当用$符号包裹一个CSS风格选择器的时候,你得到一个jQuery对象. var heading = $('h1'); jQuery对象是对DOM ELement封装过后的数组.注意, ...

  2. myeclipse的新建severlet不见解决方法

    点击myeclipse中的window菜单里面选择myeclipse java Enterprise 选项就可以恢复到默认.

  3. 第八篇、UITableView常用功能(左滑出现多个按钮,多选删除等)

    1.左滑动出现多个按钮 /** * 只要实现了这个方法,左滑出现按钮的功能就有了 (一旦左滑出现了N个按钮,tableView就进入了编辑模式, tableView.editing = YES) */ ...

  4. 在Xcode6中找回失去的模板

    [Add]2014.07.27 添加OC category.protocol模板 Xcode 6从beta 3开始在创建新项目窗口中移除了“Empty Application”,如下: 其他选项很不幸 ...

  5. ios面试题集锦(一)

    一.前言部分 文中的问题多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.iOS9有哪些新特性? 答案: 1)改进了 Siri 基于日期.位置和相簿名称来搜索个人照片和视频 要求 Siri 来 ...

  6. Easyui-Combobox多选下拉框

    因为工作需要,引入combobox多选下拉框,并且获取选择的值并以","分开. 效果如下: 代码如下: <html> <head> <title> ...

  7. 函数-头文件//Calculator.h

    // // Calculator.h // 函数-头文件 // // Created by zhangxueming on 15/6/2. // Copyright (c) 2015年 zhangxu ...

  8. libjingle线程机制

    libjingle包装了所有的线程,包括signaling thread,worker thread, 和其它任何线程,用talk_base::Thread来包装.所有的 Thread对象由Threa ...

  9. Windows和Linux下查看Apache、MySQL、PHP版本

    # Windows查看Apache版本: 使用命令:httpd -v # Linux查看Apache版本: 使用命令:apachectl -v # Windows查看MySQL版本: 使用命令:mys ...

  10. MongoDB入门三步曲2--基本操作(续)--聚合、索引、游标及mapReduce

    mongodb 基本操作(续)--聚合.索引.游标及mapReduce 目录 聚合操作 MapReduce 游标 索引 聚合操作 像大多关系数据库一样,Mongodb也提供了聚合操作,这里仅列取常见到 ...