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 ...
随机推荐
- 【Unity3D】【NGUI】Atlas的动态创建
NGUI版本号:3.6.5 1.參见SZUIAtlasMakerRuntimeTest设置对应的值以上值须要提前设置好 2.没有检查是否atlas可以正确创建,自己可以改,增加返回值 3.代码都是在N ...
- oracle索引INdex
索引是与表相关的一种可选择数据库对象.索引是为提高数据检索的性能而建立,利用它可快速地确定指定的信息. 索引可建立在一表的一列或多列上,一旦建立,由ORACLE自动维护和使用,对用户是完全透明的.然而 ...
- Python学习总结之二 -- 数据类型
带你走进数据类型 一:整数.浮点数 Python中整数和浮点数的定义以及运算和C++都是一样的,我在这里就不需多说了,我就说明一点:Python相对于C/C++而言,定义整数没有int 和 long ...
- 开源监控系统Prometheus介绍
前言 Prometheus是CNCF的一个开源项目,Google BorgMon监控系统的开源版本,是一个系统和服务的监控系统.周期性采集metrics指标,匹配规则和展示结果,以及触发某些条件的告警 ...
- process_thread_action
import psycopg2 import threading conn_fmac = psycopg2.connect(database='filter_useless_mac', user='u ...
- table表格隔行变色
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- ABAP screen
Instance One : SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-100. SELECTION-SCREEN BEGIN ...
- selenium WebDriverException: Message: unknown error: DevToolsActivePort file doesnt exist
在centos中使用无头chrome报以下错误 selenium.common.exceptions.WebDriverException: Message: unknown error: DevTo ...
- 使用electron静默打印
1.使用electron打印的理由 很多情况下程序中使用的打印都是用户无感知的.并且想要灵活的控制打印内容,往往需要借助打印机给我们提供的api再进行开发,这种开发方式非常繁琐,并且开发难度较大. e ...
- node+express上传图片到七牛
本人微信公众号:前端修炼之路,欢迎关注 最近做项目的时候有一个上传图片的需求,由于没有后端的配合,所以决定自己来搭个服务器,实现上传图片功能.以后如果需要修改成java或者php为后端,直接使用即可, ...