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个学校,每个学校能够单向到达某些学校,1.求出最少要给几个学校发软件才能使每个学校都有软件用 2.求出最少需要连接多少条边才能使任意学校出发都能到达其他学校
思路:第一个问的话,我们先求出该图中的所有强连通分量,将每个强连通分量看成一个点,求出入度为0的强连通分量的个数num1即可;第二问则还需要求出强连通分量中出度为0的点的个数num2,最后取max(num1,num2)
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack> using namespace std;
const int maxn = ;
int low[maxn],dfn[maxn];
int vis[maxn],f[maxn],num[maxn];
int in[maxn],out[maxn];
vector<int>edge[maxn];
int n,cnt,color;//cnt为low数组的节点数
stack<int>Q;
void tarjan(int u)
{
low[u] = dfn[u] = ++cnt;
vis[u] = ;
Q.push(u);
for(int i=;i<edge[u].size();i++){
int t = edge[u][i];
if(!dfn[t]){
tarjan(t);
low[u] =min(low[u],low[t]);
}else if(vis[t])
low[u] = min(low[u],dfn[t]);
}
if(dfn[u]==low[u]){
vis[u] = ;
f[u] = ++color;//染色缩点
while((Q.top()!=u) && Q.size()){
f[Q.top()] = color;
vis[Q.top()] = ;
Q.pop();
}
Q.pop();
}
}
void init()
{
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(f,,sizeof(f));
cnt = ;color=;
}
int main()
{
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++)edge[i].clear();
for(int x,i=;i<=n;i++){
while(scanf("%d",&x)&&x)
edge[i].push_back(x);
}
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=;i<=n;i++){
for(int j=;j<edge[i].size();j++){
int v = edge[i][j];
if(f[i]!=f[v]){//若不属于同一个强连通分量
in[f[v]]++;
out[f[i]]++;
}
}
}
int ans1=,ans2=;
for(int i=;i<=color;i++){
if(in[i]==)ans1++;
if(out[i]==)ans2++;
}
if(color==)printf("1\n0\n");
else printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return ;
}

最新文章

  1. iOS开发之多种Cell高度自适应实现方案的UI流畅度分析
  2. WinForm 遍历用户控件里CheckBox
  3. 用Node.js发送邮件
  4. JavaScript实现通过的集合类
  5. Centos5.8 安装 MySQL5.6.19
  6. layoutSubviews #pragma mark -
  7. [ACM_图论] 棋盘问题 (棋盘上放棋子的方案数)
  8. 一分钟制作U盘版BT3 - 有图滴儿 bt3破解教程
  9. 迷宫 maze
  10. 新闻:型牌男装:网上订服装,如何将返修率降到5个点以下 | IT桔子
  11. Win32多线程编程(3) — 线程同步与通信
  12. notepad++中的zencoding的快捷键修改[转]
  13. Oracle10g数据泵EXPDP和IMPDP备份与恢复数据
  14. PHP-微信公众平台开发-接收用户输入消息类型并响应
  15. AgileEAS.NET SOA中间件平台/敏捷软件开发平台
  16. 听翁恺老师mooc笔记(1)--为何选择学习C
  17. bzoj1858[Scoi2010]序列操作 线段树
  18. map函数、filer函数、reduce函数的用法和区别
  19. Eclipse maven hadoop -- java.io.IOException: No FileSystem for scheme: hdfs
  20. mongo学习笔记2--索引及表设计

热门文章

  1. golang数据类型三
  2. 初探postman
  3. 猜年龄v2.0
  4. JAVA中this的三种用法的详解
  5. Mysterious Antiques in Sackler Museum(判断长方形)
  6. 基于MaxCompute的数仓数据质量管理
  7. ArcGIS 发布高程服务。10.4
  8. qt中窗口绘制——图片的绘制
  9. Lambda plus: 云上大数据解决方案
  10. 独家 | TensorFlow 2.0将把Eager Execution变为默认执行模式,你该转向动态计算图了