POJ - 1523 SPF
题目要求割顶集,并且还要求出去掉割顶之后剩下的图连通数目。
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的更多相关文章
- poj 1523 SPF(双连通分量割点模板)
题目链接:http://poj.org/problem?id=1523 题意:给出无向图的若干条边,求割点以及各个删掉其中一个割点后将图分为几块. 题目分析:割点用tarjan算法求出来,对于每个割点 ...
- zoj 1119 / poj 1523 SPF (典型例题 求割点 Tarjan 算法)
poj : http://poj.org/problem?id=1523 如果无向图中一个点 u 为割点 则u 或者是具有两个及以上子女的深度优先生成树的根,或者虽然不是一个根,但是它有一个子女 w, ...
- POJ 1523 SPF tarjan求割点
SPF Time Limit: 1000MS Memory Limit ...
- POJ 1523 SPF(寻找关节点)
SPF Time Limit: 1000MS Memory ...
- POJ 1523 SPF(求割点)
题目链接 题意 : 找出图中所有的割点,然后输出删掉他们之后还剩多少个连通分量. 思路 : v与u邻接,要么v是u的孩子,要么u是v的祖先,(u,v)构成一条回边. //poj1523 #includ ...
- POJ 1523 SPF (割点,连通分量)
题意:给出一个网络(不一定连通),求所有的割点,以及割点可以切分出多少个连通分量. 思路:很多种情况. (1)如果给的图已经不是连通图,直接“ No SPF nodes”. (2)求所有割点应该不难 ...
- zoj 1119 /poj 1523 SPF
题目描述:考虑图8.9中的两个网络,假定网络中的数据只在有线路直接连接的2个结点之间以点对点的方式传输.一个结点出现故障,比如图(a)所示的网络中结点3出现故障,将会阻止其他某些结点之间的通信.结点1 ...
- poj 1523 SPF【点双连通求去掉割点后bcc个数】
SPF Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7246 Accepted: 3302 Description C ...
- POJ 1523 SPF (去掉割点能形成联通块的个数)
思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一 ...
随机推荐
- FontAwesome 奥森图标的学习
很早之前,就看到大家在使用代码做出很漂亮的图标,但是觉得需求不是很大,所以就没有看,但是技多不压身,这次有时间来学习下. FontAwesome官方网站 1,下载文件包 里面有两个文件夹,css 和 ...
- scala学习笔记:理解函数
定义一个函数: scala> def foo(x:Int)=x*2 foo: (x: Int)Int 可以采用匿名参数: scala> def foo:((Int)=>Int) = ...
- ###《Machine Learning》by Andrew NG
点击查看Evernote原文. #@author: gr #@date: 2014-10-17 #@email: forgerui@gmail.com Fundamental 一. 矩阵的迹.秩 矩阵 ...
- ###学习《C++ Primer》- 4
点击查看Evernote原文. #@author: gr #@date: 2014-10-16 #@email: forgerui@gmail.com Part 4: STL关联容器(第11章) 一. ...
- ios 获取字符串所需要占用的label的高度
// 设置字体大小 UIFont *fnt=[UIFont systemFontOfSize:16]; NSDictionary *attribute = @{NSFontAttributeNa ...
- Custom Action : dynamic link library
工具:VS2010, Installshield 2008 实现功能: 创建一个C++ win32 DLL的工程,MSI 工程需要调用这个DLL,并将Basic MSI工程中的两个参数,传递给DLL, ...
- DLL详解及Denpendcy Walker的使用
下面的文章被N次转载,为了尊重原作,\(^o^)/~,贴出最早发布这篇文章的地址及作者. 动态链接库 Windows的活动大陆 2006-07-26 09:21 作者:狂ρκ来源:电脑爱好者 在 ...
- STL的简介
Standard Template Library,(标准模板库)<来自百度百科的整理> ————可复用性(reusability) STL是基于模板,内联函数的使用使得生成的代码短小高效 ...
- 国内IT技术博客对比
今天我想就自己对用了国内几个IT行业领先的博客做一个心得体会的总结: 我总共是用了三个,第一个是新浪,第二个是CSDN,第三个是博客园: 当然期间有自己搭建过wordpress,也用了一段时间,但是感 ...
- Qt-获取主机网络信息之QHostInfo
Qt中提供了几个用于获取主机网络信息的类,包括QHostInfo.QHostAddress.QNetworkInterface以及QNetworkAddress.在本节中,我将在这里总结QHostIn ...