Network(特殊的输入格式+tarjan求割点模板题)

本文介绍了一种基于Tarjan算法求解无向图中割点的方法,并提供了完整的AC代码示例。通过具体实例讲解了如何识别网络中关键节点,这些节点一旦发生故障将导致网络部分区域失去连接。


Link:http://poj.org/problem?id=1144



Network
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11006 Accepted: 5088

Description

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is 
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure 
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated 
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;

Output

The output contains for each block except the last in the input file one line containing the number of critical places.

Sample Input

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

Sample Output

1
2

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

Source



题意:对于输入格式,好好理解这段话( Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place.)。知道如何构图后,剩下的就是要求无向图的割点了。


编程思想:tarjan求割点模板。

求割点算法原理:

求割点。判断一个点是否是割点有两种判断情况:

如果u为割点,当且仅当满足下面的1条

1、如果u为树根,那么u必须有多于1棵子树

2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。

然后根据这两句来找割点就可以了。



AC code:

#include <iostream>
#include <cmath>
#include<stdlib.h> 
#include<vector>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#define LL long long
using namespace std;
#define INF 0x7fffffff
#define N 50100
//N为最大点数
#define M 150100
//M为最大边数
int n, m;//n m 为点数和边数

struct Edge{
	int from, to, nex,w;
	bool sign;//是否为桥
}edge[M<<1];
int head[N], edgenum;
void add(int u, int v,int w){//边的起点和终点
	Edge E={u, v, head[u], w,false};
	edge[edgenum] = E;
	head[u] = edgenum++;
}

int DFN[N], Low[N], Stack[N], top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
int taj;//连通分支标号,从1开始
int ge[N];//ge[i]!=0表示i为割点 
int ans;
void tarjan(int u ,int fa)
{  
	DFN[u] = Low[u] = ++ Time ;  
	int son=0; 
	for (int i = head[u] ; ~i ; i = edge[i].nex )
	{  
		int v = edge[i].to ;
		son++;  
		if(DFN[v] == -1)
		{  
			tarjan(v , u) ;  
			Low[u] = min(Low[u] ,Low[v]) ;
			/*如果u为割点,当且仅当满足下面的1条:
			1、如果u为树根,那么u必须有多于1棵子树
			2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。*/
			if(u==1&&son>1||DFN[u]<=Low[v]&&u!=1)
			{
				ge[u]++;
			}
		}  
 		else if(v!=fa) Low[u] = min(Low[u] ,DFN[v]) ; 		
	}  
}

void init()
{
	memset(head, -1, sizeof(head)); 
	edgenum=0;
	Time=0; 
	ans=0;
	memset(DFN,-1,sizeof(DFN));
	memset(ge,0,sizeof(ge));
}

int main()
{
    //freopen("D:\\in.txt","r",stdin);
	int u,v,ww,i,j,k,T;
	//scanf("%d",&T);
	while(scanf("%d",&n)!=EOF&&n)
	{
		init();
		while(scanf("%d",&u)!=EOF&&u)
		{
			while(getchar()!='\n')
			{
				scanf("%d",&v);
				add(u,v,0);
				add(v,u,0);
			}
		}
	
		tarjan(1,-1);
		for(i=1;i<=n;i++)
		{
			if(ge[i])
				ans++;
		}
		printf("%d\n",ans);
	}
	
	return 0;
 } 


### Tarjan算法解图中的实现方法及其原理 Tarjan算法是一种高效的深度优先搜索(DFS)算法,用于解图中的和桥(边)。它通过一次DFS遍历即可完成所有关键信息的收集,从而避免了暴力法多次DFS的低效性。 #### 的定义 是指在一个无向连通图中,删除某个顶及其相连的边后,图的连通分量数量增加。换句话说,该顶是图中连接多个子图的关键节。判断一个顶是否为的核心思想是:如果该顶的子图无法通过回边连接到其祖先节,则该顶。 #### Tarjan算法的基本原理 Tarjan算法基于DFS,并维护三个关键数组:`dfn[]`、`low[]`和`parent[]`。 - `dfn[u]`:记录顶`u`在DFS中的访问顺序(时间戳)。 - `low[u]`:记录顶`u`通过其子节能够访问到的最早的祖先节的时间戳。 - `parent[u]`:记录顶`u`的父节。 在DFS过程中,对于每条边`(u, v)`: 1. 如果`v`未被访问,则递归访问`v`,并更新`low[u] = min(low[u], low[v])`。 2. 如果`v`已被访问且`v`不是`u`的父节,则说明存在回边,此时更新`low[u] = min(low[u], dfn[v])`。 的判定条件为: - 对于非根节`u`,如果存在一个子节`v`,使得`low[v] &gt;= dfn[u]`,则`u`是。 - 对于根节,如果其有多个子节分支,则根节。 #### 实现细节 以下是Tarjan算法的C++实现代码: ```cpp #include &lt;bits/stdc++.h&gt; using namespace std; const int MAXN = 10005; vector&lt;int&gt; graph[MAXN]; // 图的邻接表表示 int dfn[MAXN], low[MAXN], parent[MAXN]; // 时间戳、最早祖先时间戳、父节 bool isCut[MAXN]; // 记录是否为 int timestamp = 0; // 时间戳变量 // DFS函数 void tarjan(int u, int root) { int child = 0; // 记录当前节的子节数量 dfn[u] = low[u] = ++timestamp; for (int v : graph[u]) { if (!dfn[v]) { // 如果v未被访问 parent[v] = u; tarjan(v, root); low[u] = min(low[u], low[v]); child++; // 判定 if ((u != root &amp;&amp; low[v] &gt;= dfn[u]) || (u == root &amp;&amp; child &gt; 1)) { isCut[u] = true; } } else if (v != parent[u]) { // 回边 low[u] = min(low[u], dfn[v]); } } } // 主函数 int main() { int n, m; cin &gt;&gt; n &gt;&gt; m; for (int i = 0; i &lt; m; i++) { int u, v; cin &gt;&gt; u &gt;&gt; v; graph[u].push_back(v); graph[v].push_back(u); } for (int i = 1; i &lt;= n; i++) { if (!dfn[i]) { tarjan(i, i); } } // 输出所有 cout &lt;&lt; &quot;为:&quot;; for (int i = 1; i &lt;= n; i++) { if (isCut[i]) { cout &lt;&lt; i &lt;&lt; &quot; &quot;; } } return 0; } ``` #### 算法复杂度 Tarjan算法的时间复杂度为`O(n + m)`,其中`n`是顶数,`m`是边数。这是因为算法只需要对图进行一次DFS遍历,且每个顶和边仅被访问一次。 #### 应用场景 Tarjan算法广泛应用于网络分析、电路设计和社交网络等领域。例如,在通信网络中,可能代表网络中的关键节,删除这些节可能导致网络分裂。通过识别这些关键节,可以优化网络结构以提高其鲁棒性。 #### 总结 Tarjan算法通过DFS和三个关键数组(`dfn[]`、`low[]`、`parent[]`)高效地解图中的问题。其核心思想是利用回边和时间戳信息判断顶是否为。该算法在理论研究和实际应用中均具有重要意义。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值