POJ1144(割点入门题)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11378 | Accepted: 5285 |
Description
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
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
Output
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(割点入门题)的更多相关文章
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- poj 2524:Ubiquitous Religions(并查集,入门题)
Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23997 Accepted: ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- 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 ...
- poj 3254 状压dp入门题
1.poj 3254 Corn Fields 状态压缩dp入门题 2.总结:二进制实在巧妙,以前从来没想过可以这样用. 题意:n行m列,1表示肥沃,0表示贫瘠,把牛放在肥沃处,要求所有牛不能相 ...
- zstu.4194: 字符串匹配(kmp入门题&& 心得)
4194: 字符串匹配 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 206 Solved: 78 Description 给你两个字符串A,B,请 ...
- hrbustoj 1073:病毒(并查集,入门题)
病毒Time Limit: 1000 MS Memory Limit: 65536 KTotal Submit: 719(185 users) Total Accepted: 247(163 user ...
- hdu 1465:不容易系列之一(递推入门题)
不容易系列之一 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
基础数据结构——顺序表(2) Time Limit: 1000 MS Memory Limit: 10240 K Total Submit: 355(143 users) Total Accep ...
随机推荐
- 深入浅出Attribute(三)
约定: 1.”attribute”和”attributes”均不翻译 2.”property”译为“属性” 3.msdn中的原句不翻译 4.”program entity”译为”语言元素” Attri ...
- struts2 文件上传和下载,以及部分源代码解析
struts2 文件上传 和部分源代码解析,以及一般上传原理 (1) 单文件上传 一.简单介绍 Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form ...
- ios开发之猜数字游戏
// // main.m // 猜数 // #import <Foundation/Foundation.h> #import "Guess.h" int main(i ...
- U-Boot中关于TEXT_BASE,代码重定位,链接地址相关说明
都知道U-BOOT分为两个阶段,第一阶段是(~/cpu/arm920t/start.S中)在FLASH上运行(一般情况 下),完成对硬件的初始化,包括看门狗,中断缓存等,并且负责把代码搬移到SDRAM ...
- Android之怎样全屏显示
三种方法: 1 自己定义主题(见设置自己定义样式和主题一节) http://blog.csdn.net/wei_chong_chong/article/details/47438907 2 使用系统自 ...
- php类中const
常量 const 在类里面定义常量用 const 关键字,而不是通常的 define() 函数. 语法: const constant = "value"; 例子: <?ph ...
- Android API Guides---Storage Access Framework
存储訪问架构 Android 4.4系统(API级别19)推出存储訪问框架(SAF).新加坡武装部队变得很easy,为用户在其全部自己喜欢的文件存储提供商的浏览和打开文档,图像和其它文件.一个标准的, ...
- 流畅python学习笔记:第十四章:迭代器和生成器
迭代器和生成器是python中的重要特性,本章作者花了很大的篇幅来介绍迭代器和生成器的用法. 首先来看一个单词序列的例子: import re re_word=re.compile(r'\w+') c ...
- Error: Failed to fetch plugin E:_My_File______workMyCodemyCodecordova-workspaceplugman-testMyMath via registry. Probably this is either a connection problem, or plugin spec is incorrect.
$ cordova plugin add E:\_My_File_____\_work\MyCode\myCode\cordova-workspace\plugman-test\MyMath --sa ...
- java.sql.SQLException: Access denied for user 'somebody'@'localhost' (using password: YES)
用mybatis和spring整合时出现了一个错误: 我是在IntelliJ IDEA上整合Mybatis和Spring的,运行测试用例出现了如上错误. 红色的马赛克部分是我的名字. 问题是,我的数据 ...