POJ1236 Network of Schools

文章目录

  • Description
  • 题意:
  • 题解:
  • 代码:

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

题意:

给你一些点的连接关系(有向边),具有传递性
然后有两个任务
任务A:
至少要选择多少个点,通过边进行传递,可以经过所有边
任务B:
要使所有点都可以彼此到达,最少要添加多少有向边

题解:

并查集应该可以做吧(但我并没有用)
我们用tarjan来做
我们先来分析和整合题意:
一个强连通分块里面是可以相互到达的,我们可以当做一个点来处理
任务A:
我们可以进行tarjan缩点,如果一个点入度为0,也就是没有其他点指向他,我们就必须选择他,否则这样的点就不会被遍历,那么这个点就是满足要求的
任务B:
其实就是问最少添加几个边可以使得整个图变成强连通图,
如果一个图是强连通图,里面所有点可以互达,就说明每个点入度出度均大于0.
那么缩点后,我们就统计入度为0的A类点数与出度为0的B类点数,只要从B类点生成一条指向A类点的有向线段即可,所以求这两类点的较大值
总结就是:
Tarjan+强连通分量+缩点
代码有详细讲解

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstdio>
using namespace std;
int n,head[10005],cnt=0,col=0,sum;
int top=1,stack[10005],color[10005],dfn[10005],low[10005],vis[10005];//color记录染色后点的颜色。
int in[10005],out[10005];//in和out分别是入度和出度。
struct net
{int to,next;
}e[1000000];
void add(int x,int y)
{cnt++;e[cnt].to=y;e[cnt].next=head[x];head[x]=cnt;
}
void tarjian(int x)
{sum++;dfn[x]=low[x]=sum;stack[top]=x;top++;vis[x]=1;for(int w=head[x];w!=0;w=e[w].next){if(vis[e[w].to]==0){tarjian(e[w].to);low[x]=min(low[x],low[e[w].to]);}else if(vis[e[w].to]==1)low[x]=min(low[x],dfn[e[w].to]);}if(dfn[x]==low[x]){col++;do{top--;color[stack[top]]=col;vis[stack[top]]=-1;}while(stack[top]!=x);}//常规的tarjan缩点 模板 return ;
}
int hym[1000000][3];
int main()
{int i,k=0;cin>>n;for(i=1;i<=n;i++){int x;cin>>x;while(x!=0){k++;add(i,x);hym[k][1]=i;//1表示这条边的始点,2表示这条边的终点 hym[k][2]=x;//记下边,方便之后统计入度和出度。scanf("%d",&x);}}for(i=1;i<=n;i++)if(!vis[i]) tarjian(i);for(i=1;i<=k;i++)if(color[hym[i][1]]!=color[hym[i][2]])//如果相等说明这个点在同一个强连通分量里,否则就是两个强连通分量之间的边//那我们分别计这个边两端的入度和出度 {out[color[hym[i][1]]]++;//始点记出度 in[color[hym[i][2]]]++;//终点记入度 }//同种颜色不需要统计,不同颜色更改出度和入度。int ans1=0,ans2=0;for(i=1;i<=col;i++){//cout<<in[i]<<' '<<out[i]<<endl;if(in[i]==0)  ans1++;if(out[i]==0) ans2++;//寻找出度和入度为0的点数 }if(col==1) cout<<1<<endl<<0;elsecout<<ans1<<endl<<max(ans1,ans2); return 0;
}

POJ1236 Network of Schools相关推荐

  1. POJ1236 Network of Schools【强连通】

    题意: N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件 ...

  2. POJ-1236 Network of Schools 缩点

    题意:就是给我们一个网络 让我们求 1 选择最少的点传信 能够使得这个信息传遍整个网络 2 求加的最少的边 使得 加上这些边后整个图任取一个点信息就可以传到网络中任何一个店 分析: 对于1问 可以用t ...

  3. POJ 1236 Network of Schools(tarjan)

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

  4. 【缩点】解题报告:luogu P2746 [USACO5.3]校园网Network of Schools(有向图、强连通分量、缩点)

    题目链接:P2746 [USACO5.3]校园网Network of Schools 用tarjan算法求出强连通分量,并且缩点,如果缩点后只有一个点,则答案为1,0 对于第一问,如果缩点后某一点的入 ...

  5. [tarjan] poj 1236 Network of Schools

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

  6. 洛谷 P2746 [USACO5.3]校园网Network of Schools (Tarjan,SCC缩点,DAG性质)

    P2746 [USACO5.3]校园网Network of Schools https://www.luogu.org/problem/P2746 题目描述 一些学校连入一个电脑网络.那些学校已订立了 ...

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

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

  8. Network of Schools(POJ-1236)

    Problem Description A number of schools are connected to a computer network. Agreements have been de ...

  9. poj1236/luogu2746 Network of Schools (tarjan)

    tarjan缩点后,第一问答案显然是入度为零的点得个数 第二问:考虑到 没有入度或出度为0的点 的图强连通, 所以答案就是max{入度为零的个数,出度为零的个数} (把出度为零的连到入度为零的点,然后 ...

最新文章

  1. 4G EPS 中的消息类型
  2. 1-3_基本概念_程序进程
  3. App乱世,3721离我们有多远
  4. ​年底大会火爆,看“瑶台”如何搭建一场高质量沉浸式大会
  5. JavaScript中的原型prototype及原型链
  6. 整数反转—leetcode7
  7. java string 字符个数字_java从字符串中提取数字
  8. Linux找最大最小值的命令,Linux中awk命令正确的求最大值、最小值、平均值、总和...
  9. gdal - ogr简单对象模型
  10. 管道符和作业控制 shell变量 环境变量配置文件
  11. Destoon源数据库配置文件在哪_数据库监控软件Lepus安装部署详解
  12. javascript 常用的数组操作
  13. oppo手机维语字体_OPPO手机中字体设置在哪里 OPPO手机中字体设置方法
  14. 在设计四人抢答器中灯全亮_四人智力竞赛抢答器最终版(资料4)
  15. pr_debug打印输出
  16. 三大web服务器对比(lighttpd,apache,nginx)
  17. 一维信号小波去噪原理及python实现示例
  18. 2个步骤让你秒获KOL抖音运营数据分析报告
  19. dedecms mytag_js.php,一种奇特的DEDE隐藏后门办法_91Ri.org
  20. 雅虎创始人--杨致远

热门文章

  1. 在家做什么能让隔壁报警?| 今日最佳
  2. 撤回的微信消息真的看不到?78行Python代码帮你看穿一切!
  3. 一位像素艺术家用39张动图,将大自然的唯美尽收眼底…
  4. 再生希尔伯特空间_向量、函数向量、再生核希尔伯特空间、核技巧
  5. mysql安装配置yum_在CentOS 7下使用yum配置MySQL源并安装MySQL
  6. n以内的素数c语言,关于求N以内素数的一点小问题(N小于一亿)
  7. wordpress home.php,WordPress主题通过function.php来加载js和css文件
  8. 怎么打包图片_超简单的免费批量图片压缩技巧,只需3步
  9. access 战地1不加入ea_炒牛肉时,想要牛肉嫩滑又不老,只需加入1样东西,很多人都不懂...
  10. leetcode347. 前 K 个高频元素