Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11378   Accepted: 5285

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
/*
割点:在无向连通图中,若将该点去掉那么图变为两个或两个以上连通分量。
*/
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,next;
}es[MAXN];
int head[];
int E;
int Min(int a,int b)
{
return a>b?b:a;
}
void add_edge(int u,int v)
{
es[E].to=v;
es[E].next=head[u];
head[u]=E++;
}
int ans;
int root;
int dfn[];//dfs时,记录访问结点的时序
int low[];//记录结点的返祖最早结点
int vis[];
int articulation[];
int time;
void Tarjan(int u,int fa)
{
int son=;
vis[u]=;
dfn[u]=low[u]=++time;
for(int i=head[u];i!=-;i=es[i].next)
{
int v=es[i].to;
if(!vis[v])//父子边
{
Tarjan(v,u);
son++;
low[u]=Min(low[u],low[v]);
if((u==root&&son>)||(u!=root&&dfn[u]<=low[v]))
{
//1.若u为根节点且儿子不止一个,那么该点为割点
//2.若u不为根结点,当dfn[u]==low[v]时说明存在返祖边使u处于割边与环的交点,所以其为割点
//当dfn[u]<low[v]时,说明u指向v只有一条路径,所以u为割点
articulation[u]=;//不可设为ans++,因为一个割点可能连接多个割边
}
}
if(vis[v]&&v!=fa)//返祖边
{
low[u]=Min(low[u],dfn[v]);
}
}
}
int seek(int n)
{
int ans=;
for(int i=;i<=n;i++)
if(articulation[i]) ans++;
return ans;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n!=)
{
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(articulation,,sizeof(articulation));
E=;
time=;
ans=;
int u,v;
while(scanf("%d",&u)&&u!=)
{
while(getchar()!='\n')
{
scanf("%d",&v);
add_edge(u,v);
add_edge(v,u);
}
}
root=;
Tarjan(root,-);
int ans=seek(n);
printf("%d\n",ans);
}
return ;
}

简洁代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
vector<int> mp[MAXN];
int dfn[MAXN],low[MAXN],time;
bool crit[MAXN];
int root;
void dfs(int u,int fa)
{
dfn[u]=low[u]=++time;
int son=;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!dfn[v])
{
dfs(v,u);
son++;
low[u]=min(low[u],low[v]);
if((root==u&&son>)||(u!=root&&dfn[u]<=low[v]))
{
crit[u]=true;
}
}
else if(v!=fa) low[u]=min(low[u],dfn[v]);
}
} int n;
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
for(int i=;i<=n;i++)
mp[i].clear();
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
time=;
memset(crit,false,sizeof(crit));
int u;
while(scanf("%d",&u)!=EOF&&u!=)
{
int v;
while(getchar()!='\n')
{
scanf("%d",&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
}
for(int i=;i<=n;i++)
if(!dfn[i])
{
root=i;
dfs(i,-);
}
int cnt=;
for(int i=;i<=n;i++)
if(crit[i])
cnt++;
printf("%d\n",cnt); }
return ;
}

来个JAVA版的

 /**
* @title: poj1144
* @auther: baneHunter
* @time: 454MS
* @memory: 5244K
*/
import java.util.*;
import static java.lang.System.*;
public class Main{ static final int MAXN=105;
static class Graph{
ArrayList<Integer> vertex = new ArrayList<Integer>();
} static ArrayList<Integer> Input(String s)
{
ArrayList<Integer> v=new ArrayList<Integer>();
int e=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)==' ')
{
v.add(e);
e=0;
}
else
{
e*=10;
int x=(int)(s.charAt(i)-'0');
e+=x;
}
}
v.add(e);
return v;
}
static class Tarjan{
int n;
int m;
Tarjan(int n)
{
this.n=n;
for(int i=1;i<=n;i++)
map[i] = new Graph();
}
Graph map[] = new Graph[MAXN];
int dfn[] = new int[MAXN];
int low[] = new int[MAXN];
int time;
int root;
boolean articulation[] = new boolean[MAXN]; void addegde(int u,int v)
{
map[u].vertex.add(v);
map[v].vertex.add(u);
m++;
} void dfs(int fa,int u)
{
dfn[u]=low[u]=++time;
int son=0;
for(int i=0;i<map[u].vertex.size();i++)
{
int v=map[u].vertex.get(i);
if(dfn[v]==0)
{
dfs(u,v);
low[u]=Math.min(low[u], low[v]);
son++;
if((u==root&&son>=2)||(u!=root&&dfn[u]<=low[v]))
{
articulation[u]=true;
}
}
else if(v!=fa) low[u]=Math.min(low[u],dfn[v]);
}
} int cal()
{
for(int i=1;i<=n;i++)
{
if(dfn[i]==0)
{
root=i;
dfs(-1,i);
}
} int cnt=0;
for(int i=1;i<=n;i++)
{
if(articulation[i])
cnt++;
}
return cnt;
}
}
static Scanner in = new Scanner(System.in);
public static void main(String[] args){ int n;
while(in.hasNext())
{
n=in.nextInt();
in.nextLine();
if(n==0) break;
Tarjan tar = new Tarjan(n);
while(true)
{
String s;
s=in.nextLine();
ArrayList<Integer> v;
v=Input(s);
int u;
u=v.get(0);
if(u==0)break;
for(int i=1;i<v.size();i++)
{
int to=v.get(i);
if(to!=0)
{
tar.addegde(u, to);
tar.addegde(to, u);
}
}
}
int res=tar.cal();
out.println(res);
}
}
}

