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 ...
随机推荐
- JS基础:正则表达式
简介 正则表达式 (regular expression) 描述了一种字符串匹配的模式,可以用来检查一个字符串是否含有某种子串.将匹配的子串做替换或者从某个字符串中取出符合某个条件的子串等.在 JS ...
- 【BZOJ4710】分特产(容斥原理,组合计数)
题意:有m种特产,第i种有a[i]个 有n个同学分特产,要求: 1.恰好分完 2.每个人至少要分到一个 求方案数模10^9+7 n,m,a[i]<=1000 思路:WYZ作业 首先考虑对于每一种 ...
- msp430入门编程11
msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程
- C# 图片识别(支持21种语言)转
来自:http://www.cnblogs.com/stone_w/archive/2011/10/08/2202397.html 图片识别的技术到几天已经很成熟了,只是相关的资料很少,为了方便在此汇 ...
- Android多线程研究(3)——线程同步和相互排斥及死锁
为什么会有线程同步的概念呢?为什么要同步?什么是线程同步?先看一段代码: package com.maso.test; public class ThreadTest2 implements Runn ...
- ubuntu Install Firefox
Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox,给每个版本的 Firefox 创建一个单独的文件夹. 例如:1.Firefox 版 ...
- Android内存泄露之开篇
先来想这三个问题 内存泄露是怎么回事 内存会泄露的原因 避免内存泄露 1.内存泄露怎么回事 一个程序中,已经不须要使用某个对象,可是由于仍然有引用指向它垃圾回收器就无法回收它,当然该对象占用的内存就无 ...
- C语言++a与a++的实现机制与操作符结合优先级
看到一道"经典Linux C"面试题,关于左值和右值的. 华为笔试题 1.写出推断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值(3分) int a = 4; ...
- MySql command line client 命令系列
—————————————————————————————————————————————————————————— 一.启动与退出 1.进入MySQL:启动MySQL Command Line Cl ...
- 从零開始学android<Bitmap图形组件.四十七.>
android.graphics.Bitmap(位图)是Android手机中专门提供的用于操作图片资源的操作类,使用此类能够直接从资源文件之中进行图片资源的读取.而且对这些图片进行一些简单的改动. 经 ...