Poj 1236 Network of Schools (Tarjan)
题目链接:
题目描述:
有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个网络才能够让所有学校被网络覆盖?2:至少要加几条线路就能做到在任意一个学校安装网络都可以覆盖全部学校?
解题思路:
先用Tarjan对强连通分量进行缩点,然后对缩点以后的图进行处理,统计图中节点出度为零的有多少,入度为零的有多少个?
因为入度为零的点不能由其他的点到达,在每个入度为零的节点安装网络可以让所有学校被网络覆盖。
全部学校在一个强连通图里面,就可以实现第二问,因为出度为零的节点不能到达其他节点,入度为零的点不能由其他节点进来,所以要对出度为零加出度,入度为零的便加入度即可。PS:当全图只有一个连通块的时候应该不用加边的。
#include <cstring>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = ;
struct node
{
int to ,next;
} edge[maxn*maxn];
int head[maxn], dfn[maxn], low[maxn], in[maxn], out[maxn], id[maxn];
int tot, cnt, top, time, instack[maxn], stack[maxn];
void init ()
{
tot = top = ;
cnt = time = ;
memset (head, -, sizeof(head));
memset (dfn, , sizeof(dfn));
memset (low, , sizeof(low));
memset (in, , sizeof(in));
memset (out, , sizeof(out));
memset (id, , sizeof(id));
memset (instack, , sizeof(instack));
memset (stack, , sizeof(stack));
}
void Add (int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot ++;
}
void Tarjan (int u)
{
dfn[u] = low[u] = ++time;
instack[u] = ;
stack[top ++] = u;
for (int i=head[u]; i!=-; i=edge[i].next)
{
int v = edge[i].to;
if (!dfn[v])
Tarjan (v);
if (instack[v])
low[u] = min (low[u], low[v]);
}
if (dfn[u] == low[u])
{
cnt ++;
while ()
{
int v = stack[--top];
instack[v] = ;
id[v] = cnt;
if (v == u)
break;
}
}
}
int main ()
{
int n;
while (scanf ("%d", &n) != EOF)
{
init ();
for (int i=; i<=n; i++)
{
int to;
while (scanf ("%d", &to), to)
Add (i, to);
}
for (int i=; i<=n; i++)
if (!dfn[i])
Tarjan (i);
for (int i=; i<=n; i++)
for (int j=head[i]; j!=-; j=edge[j].next)
{
int v = id[i];
int u = id[edge[j].to];
if (v != u)
{
out[v] ++;
in[u] ++;
}
}
int In, Out;
In = Out = ;
for (int i=; i<=cnt; i++)
{
if (!in[i])
In ++;
if (!out[i])
Out ++;
}
if (cnt == )
printf ("1\n0\n");
else
printf ("%d\n%d\n", In, max(In, Out));
}
return ;
}
Poj 1236 Network of Schools (Tarjan)的更多相关文章
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ 1236 Network of Schools Tarjan缩点
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22729 Accepted: 89 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- poj 1236 Network of Schools(tarjan+缩点)
Network of Schools Description A number of schools are connected to a computer network. Agreements h ...
随机推荐
- hdu 4770 状压+枚举
/* 长记性了,以后对大数组初始化要注意了!140ms 原来是对vis数组进行每次初始化,每次初始化要200*200的复杂度 一直超时,发现没必要这样,直接标记点就行了,只需要一个15的数组用来标记, ...
- poj1984并查集的相对偏移
#include<stdio.h>//典型题 #include<math.h> #define N 40010 struct node { int x,y,z; }pre[N] ...
- Network-POJ3694(最小公共祖先LCA+Tarjin)
http://poj.org/problem?id=3694 这一题 为什么要找最小祖先呢 当两个节点连到一块的时候 找最小公共节点就相当于找强连通分支 再找最小公共节点的过程中直到找到 这个过 ...
- HDU——2647 Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- Eclipse的SVN插件 Subclipse
原文:https://www.oschina.net/p/subclipse Subclipse 是一个为 Eclipse IDE 添加 Subversion 支持的项目.支持几乎所有版本的Eclip ...
- 【转】深入理解javascript作用域——词法作用域和动态作用域
前面的话 大多数时候,我们对作用域产生混乱的主要原因是分不清楚应该按照函数位置的嵌套顺序,还是按照函数的调用顺序进行变量查找.再加上this机制的干扰,使得变量查找极易出错.这实际上是由两种作用域工作 ...
- Ubuntu下Zabbix安装及使用问题
1.configure: error: MySQL library not found MySQL library not found root@kallen:~# apt-get install l ...
- 【Struts2五】ValueStack以及ognl表达式二(经常使用标签)
Ognl经常使用标签: 1.s:debug 假设把该标签放入到s:iterator中能够看到当前正在迭代的元素的状态 2.s:property 1.输出 ...
- PHP 给图片加边框
/** * 给图片加边框 by liangjian 2014-06-19 * @param $ImgUrl 图片地址 * @param $SavePath 新图片保存路径 * @param $px 边 ...
- easyUI datagrid 前端假分页
datagrid有两种分页方式,真分页和假分页. 所谓真分页,就是真的每次只获取一张分页的数据. 所谓假分页,就是将所有数据全部获取下来,然后利用其分页控件进行分页. 下面具体说说假分页: 1.dat ...