POJ-1144 Network——Trajan+割点

【题目描述】

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

【题目分析】
题目要求求割点,首先什么是割点呢?就是一个连通图/连通分量中将这个点去掉就不再是一个连通图的点。
我们想要求割点,很显然可以直接暴力,先在一个连通图里面删去这个点所有的边,如果变得不连通了就是割点。可是这样时间复杂度显然是不允许的。
我们再来仔细观察割点有什么特点:删去这个点就有一些点无法到达。
我们结合Trajan算法中的DFS搜索树进行分析,如果一个点是割点,如果他后面的子树就无法访问到前面的点,那么删去这个点,前面的点也就无法访问后面的点了,前后两部分无法相互访问,那说明这个点就是一个割点。我们如何判断后面的子树不会访问到前面的点呢?我们可以用Trajan算法中的LOW数组 ,如果子树的LOW数组的值比这个点的DFN的值还大说明这个点就是一个割点。
可是对于根节点(开始访问的点)来讲,就算这个点是一个割点后面的点也不会访问到他前面的点,但是这个根节点不同搜索树上的点是互相没有关系的,如果他有两个或者两个以上的搜索树,那么删去根节点也可以形成割点。但是对于一般的点,就算他有好几个儿子节点也不一定是一个割点,因为这些儿子之中有可能有之前访问过的点,但是对于访问过的点我们是不会放入这个搜索树的,所以不同搜索树有可能访问到同一个之前的点,这样就算删去这个点也可能有通路。但是对于根节点就无法访问到更前面的点了,所以就可以确定是一个割点。

总结一下,割点总共就两类:

  1. 含有两个或者两个以上子树的根节点u (u==root(u==root(u==root && son&gt;1)son&gt;1)son>1)
  2. 子树节点v的搜索树中不含有祖先节点的点u(u!=root(u!=root(u!=root && LOW[v]&gt;=DFN[u])LOW[v]&gt;=DFN[u])LOW[v]>=DFN[u])

