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 ...
随机推荐
- MagicZoom bug-Strict Standards: Only variables should be assigned by reference Error
问题:zencart Strict standards: Only variables should be assigned by reference in jscript_zen_magiczoo ...
- Android 音频 OpenSL ES 录音 采集
1,; int channelConfig = AudioFormat.CHANNEL_OUT_STEREO; int audioFormat = AudioFormat.ENCODING_PCM_1 ...
- win本地配置docker环境
先上官网链接:https://docs.docker.com/get-started/part2/#introduction 优质入门教程:http://www.docker.org.cn/book/ ...
- JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程
概述 JBossWeb 是JBoss 中的 Web 容器.他是对 Tomcat 的封装,本文以 Http 连接器为例.简单说明 JBossWeb/Tomcat 初始化连接器和处理 Http 请求过程 ...
- 怎么用cookie解决选项卡问题刷新后怎么保持原来的选项?
什么是cookie? Cookies虽然一般都以英文名呈现,但是它还是有一个可爱的中文名“小甜饼”.Cookies是指服务器暂存放在你的电脑里的txt格式的文本文件资料,主要用于网络服务器辨别电脑使用 ...
- NotePad++ 正则表达式替换
NotePad++ 正则表达式替换 高级用法 [转] - aj117 - 博客园 https://www.cnblogs.com/tibit/p/6387199.html const getQAPar ...
- Java线程池的配置
1.ThreadPoolExecutor的重要参数 1.corePoolSize:核心线程数 * 核心线程会一直存活,及时没有任务需要执行 * 当线程数小于核心线程数时,即使有线程空闲,线程池也会优先 ...
- SQL join中级篇--hive中 mapreduce join方法分析
1. 概述. 本文主要介绍了mapreduce框架上如何实现两表JOIN. 2. 常见的join方法介绍 假设要进行join的数据分别来自File1和File2. 2.1 reduce side jo ...
- Java基础知识整理之static修饰属性
static 关键字,我们在开发用的还是比较多的.在<Java编程思想>有下面一段话 static 方法就是没有 this 的方法.在 static 方法内部不能调用非静态方法,反过来是可 ...
- UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组
题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...