Network of Schools

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
给定一个有向图,求:

 

1) 至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点

 

2) 至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点

 

—        顶点数<= 100

解题思路:

—        1. 求出所有强连通分量

—        2. 每个强连通分量缩成一点,则形成一个有向无环图DAG

—        3. DAG上面有多少个入度为0的顶点,问题1的答案就是多少

在DAG上要加几条边,才能使得DAG变成强连通的,问题2的答案就是多少

加边的方法:

要为每个入度为0的点添加入边,为每个出度为0的点添加出边

假定有 n 个入度为0的点,m个出度为0的点,如何加边?

把所有入度为0的点编号 0,1,2,3,4 ....N -1

每次为一个编号为i的入度0点可达的出度0点,添加一条出边,连到编号为(i+1)%N 的那个出度0点,

这需要加n条边

若 m <= n,则

加了这n条边后,已经没有入度0点,则问题解决,一共加了n条边

若 m > n,则还有m-n个入度0点,则从这些点以外任取一点,和这些点都连上边,即可,这还需加m-n条边。

所以,max(m,n)就是第二个问题的解

此外:当只有一个强连通分支的时候,就是缩点后只有一个点,虽然入度出度为0的都有一个,但是实际上不需要增加清单的项了,所以答案是1,0
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
typedef long long ll;
using namespace std;
const int N = ;
const int M = ;
int vis[N],dfn[N],low[N],head[N],stack1[N],num[N],out[N],in[N];
int cost[N];
int n,m,tot,son,maxn,tim,top,cut;
int ans;
struct EDG{int to,next;}edg[N*N];
struct node{ll x,y,r,c;}a[N];
bool cmp(node f,node g){return f.c<g.c;}
void add(int u,int v){
edg[tot].to=v;edg[tot].next=head[u];head[u]=tot++;
}
void init(){
met(head,-);
tot=tim=top=cut=;
met(vis,);
met(edg,);
met(out,);met(in,);
met(cost,inf);
met(stack1,);met(num,);met(dfn,);met(low,);
}
void Tarjan(int u) {
int v;
low[u] = dfn[u] = ++tim;
stack1[top++] = u;
vis[u] = ;
for(int e = head[u]; e != -; e = edg[e].next){
v = edg[e].to;
if(!dfn[v]){
Tarjan(v);
low[u] = min(low[u], low[v]);
}else if(vis[v]){
low[u] = min(low[u], dfn[v]);
}
}
if(low[u] == dfn[u]){
cut++;
do{
v = stack1[--top];
num[v] = cut;
vis[v] = ;
}while(u != v);
}
}
int main() {
while(~scanf("%d",&n)){
init();
int u,v,ret=;
for(int i=;i<=n;i++){
while(~scanf("%d",&u)&&u){
add(i,u);
}
}
for(int i=;i<=n;i++){
if(!dfn[i])Tarjan(i);
}
for(int i=; i<=n; i++) {
for(int j=head[i]; j!=-; j=edg[j].next) {
int v=edg[j].to;
if(num[i]!=num[v])out[num[i]]++,in[num[v]]++;
}
}
ans=;
if(cut==)printf("1\n0\n");
else {
for(int i=;i<=cut;i++){
if(!out[i])ans++;
if(!in[i])ret++;
}
printf("%d\n%d\n",ret,max(ret,ans));
}
}
return ;
}

最新文章

  1. 【听说是线段树】bzoj1012 [JSOI2008]最大数maxnumber
  2. 【金】nginx+uwsgi+django+python 应用架构部署
  3. color 颜色代码 android res/values/colors.xml
  4. java listener实现定时任务
  5. Bayesian Formulation on Cooperative Tracking
  6. linux 下 安装nexus
  7. webapp之路--之ios上图标
  8. 读书笔记之宿舍共享wifi
  9. android dialog弹出的情况下监听返回键
  10. Alpha 冲刺 (9/10)
  11. 10#Windows注册表的那些事儿
  12. MT【239】离心率最大
  13. HTML表格的简单使用1
  14. 计算概论(A)/基础编程练习1(8题)/1:大象喝水
  15. Dubbo/jupiterSPI 扩展引用
  16. Day14 自己定义泛型类的使用
  17. hibernate.cfg配置mysql方言
  18. 管道pipe与dup结合使用
  19. linux下一个网卡配置多个ip【虚拟ip】
  20. 软工网络15团队作业8——Beta阶段敏捷冲刺(Day4)

热门文章

  1. [洛谷P3413]SAC#1 - 萌数
  2. POJ1637:Sightseeing tour(混合图的欧拉回路)
  3. BZOJ1051:受欢迎的牛(并查集 / Tarjan)
  4. com.mongodb.MongoException$CursorNotFound: cursor not found on server异常处理
  5. shell之流程控制
  6. shell分发文件脚本
  7. SpringMVC异常报406 (Not Acceptable)的解决办法
  8. bzoj 2120 线段树套平衡树
  9. HDU1068 (二分图最大匹配匈牙利算法)
  10. python学习笔记 序列化