POJ 1523 SPF 割点与桥的推断算法-Tarjan
题目链接:
题意:
问一个连通的网络中有多少个关节点,这些关节点分别能把网络分成几部分
题解:
Tarjan 算法模板题
顺序遍历整个图,能够得到一棵生成树:
树边:可理解为在DFS过程中訪问未訪问节点时所经过的边。也称为父子边
回边:可理解为在DFS过程中遇到已訪问节点时所经过的边。也称为返祖边、后向边
对根节点u,若其有两棵或两棵以上的子树。则该根结点u为割点。
对非叶子节点u(非根节点)。若其子树的节点均没有指向u的祖先节点的回边,说明删除u之后,根结点与u的子树的节点不再连通;则节点u为割点。
// 当(u,v)为树边且low[v]>dfn[u]时,表示v节点仅仅能通过该边(u,v)与u连通,那么(u,v)即为割边。
用一个数组保存每一个节点的子树个数就可以
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxn 1050
using namespace std;
int dfn[maxn],low[maxn]; //dfs序 和子树连接的最小节点
int vis[maxn];
vector<int>edge[maxn];
int child[maxn];
int num,son;
void init()
{
memset(vis,0,sizeof(vis));
memset(child,0,sizeof(child));
vis[1]=1;
num=0;
son=0;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++num;
vis[u]=1;
for(int i=0; i<edge[u].size(); i++)
{
int v=edge[u][i];
if(!vis[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v]) //得到子树
{
if(u!=1)
child[u]++;
else
son++;
}
}
else low[u]=min(low[u],dfn[v]);
}
}
int main()
{
// freopen("in.txt","r",stdin);
int a,b;
int Case=1;
while(1)
{
while(scanf("%d",&a)&&a)
{ scanf("%d",&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
init(); Tarjan(1);
// for(int i=1;i<=5;i++)
// cout<<dfn[i]<<" "<<low[i]<<endl;
if(Case>1)
cout<<endl;
printf("Network #%d\n",Case++);
int flag=1;
child[1]=son-1;
for(int i=1; i<=1000; i++)
if(child[i]>0)
{
flag=0;
printf(" SPF node %d leaves %d subnets\n",i,child[i]+1);
}
if(flag)
cout<<" No SPF nodes"<<endl; for(int i=1; i<=1000; i++)
edge[i].clear();
scanf("%d",&a);
if(a==0)
break;
scanf("%d",&b);
edge[a].push_back(b);
edge[b].push_back(a);
}
return 0;
}
POJ 1523 SPF 割点与桥的推断算法-Tarjan的更多相关文章
- POJ 1523 SPF (割点,连通分量)
题意:给出一个网络(不一定连通),求所有的割点,以及割点可以切分出多少个连通分量. 思路:很多种情况. (1)如果给的图已经不是连通图,直接“ No SPF nodes”. (2)求所有割点应该不难 ...
- POJ 1523 SPF 割点 Tarjan
SPF Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9317 Accepted: 4218 Description C ...
- Electricity POJ - 2117 + SPF POJ - 1523 去除割点后求强连通分量个数问题
Electricity POJ - 2117 题目描述 Blackouts and Dark Nights (also known as ACM++) is a company that provid ...
- POJ 1523 SPF (去掉割点能形成联通块的个数)
思路:使用tarjan算法求出割点,在枚举去掉每一个割点所能形成的联通块的个数. 注意:后来我看了下别的代码,发现我的枚举割点的方式是比较蠢的方式,我们完全可以在tarjan过程中把答案求出来,引入一 ...
- 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 求割点以及删除该割点后联通块的数量
SPF Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7136 Accepted: 3255 Description C ...
- POJ 1523 SPF tarjan求割点
SPF Time Limit: 1000MS Memory Limit ...
- POJ 1523 SPF(求割点)
题目链接 题意 : 找出图中所有的割点,然后输出删掉他们之后还剩多少个连通分量. 思路 : v与u邻接,要么v是u的孩子,要么u是v的祖先,(u,v)构成一条回边. //poj1523 #includ ...
随机推荐
- Flask接通微信公众号
import hashlib import xml.etree.ElementTree as ET from flask import Flask, request import time app = ...
- Python中 append 和 extend 的区别
Python中Lists 的两个方法: append 和 extend : list.append(object) 向列表中添加一个对象object.append 接受一个参数,这个参数可以是任何数据 ...
- vi命令速查图
Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7
- Fusion Tables 图层用于呈现 Google Fusion Tables 中包含的数据
Google Maps API 允许您使用 FusionTablesLayer 对象将 Google Fusion Tables 中包含的数据呈现为地图上的图层.Google Fusion Table ...
- Linux内核同步 - Read/Write spin lock
一.为何会有rw spin lock? 在有了强大的spin lock之后,为何还会有rw spin lock呢?无他,仅仅是为了增加内核的并发,从而增加性能而已.spin lock严格的限制只有一个 ...
- Spark Core Runtime分析: DAGScheduler, TaskScheduler, SchedulerBackend
Spark Runtime里的主要层次分析,梳理Runtime组件和运行流程, DAGScheduler Job=多个stage,Stage=多个同种task, Task分为ShuffleMapTas ...
- 【服务器防护】linux 如何查看防火墙是否开启
service iptables status可以查看到iptables服务的当前状态.但是即使服务运行了,防火墙也不一定起作用,你还得看防火墙规则的设置 iptables -L在此说一下关于启动和关 ...
- Excel 求差集和并集
1. excel求两列差集(查找A列中与B列不同的部分) 示例: 行号 A列 B列 C列结果(A-B) 1 1 3 ...
- VirtualBOX 不能mount优盘,移动硬盘解决方案
The Solution (basically nayasis' solution with the second driver installation added): Safely unplug ...
- echarts geo地图坐标转换为页面Offset坐标
https://github.com/apache/incubator-echarts/issues/2540 代码示例: // 获取系列 ) // 获取地理坐标系实例 var coordSys = ...