POJ1144(割点入门题)的更多相关文章

  1. hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  2. poj 2524:Ubiquitous Religions(并查集,入门题)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23997   Accepted:  ...

  3. poj 3984:迷宫问题(广搜,入门题)

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

  4. hdu 1754:I Hate It(线段树,入门题,RMQ问题)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. poj 3254 状压dp入门题

    1.poj 3254  Corn Fields    状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...

  6. zstu.4194: 字符串匹配(kmp入门题&& 心得)

    4194: 字符串匹配 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 206  Solved: 78 Description 给你两个字符串A,B,请 ...

  7. hrbustoj 1073:病毒(并查集,入门题)

    病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...

  8. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  9. hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)

    基础数据结构——顺序表(2) Time Limit: 1000 MS    Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...

随机推荐

  1. JavaScript读书笔记(5)-Object Date

    1.Object类型 (1)创建Object实例 第一种方式:new操作符后跟Object构造函数 var person=new Object(); person.name=”Nicholas”; p ...

  2. VSTS跟Kubernetes整合进行CI/CD

    利用VSTS跟Kubernetes整合进行CI/CD   为什么VSTS要搭配Kubernetes? 通常我们在开发管理软件项目的时候都会碰到一个很头痛的问题,就是开发.测试.生产环境不一致,导致开发 ...

  3. Visual Studio 2017 for Mac Preview

    Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...

  4. 离线安装Cloudera Manager5.3.4与CDH5.3.4(一)

    这几天一直在安装CDH,头都搞大了,安装第三次,最终成功了. 第一次问题非常多.后面卸载了.由于没有卸载干净导致第二次安装失败. 后来索性重装系统了.直接使用了纯净系统进行安装.一个人跑到学院机房去装 ...

  5. 点击textbox弹出对话框,返回弹出对话框的值

    主要是在父页面使用 function PopupWindow() {            window.open(url, "", "status=no,resizab ...

  6. 解决:IOS viewDidAppear/viewWillAppear无法被调用

    本文转载至 http://my.oschina.net/lvlove/blog/82264   原因: 苹果的文档是这样描述的: If the view belonging to a view con ...

  7. 【BZOJ1115】[POI2009]石子游戏Kam 阶梯博弈

    [BZOJ1115][POI2009]石子游戏Kam Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要 ...

  8. 在做RTSP摄像机H5无插件直播中遇到的对接海康摄像机发送OPTIONS心跳时遇到的坑

    我们在实现一套EasyNVR无插件直播方案时,选择了采用厂家无关化的通用协议RTSP/Onvif接入摄像机IPC/NVR设备,总所周知,Onvif是摄像机的发现与控制管理协议,Onvif用到的流媒体协 ...

  9. HNOI2016

    本蒟蒻表示终于$AC$了$HNOI2016$的六道毒瘤题... 高兴! 附上各个题的题解: $DAY1$: $T1$: BZOJ4537: [Hnoi2016]最小公倍数 $T2$: BZOJ4538 ...

  10. [异步][jms][activeMq]怎样做到重试机制不会导致一条消息被多次运行.

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fei33423/article/details/32723571 淘宝海量存储之单机事务面临的问题及 ...