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. Proving Equivalences (hdu 2767 强联通缩点)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. 深入浅出Attribute(二)

    上篇里,我们把Attribute“粘”在类的成员方法上show了一把,让Attribute跟大家混了个脸儿熟.中篇里,我们将探讨“究竟什么是Attribute”和“如何创建及使用Attribute”这 ...

  3. 渐变背景(background)效果

    chrom and Safari浏览器: webkit核心的浏览器.使用CSS3渐变方法(css-gradient) -webkit-gradient(type, start_point, end_p ...

  4. linux SPI驱动——spidev之deive(五)

    1.定义board设备 1: struct spi_board_info { 2: /* the device name and module name are coupled, like platf ...

  5. Ubuntu/CentOS下编译Nginx最基本参数

    Ubuntu/CentOS下编译Nginx安装基本参数,做个记录: groupadd www useradd -g www www ./configure --user=www --group=www ...

  6. 目标检测之vibe---ViBe(Visual Background extractor)背景建模或前景检测

    ViBe算法:ViBe - a powerful technique for background detection and subtraction in video sequences 算法官网: ...

  7. mac os x升级MOUNTAIN LION后svn command not found的解决

    因为svn是个开发工具 所以苹果把他移到 Xcode developer package 里 去了,所以你没装xcode之类的,先去AppStore把xcode装了   装好之后sudo vi /et ...

  8. [Android基础]Android中使用HttpURLConnection

    HttpURLConnection继承了URLConnection,因此也能够向指定站点发送GET请求.POST请求.它在URLConnetion的基础上提供了例如以下便捷的方法. int getRe ...

  9. 九度OJ 1176:树查找 (完全二叉树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5209 解决:2193 题目描述: 有一棵树,输出某一深度的所有节点,有则输出这些节点,无则输出EMPTY.该树是完全二叉树. 输入: 输入 ...

  10. 我的Java开发学习之旅------>Java使用ObjectOutputStream和ObjectInputStream序列号对象报java.io.EOFException异常的解决方法

    今天用ObjectOutputStream和ObjectInputStream进行对象序列化话操作的时候,报了java.io.EOFException异常. 异常代码如下: java.io.EOFEx ...