Network(特殊的输入格式+tarjan求割点模板题)
2015-10-08 12:28
274 查看
Link:http://poj.org/problem?id=1144
题意:对于输入格式,好好理解这段话( 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.)。知道如何构图后,剩下的就是要求无向图的割点了。
编程思想:tarjan求割点模板。
求割点算法原理:
求割点。判断一个点是否是割点有两种判断情况:
如果u为割点,当且仅当满足下面的1条
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。
然后根据这两句来找割点就可以了。
AC code:
#include <iostream>
#include <cmath>
#include<stdlib.h>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#define LL long long
using namespace std;
#define INF 0x7fffffff
#define N 50100
//N为最大点数
#define M 150100
//M为最大边数
int n, m;//n m 为点数和边数
struct Edge{
int from, to, nex,w;
bool sign;//是否为桥
}edge[M<<1];
int head
, edgenum;
void add(int u, int v,int w){//边的起点和终点
Edge E={u, v, head[u], w,false};
edge[edgenum] = E;
head[u] = edgenum++;
}
int DFN
, Low
, Stack
, top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
int taj;//连通分支标号,从1开始
int ge
;//ge[i]!=0表示i为割点
int ans;
void tarjan(int u ,int fa)
{
DFN[u] = Low[u] = ++ Time ;
int son=0;
for (int i = head[u] ; ~i ; i = edge[i].nex )
{
int v = edge[i].to ;
son++;
if(DFN[v] == -1)
{
tarjan(v , u) ;
Low[u] = min(Low[u] ,Low[v]) ;
/*如果u为割点,当且仅当满足下面的1条:
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。*/
if(u==1&&son>1||DFN[u]<=Low[v]&&u!=1)
{
ge[u]++;
}
}
else if(v!=fa) Low[u] = min(Low[u] ,DFN[v]) ;
}
}
void init()
{
memset(head, -1, sizeof(head));
edgenum=0;
Time=0;
ans=0;
memset(DFN,-1,sizeof(DFN));
memset(ge,0,sizeof(ge));
}
int main()
{
//freopen("D:\\in.txt","r",stdin);
int u,v,ww,i,j,k,T;
//scanf("%d",&T);
while(scanf("%d",&n)!=EOF&&n)
{
init();
while(scanf("%d",&u)!=EOF&&u)
{
while(getchar()!='\n')
{
scanf("%d",&v);
add(u,v,0);
add(v,u,0);
}
}
tarjan(1,-1);
for(i=1;i<=n;i++)
{
if(ge[i])
ans++;
}
printf("%d\n",ans);
}
return 0;
}
Network
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 Hint You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line. Source Central Europe 1996 |
编程思想:tarjan求割点模板。
求割点算法原理:
求割点。判断一个点是否是割点有两种判断情况:
如果u为割点,当且仅当满足下面的1条
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。
然后根据这两句来找割点就可以了。
AC code:
#include <iostream>
#include <cmath>
#include<stdlib.h>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<algorithm>
#define LL long long
using namespace std;
#define INF 0x7fffffff
#define N 50100
//N为最大点数
#define M 150100
//M为最大边数
int n, m;//n m 为点数和边数
struct Edge{
int from, to, nex,w;
bool sign;//是否为桥
}edge[M<<1];
int head
, edgenum;
void add(int u, int v,int w){//边的起点和终点
Edge E={u, v, head[u], w,false};
edge[edgenum] = E;
head[u] = edgenum++;
}
int DFN
, Low
, Stack
, top, Time; //Low[u]是点集{u点及以u点为根的子树} 中(所有反向弧)能指向的(离根最近的祖先v) 的DFN[v]值(即v点时间戳)
int taj;//连通分支标号,从1开始
int ge
;//ge[i]!=0表示i为割点
int ans;
void tarjan(int u ,int fa)
{
DFN[u] = Low[u] = ++ Time ;
int son=0;
for (int i = head[u] ; ~i ; i = edge[i].nex )
{
int v = edge[i].to ;
son++;
if(DFN[v] == -1)
{
tarjan(v , u) ;
Low[u] = min(Low[u] ,Low[v]) ;
/*如果u为割点,当且仅当满足下面的1条:
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。*/
if(u==1&&son>1||DFN[u]<=Low[v]&&u!=1)
{
ge[u]++;
}
}
else if(v!=fa) Low[u] = min(Low[u] ,DFN[v]) ;
}
}
void init()
{
memset(head, -1, sizeof(head));
edgenum=0;
Time=0;
ans=0;
memset(DFN,-1,sizeof(DFN));
memset(ge,0,sizeof(ge));
}
int main()
{
//freopen("D:\\in.txt","r",stdin);
int u,v,ww,i,j,k,T;
//scanf("%d",&T);
while(scanf("%d",&n)!=EOF&&n)
{
init();
while(scanf("%d",&u)!=EOF&&u)
{
while(getchar()!='\n')
{
scanf("%d",&v);
add(u,v,0);
add(v,u,0);
}
}
tarjan(1,-1);
for(i=1;i<=n;i++)
{
if(ge[i])
ans++;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- Native.loadLibrary Caused by: java.lang.UnsatisfiedLinkError: Unable to load library
- java swing中Jtable+JScrollpane实现自动滚动到底部——测试真实有效
- Druid连接池及监控在Spring配置如下:
- Markdown学习
- php 常用函数
- 【转】Android布局优化之ViewStub
- Contains Duplicate
- 【转】Android布局优化之ViewStub
- CPU 材料学才是最顶级的学科
- LeetCode290 Word Pattern
- Android 使用 9.png 为 background 时 产生位移
- 港港
- 在ssm框架中前后台数据交互均使用json格式
- Java基础知识强化之网络编程笔记08:TCP之客户端键盘录入服务器控制台输出
- Apache的AB测试介绍
- Apache的AB测试介绍
- 嫁给程序员的十大好处
- 工厂方法模式和抽象工厂模式
- nyoj 最大和 104 (二维子矩阵 DP) 好题
- C#写快速排序