主题链接:

http://poj.org/problem?id=1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 11433   Accepted: 4551

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

Source

[Submit]   [Go Back]   [Status]  
[Discuss]

题目意思:

给出每一个学校可以传送信息的学校名单。

求Subtask A:至少要给多少个学校发送源信息,才干是全部学校都收到信息。

求Subtask B:求至少须要加入几个接受信息学校,使得当信息发送给随意一个学校时,都能传送到其它学校。

解题思路:

先求有向图的强连通分量。然后缩点。

统计各点出度和入度。

Subtask A 就是求入度为0的点的个数。

Subtask B就是求max(入度为0个数。出度为零个数)。

把每一个出度为零的节点连到下一颗树的入度为0的节点上。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 110
int low[Maxn],dfn[Maxn],dindex,n;
int sta[Maxn],belong[Maxn],bcnt,ss;
bool iss[Maxn];
int de1[Maxn],de2[Maxn];
vector<vector<int> >myv; void tarjan(int cur)
{
//printf(":%d\n",cur);
//system("pause");
int ne; dfn[cur]=low[cur]=++dindex;
iss[cur]=true;
sta[++ss]=cur; for(int i=0;i<myv[cur].size();i++)
{
ne=myv[cur][i];
if(!dfn[ne])
{
tarjan(ne);
low[cur]=min(low[cur],low[ne]);
}
else if(iss[ne]&&dfn[ne]<low[cur])
low[cur]=dfn[ne];
} if(dfn[cur]==low[cur])
{
bcnt++;
do
{
ne=sta[ss--];
iss[ne]=false;
belong[ne]=bcnt;
}while(ne!=cur);
}
} void solve()
{
int i;
ss=bcnt=dindex=0;
memset(dfn,0,sizeof(dfn));
memset(iss,false,sizeof(iss));
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n))
{
myv.clear();
myv.resize(n+1);
for(int i=1;i<=n;i++)
{
int a;
while(scanf("%d",&a)&&a)
myv[i].push_back(a);
}
solve(); if(bcnt==1) //本来就是一个强连通分量
{
printf("1\n0\n");
continue;
}
int ansa=0,ansb=0; memset(de1,0,sizeof(de1));
memset(de2,0,sizeof(de2)); for(int i=1;i<=n;i++)
{
for(int j=0;j<myv[i].size();j++)
{
int ne=myv[i][j];
if(belong[i]!=belong[ne])
{
de1[belong[ne]]++;//入度
de2[belong[i]]++; //出度
} }
}
for(int i=1;i<=bcnt;i++)
{
if(!de1[i])
ansa++;
if(!de2[i])
ansb++;
}
ansb=max(ansb,ansa); printf("%d\n%d\n",ansa,ansb);
}
return 0;
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

[tarjan] poj 1236 Network of Schools的更多相关文章

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

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

  2. Poj 1236 Network of Schools (Tarjan)

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

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

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

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

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

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

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

  6. POJ 1236 Network of Schools(Tarjan缩点)

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

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

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

  8. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  9. POJ 1236 Network of Schools (Tarjan)

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

随机推荐

  1. 异常:ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed...

    ERROR [org.hibernate.proxy.BasicLazyInitializer] - CGLIB Enhancement failed: com.movie.类 放到lib 包下 \W ...

  2. 清除mac上安装软件的用户信息

    有时候在mac系统上安装了一些软件后,尽管你将该软件卸载之后,可是原来的登录信息依然存在, 那么你就可以到下面的这个目录中查看一下,是否残留有信息文件.

  3. 从一段代码看fork()函数及其引发的竞争

    首先来看一段从<UNIX环境高级编程>中摘录的一段很有意思的代码.借此我们再来谈谈fork()函数的一些问题. #include "apue.h" static voi ...

  4. LR实战之Discuz开源论坛——登录脚本

    脚本业务流:访问Discuz论坛首页——登录论坛——退出论坛.本次使用LoadRunner11版本. 一.录制脚本注意 1.确保Discuz论坛能在服务器运行正常. 2.录制前先试访问Discuz论坛 ...

  5. DNS,ARP,RARP,NAT,WINS的作用和区别

    DNS 域名服务系统,是将域名(比如www.cnblogs.com)转成ip地址.arp 地址转换协议,是将ip地址转成mac地址(物理地址,可用ipconfig /all查看).rarp从mac转到 ...

  6. UILabel + 导入字体

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 300, 100)]; 1.设置文字颜色 label.textC ...

  7. 【Remoting-4】

    [服务对象三种激活方式的不同] [1]客户端激活方式 [A]对象的创建,对象方法的执行都是在远程服务端. [B]服务端为每一个客户端创建其专属的对象,为这个客户提供服务,并且保存状态 [C]可以从远程 ...

  8. 小项目--反eclass

    前言—— 最近会把前一段时间闲的无聊写的一些很小的项目写一些博客,用来练练手. 引子—— 最近班里有个很讨厌的软件,,,,教育局规定每个学校要上传多媒体使用记录,所以学校就给班里每台电脑上装了一个比较 ...

  9. 移动端-弹窗demo

    <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name ...

  10. DedeCms 建站随笔(一)

    站名:524社区网 代码:DedeCms织梦5.7代码(UTF-8) 服务器:Linux 问题一:火车头(熊猫)采集,登录成功之后无法获取栏目列表. 原因:1.新建栏目后没有更新栏目HTML文件,并且 ...