Problem 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

题意:给出 n 个结点,以及每个结点可到达的点,求有几个强连通分量,然后问至少在可到达的点中添加几个点,使得图变为强连通图

思路:先求出图的所有强连通分量,然后进行缩点构建新图,第一问是新图中入度为 0 点的个数,第二问是新图中入度0点数与出度0点数最大的一个

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 20001
#define MOD 16007
#define E 1e-6
#define LL long long
using namespace std;
int n,m;
vector<int> G[N];
stack<int> S;
int dfn[N],low[N];
bool in[N],out[N];
bool vis[N];//标记数组
int sccno[N];//记录结点i属于哪个强连通分量
int block_cnt;//时间戳
int sig;//记录强连通分量个数
void Tarjan(int x){vis[x]=true;dfn[x]=low[x]=++block_cnt;//每找到一个新点,纪录当前节点的时间戳S.push(x);//当前结点入栈for(int i=0;i<G[x].size();i++){//遍历整个栈int y=G[x][i];//当前结点的下一结点if(vis[y]==false){//若未被访问过Tarjan(y);low[x]=min(low[x],low[y]);}else if(!sccno[y])//若已被访问过,且不属于任何一个连通分量low[x]=min(low[x],dfn[y]);}if(dfn[x]==low[x]){//满足强连通分量要求sig++;//记录强连通分量个数while(true){//记录元素属于第几个强连通分量int temp=S.top();S.pop();sccno[temp]=sig;if(temp==x)break;}}
}
int main()
{while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)G[i].clear();for(int i=0;i<n;i++){int y;while(scanf("%d",&y)!=EOF&&y){y--;G[i].push_back(y);}}sig=0;block_cnt=0;memset(vis,0,sizeof(vis));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(sccno,0,sizeof(sccno));for(int i=0;i<n;i++)if(vis[i]==false)Tarjan(i);memset(in,false,sizeof(in));memset(out,false,sizeof(out));for(int i=1;i<=sig;i++)in[i]=out[i]=true;for(int x=0;x<n;x++)for(int i=0;i<G[x].size();i++){int y=G[x][i];if(sccno[x]!=sccno[y])in[sccno[y]]=out[sccno[x]]=false;}int a=0,b=0;for(int i=1;i<=sig;i++){if(in[i])a++;if(out[i])b++;}if(sig==1)printf("1\n0\n");elseprintf("%d\n%d\n",a,max(a,b));}
}

Network of Schools(POJ-1236)相关推荐

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

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

  2. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  3. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  4. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  5. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  6. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  7. POJ 1236 Network of Schools(tarjan)

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

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

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

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

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

  10. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

最新文章

  1. 前端学习(2424):关于问题的解决方式
  2. 基于Kubernetes(k8s)的RabbitMQ 集群
  3. SFB 项目经验-45-用培训课件当运维文档,聪明
  4. MySQL · 源码分析 · MySQL 半同步复制数据一致性分析
  5. 基于状态空间模型的控制器设计
  6. 洛谷 1449——后缀表达式(线性数据结构)
  7. python pip 快速安装第三方库和下载好whl文件
  8. 织梦安全的思路---未实践
  9. JAVA复习总结 一( 详细,干货!)
  10. 新概念二册 Lesson 29 Taxi!出租汽车! (复习现在完成时)
  11. iOS Resume【简历模板】
  12. PySide2安装教程
  13. c语言中怎么把大写字母转化为小写字母,c语言大小写字母怎么转化?
  14. 202009-4 星际旅行
  15. redis的主从自动切换
  16. 矿产资源储量报告及评审中的若干问题
  17. 通联支付公司软件测试待遇,通联支付怎么样
  18. 黑马VUE电商管理后台笔记记录
  19. bzoj3323【scoi2013】多项式的运算
  20. ps快捷键命令#ps学习教程基础视频抠图

热门文章

  1. 学好Python爬取京东知乎价值数据
  2. 职场上,比尽力更重要的,是要学会“借力”
  3. 字节教育大裁员:有员工赔了一套首付款!
  4. 面试官问单表数据量大一定要分库分表吗?我们用六个字和十张图回答
  5. 我是如何白嫖 Github 服务器自动抓取每日必应壁纸的?
  6. 图解Kafka中的基本概念
  7. Spring Cloud入门,看这篇就够了!
  8. 导致微服务失败的 11 个原因
  9. Oracle的REDO和UNDO
  10. SonarQube代码质量管理工具的安装(Linux)