【AC代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;typedef long long ll;
const int MAXN=105;
const int MAXM=MAXN*MAXN;
struct node
{int v,next;
}Edge[MAXM];
int head[MAXN],tot;
int DFN[MAXN],LOW[MAXN];
bool cnt[MAXN];
int vis[MAXN],Time;
int n,root,ans;void init()
{memset(head,0,sizeof(head));tot=0;memset(vis,0,sizeof(vis));Time=0;memset(cnt,0,sizeof(cnt));
}void AddEdge(int u,int v)
{tot++;Edge[tot].v=v; Edge[tot].next=head[u];head[u]=tot;
}void read()
{int u,v;while(~scanf("%d",&u) && u){while(getchar()!='\n'){scanf("%d",&v);AddEdge(u,v); AddEdge(v,u);}}
}void Trajan(int x,int fa)
{int v;int son=0;DFN[x]=LOW[x]=++Time;vis[x]=1;for(int i=head[x];i;i=Edge[i].next){v=Edge[i].v;if(vis[v]==0){Trajan(v,x);son++;LOW[x]=min(LOW[x],LOW[v]);if(x==root &&son>1 || x!=root && LOW[v]>=DFN[x]){cnt[x]=true;}}else if(vis[v]==1){LOW[x]=min(LOW[x],DFN[v]);}}vis[x]==2;	//说明这个搜索数完结了和其他的点就没有什么关系了,因此设为2其他点就不会访问到
}void solve()
{ans=0;root=1;for(int i=1;i<=n;i++){if(!vis[i]){root=i;Trajan(i,i);}}for(int i=1;i<=n;i++){if(cnt[i]) ans++;}printf("%d\n",ans);
}int main()
{while(~scanf("%d",&n) && n){init();read();solve();}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384059.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux--生产者与消费者

http://blog.csdn.net/gebushuaidanhenhuai/article/details/74011636 基本概念 提到生产者和消费者&#xff0c;我们最有可能想到的是商店卖东西&#xff0c;顾客在货架上(缓冲区&#xff09;买东西。 生产者消费者问题&#xff0c;其实是一个多线程同步问题的经典案例。该问…

进程的挂起以及可重入函数

相关接口     pause 函数用于将进程挂起. 如果信号的处理动作是终止进程, 则进程终止, pause 函数没有返回值; 如果信号的处理动作是忽略, 则进程被挂起, pause函数不返回, 如果信号的处理动作是捕捉, 则调用信号处理动作之后pause 返回 -1.来看一段代码 #include<s…

POJ1236Network of Schools——强连通分量缩点建图

【题目描述】 A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distri…

C——通过调用函数分配内存

http://blog.csdn.net/u012627502/article/details/3579724 1&#xff09;以返回值方式返回&#xff1a;把动态分配的存储位置地址&#xff0c;赋值给指针类型返回值&#xff08;不同于被调用函数的自动变量地址&#xff09; 2&#xff09;以形参形式返回&#xff1a;二级指针类…

gdb调试多进程程序

1.gdb下调试多进程程序只需要以下几条命令即可              除此之外还可以查看正在调试的进程 info inferiors, 同时也可以将当前正在调试的进程切换到另外一个进程中让其取运行     2.代码调试演示 #include<stdio.h> #include<stdlib.h> #…

BZOJ1123-BLO——强连通分量求割点+计数

【题目描述】 Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. 所有towns连通。Input 输入n<100000 m<500000及m条边Output 输出n个数&#xff0c;代表如果把第i个点去掉&#xff0c;将有多少对点不能互通。Sample Input 5…

关于memcpy和memmove两函数的区别

http://blog.csdn.net/caowei840701/article/details/8491836 [cpp] view plaincopy <p> 关于memcpy和memmove两个c标准库函数&#xff0c;其功能都是将一块内存区域中的指定大小内容复制到目标内存中&#xff0c;在翻阅c标准库实现的源代码我们发现他们是有区别的。&…

判断字符串出栈合法性

先来看说一下思路 接下来就是写代码了 int StackOrder(SeqStack* stack, char* input, char* output, int size_input, int size_output) {if(stack NULL || input NULL || output NULL){return 0;}int i_input 0;int j_output 0;SeqStackType value;for(; j_output <…

CodeForces - 1200C——小模拟

【题目描述】 Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by n sectors, and the outer area is equally divided by m sectors. A wall exists between each pair of sectors of same area (inner o…

1 单例模式

达内 闵大神 //饿汉单例模式 #include <iostream> using namespace std;class Singleton { public:static Singleton& getInstance(){return s_instance;} private:Singleton(){}Singleton(const Singleton& that){}static Singleton s_instance;//静态成员变量 …

共享栈

1.定义 所谓共享栈就是利用一个数组实现两个栈. 先来看一下共享栈的数据结构 typedef struct SharedStack {int top1;int top2;SeqStackType* data; }SharedStack; 2. 初始化 void SharedStackInit(SharedStack* stack) {if(stack NULL){return;//非法输入}stack -> top…

BZOJ1018 | SHOI2008-堵塞的交通traffic——线段树维护区间连通性+细节

【题目描述】 BZOJ1018 | SHOI2008-堵塞的交通traffic 有一天&#xff0c;由于某种穿越现象作用&#xff0c;你来到了传说中的小人国。小人国的布局非常奇特&#xff0c;整个国家的交通系统可 以被看成是一个2行C列的矩形网格&#xff0c;网格上的每个点代表一个城市&#xff0…

C++ 函数隐藏

C该函数隐藏 只有基类成员函数的定义已声明virtualkeyword&#xff0c;当在派生类中的时间&#xff0c;以支付功能实现&#xff0c;virtualkeyword可以从时间被添加以增加。它不影响多状态。 easy混淆视听&#xff0c;掩盖&#xff1a; &#xff0c;规则例如以下&#xff1a; …

迷宫求解(递归)

首先来看一下迷宫简易图                                  我们用 0 来表示该位置是墙, 用 1 来表示该位置是路. 所以, 我们在处理迷宫问题的时候可以将其看成一个二维数组即可, 而对应的每一条路我们可以用坐标的形式将其表示, 所以还需要…

CodeForces - 786C——二分+模拟?

【题目描述】 Rick and Morty want to find MR. PBH and they cant do it alone. So they need of Mr. Meeseeks. They Have generated n Mr. Meeseeks, standing in a line numbered from 1 to n. Each of them has his own color. i-th Mr. Meeseeks color is ai.Rick and M…

迷宫求解(非递归)

上篇文章写出了利用函数形成栈桢的特性完成迷宫求解问题, 本篇文章我们自己手动维护一个栈, 其进行出栈, 入栈, 取栈顶元素, 来完成迷宫求解寻路的过程     思路和以前一样, 首先, 我们先定义一个栈, 对其初始化, 同时, 定义一个迷宫地图, 对该地图进行初始化, 先判断当前…

数据结构练习——双向链表

http://www.cnblogs.com/-Lei/archive/2012/04/10/2440399.html 复习一下数据结构。。。。说不准下个星期就用上了 只不过写的很简单&#xff0c;没有封装 DoubleLinkList.h #ifndef GUARD_DoubleLinkList_h #define GUARD_DoubleLinkList_h#include <stdio.h>struct Li…

CodeChef - DGCD——树链剖分+差分

【题目描述】 Youre given a tree on N vertices. Each vertex has a positive integer written on it, number on the ith vertex being vi. Your program must process two types of queries :1. Find query represented by F u v : Find out gcd of all numbers on the uniq…

UVa272-TeX中的引号

【题目描述】 传送门 【题目分析】 今天开始刷紫书的题目啦 这道题很简单&#xff0c;需要注意的是cgetchar()需要加上括号&#xff0c;因为赋值语句的优先级比判等低 而且书中说好像最好用整型变量&#xff0c;因为EOF的值为-1&#xff0c;在字符变量中没有这个值。&#xf…

C++中友元(友元函数和友元类)的用法和功能

http://blog.csdn.net/adriano119/article/details/5914443/ 采用类的机制后实现了数据的隐藏与封装&#xff0c;类的数据成员一般定义为私有成员&#xff0c;成员函数一般定义为公有的&#xff0c;依此提供类与外界间的通信接口。但是&#xff0c;有时需要定义一些函数&#x…