POJ 1236 Network of Schools(tarjan)

news/2024/7/1 9:33:42
Network of Schools

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)

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<vector>
 5 #include<stack>
 6 
 7 using namespace std;
 8 const int maxn = 205;
 9 int low[maxn],dfn[maxn];
10 int vis[maxn],f[maxn],num[maxn];
11 int in[maxn],out[maxn];
12 vector<int>edge[maxn];
13 int n,cnt,color;//cnt为low数组的节点数
14 stack<int>Q;
15 void tarjan(int u)
16 {
17     low[u] = dfn[u] = ++cnt;
18     vis[u] = 1;
19     Q.push(u);
20     for(int i=0;i<edge[u].size();i++){
21         int t = edge[u][i];
22         if(!dfn[t]){
23             tarjan(t);
24             low[u] =min(low[u],low[t]);
25         }else if(vis[t])
26             low[u] = min(low[u],dfn[t]);
27     }
28     if(dfn[u]==low[u]){
29         vis[u] = 0;
30         f[u] = ++color;//染色缩点
31         while((Q.top()!=u) && Q.size()){
32             f[Q.top()] = color;
33             vis[Q.top()] = 0;
34             Q.pop();
35         }
36         Q.pop();
37     }
38 }
39 void init()
40 {
41     memset(low,0,sizeof(low));
42     memset(dfn,0,sizeof(dfn));
43     memset(vis,0,sizeof(vis));
44     memset(num,0,sizeof(num));
45     memset(in,0,sizeof(in));
46     memset(out,0,sizeof(out));
47     memset(f,0,sizeof(f));
48     cnt = 0;color=0;
49 }
50 int main()
51 {
52     while(scanf("%d",&n)!=EOF){
53         init();
54         for(int i=0;i<=n;i++)edge[i].clear();
55         for(int x,i=1;i<=n;i++){
56             while(scanf("%d",&x)&&x)
57                 edge[i].push_back(x);
58         }
59         for(int i=1;i<=n;i++)
60             if(!dfn[i])
61                 tarjan(i);
62         for(int i=1;i<=n;i++){
63             for(int j=0;j<edge[i].size();j++){
64                 int v = edge[i][j];
65                 if(f[i]!=f[v]){//若不属于同一个强连通分量
66                     in[f[v]]++;
67                     out[f[i]]++;
68                 }
69             }
70         }
71         int ans1=0,ans2=0;
72         for(int i=1;i<=color;i++){
73             if(in[i]==0)ans1++;
74             if(out[i]==0)ans2++;
75         }
76         if(color==1)printf("1\n0\n");
77         else printf("%d\n%d\n",ans1,max(ans1,ans2));
78     }
79     return 0;
80 }
View Code

 

转载于:https://www.cnblogs.com/wangrunhu/p/9681268.html


http://lihuaxi.xjx100.cn/news/236199.html

相关文章

trash-cli设置Linux 回收站

trash-cli 设置 Linux 回收站 trash-cli是一个使用 python 开发的软件包&#xff0c;包含 trash-put、restore-trash、trash-list、trash-empty、trash-rm等命令&#xff0c;我们可以通过这条命令&#xff0c;将文件移动到回收站&#xff0c;或者还原删除了的文件。 trash-cli的…

nginx安全日志分析脚本的编写

https://blog.csdn.net/nextdoor6/article/details/51914966

计算机操作培训主持词,魅力女性沙龙会主持词文稿.docx

魅力女性沙龙会主持词??性的学科、一项重要的经济管理工作&#xff0c;是加强经济管理&#xff0c;提高经济效益的重要手段&#xff0c; 经济管理离不开会计&#xff0c; 经济越发展会计工作就显得越重要。会计工作在提高经济在企业的经营管理中起着重要的作用&#xff0c;其…

封装 vue 组件的过程记录

在我们使用vue的开发过程中总会遇到这样的场景&#xff0c;封装自己的业务组件。 封装页面组件前要考虑几个问题&#xff1a;1、该业务组件的使用场景 2、在什么条件下展示一些什么数据&#xff0c;数据类型是什么样的&#xff0c;及长度颜色等 3、如果是通用的内容&#xff0c…

只需3分钟,就能轻松创建 一个SpreadJS的React项目

概述SpreadJS 纯前端表格控件 V11.2(SP2) 已经全面支持了 React 的拓展。接下来我们看下如何利用3分钟快速创建一个 SpreadJS 的 React 项目。1.新建React 项目&#xff08;耗时 1 Min&#xff09;直接运行&#xff1a;npx create-react-app react-spread-sheets还不清楚什么是…

streptavidin-PEG-TRITC 链霉亲和素-聚乙二醇-四甲基罗丹明

产品名称&#xff1a;链霉亲和素-聚乙二醇-四甲基罗丹明 英文名称&#xff1a;streptavidin-PEG-TRITC 纯度&#xff1a;95% 存储条件&#xff1a;-20C&#xff0c;避光&#xff0c;避湿 外观:固体或粘性液体&#xff0c;取决于分子量 PEG分子量可选&#xff1a;350、550、750、…

计算机设备管理器不显示com,台式机设备管理器打开是空白怎么办_win10设备管理无法显示解决方法...

2015-06-15 14:08:22  浏览量&#xff1a;2252win7设备管理器空白怎么办&#xff1f;最近有用户反馈打开设备管理器的时候&#xff0c;发现win7设备管理器显示空白&#xff0c;该怎么处理这个问题&#xff1f;下面跟随小编脚步一起看看win7系统打开设备管理器空白的解决方法。…

安利Mastodon:属于未来的社交网络

我为Mastodon开发了一款安卓客户端&#xff0c;v1.0版本已经发布&#xff0c;欢迎下载使用 源码在这里&#xff1a;https://github.com/shuiRong/Gakki ??? 正文 Mastodon(长毛象)是什么&#xff1f; 是一个免费开源、去中心化、分布式的微博客社交网络&#xff0c;是微博、…