Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16806   Accepted: 6643

Description

A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B 
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.

Input

The first line contains an integer N: the number of schools in the network (2 <= N <= 100). The schools are identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write two lines to the standard output. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

题目链接:POJ 1236

由于数据结构老师说期末成绩跟刷题数量有关(有毒),然后就去随便翻了翻校内OJ里会做的题,然后突然发现有一道题跟这题非常类似,细细查看发现题面就TM是中文翻译版本而已,而且翻译的也不好,机翻水平…………。然后时隔几个礼拜就又来做了一次这道题,这次就感觉理解更加深入了。

题目中你其实是一个超级源点,学校编号是1~n,各自均有单向边连向其他点,第一个问题是问你需要发多少个软件包来使得所有学校都可以直接、间接地获得你发的软件包;第二个问题是假如你只发一个软件包,那需要加多少条边来使得所有学校都被传达到。

对于问题一,可以发现只要一个点有入边(入度不为0),则说明他肯定可以被其他学校传递到,显然你找到入度为0的点(即没其他学校给他发软件包的点)所有点就是你要发的学校集合了,如果倒着想,不给这些点发的话肯定是无法到达的,至少这个点肯定是收不到软件包的。

对于问题二,其实问题可以转化成一个有向图(极端情况下为多个强连通分量)加多少条边变成一个强连通分量,先Tarjan缩点可以发现把边加在叶子那里效果是最好的,假如每一个人都可以传递一个能量给你,那么就需要这么一条边把尽量多的能量传出去且这样边数越少越好,显然叶子聚集的能量是最多的,最好就是从叶子连出来边加回根处,形成环,不过当强连通分量只有一个的时候就不用加了,要输出0。

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
struct edge
{
int to,nxt;
};
edge E[N*N*N];
int head[N],tot;
int dfn[N],low[N],belong[N],scc,ts,top,st[N];
int in[N],out[N];
bitset<N> ins; void init()
{
CLR(head,-1);
tot=0;
CLR(dfn,0);
CLR(low,0);
CLR(belong,0);
scc=ts=top=0;
CLR(in,0);
CLR(out,0);
ins.reset();
}
inline void add(int s,int t)
{
E[tot].to=t;
E[tot].nxt=head[s];
head[s]=tot++;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++ts;
st[top++]=u;
ins[u]=1;
int i,v;
for (i=head[u]; ~i; i=E[i].nxt)
{
v=E[i].to;
if(!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
++scc;
do
{
v=st[--top];
ins[v]=0;
belong[v]=scc;
}while (u!=v);
}
}
int main(void)
{
int n,a,b,i,j;
while (~scanf("%d",&n))
{
init();
for (i=1; i<=n; ++i)
{
while (scanf("%d",&b)&&b)
add(i,b);
}
for (i=1; i<=n; ++i)
if(!dfn[i])
Tarjan(i);
for (a=1; a<=n; ++a)
{
for (j=head[a]; ~j; j=E[j].nxt)
{
int b=E[j].to;
if(belong[a]!=belong[b])
{
++out[belong[a]];
++in[belong[b]];
}
}
}
int leaf=0,root=0;
for (i=1; i<=scc; ++i)
{
if(!in[i])
++root;
if(!out[i])
++leaf;
}
printf("%d\n%d\n",root,scc==1?0:max(root,leaf));
}
return 0;
}

POJ 1236 Network of Schools(Tarjan缩点)的更多相关文章

  1. POJ 1236 Network of Schools Tarjan缩点

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22729   Accepted: 89 ...

  2. POJ 1236 Network of Schools (Tarjan + 缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  3. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  4. POJ 1236 Network of Schools 连通图缩点

    题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...

  5. POJ 1236 Network of Schools —— (缩点的应用)

    题目大意:有N个学校和一些有向边将它们连结,求: 1.最少需要向几个学校发放软件,使得他们中的每一个学校最终都能够获得软件. 2.最少需要增加几条有向边使得可以从任意一个学校发放软件,使得每一个学校最 ...

  6. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  7. POJ 1236 Network of Schools(强连通分量)

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  8. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  9. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  10. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

随机推荐

  1. ReactiveCocoa源码拆分解析(四)

    (整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 上一章节简要的说明了如何实现的热信号.但是像那么写, ...

  2. CentOS 7.0,启用iptables防火墙

    CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止fir ...

  3. SQLServer 事务隔离级别与锁的申请和释放

    脏读:当一个事务开始更新数据,但是这个事务并没有完全提交,这个时候第二个事务开始读取数据,把第一个事务所更改的数据读了出来, 第二个事务读取的数据时临时的,因为有可能第一个事务最终有可能做回滚操作 不 ...

  4. request \response 总结

    request&response request 1.获得信息的方法     1> 获得请求首行信息的方法         *getMethod         *getContextP ...

  5. iOS compare 字符串比较

    NSString 比较字符串,我介绍一些常用的方法: NSString *value = @"1234567890"; 比较的方法: [value compare:(NSStrin ...

  6. Lattice FPGA 板子 调试笔记

    最近在调试LATTICE  FPGA 做的视频板子,颇不顺利,所以记录下来作为以后的参考: 1.FPGA的IO口不是所有的都是双向的,有些有特殊作用的是单向的. 在查阅 LatticeECP3-17E ...

  7. tensorflow的安装

    binary安装(推荐) 注意需要能访问外网 Install pip (or pip3 for python3) if it is not already installed: # Ubuntu/Li ...

  8. Sql Server 中锁的概念

    锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统 脏 ...

  9. 以前用SQL实现的机构职能树,再看看

    DECLARE CNT NUMBER; TMP ); vtmp ); ltmp1 ); ltmp2 ); ltmp3 ); ltmp4 ); NTMP NUMBER; CURSOR CSR_TEST ...

  10. 在Jenkins中获取GitHub对应Repository的Resource Code

    1):Install Jenkins 请看如下链接: https://wiki.jenkins-ci.org/display/JENKINS/Installing+Jenkins 2):Install ...