首页 技术 正文
技术 2022年11月14日
0 收藏 444 点赞 2,174 浏览 2425 个字

Network of Schools

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19613   Accepted: 7725

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

52 4 3 04 5 0001 0

Sample Output

12

Source

IOI 1996

思路:求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。

缩点以后,显然入度为0的点的个数就是第一问的答案。 
然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。

代码:

 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define ms(a, b) memset(a, b, sizeof(a)) , M = ; int h[N], p[M], a[M], fa[N], out[N], in[N], dfn[N], low[N]; int scc, top, ts, cnt, sta[N], insta[N]; void add(int x, int y) { p[++cnt] = h[x]; a[cnt] = y; h[x] = cnt; } void tarjan(int u) {     int i;     dfn[u] = low[u] = ++ts;     sta[++top] = u; insta[u] = ;     for (i = h[u]; i; i = p[i])         if (!dfn[a[i]]) {             tarjan(a[i]);             low[u] = min(low[u], low[a[i]]);         } else if (insta[a[i]])             low[u] = min(low[u], dfn[a[i]]);     if (dfn[u] == low[u]) {         ++scc;         do {             i = sta[top--];             insta[i] = ;             fa[i] = scc;         } while (i != u);     } } int main() {     int i, j, n, m, x, b;     while (scanf("%d", &n) != EOF) {         ms(dfn, ); ms(); ms(h, ); ms();         ts = cnt = top = scc = ;         ; i <= n; ++i)             while (scanf("%d", &x) && x) add(i, x);         ; i <= n; ++i)             if (!dfn[i]) tarjan(i);         ; i <= n; ++i)             for (j = h[i]; j; j = p[j])                 if (fa[i] != fa[a[j]])                     ++out[fa[i]], ++in[fa[a[j]]];         ,a2=;         ; i <= scc; ++i) {             if (!out[i]) ++a1;             if (!in[i])  ++a2;         }         ) puts("1\n0");         else printf("%d\n%d\n", a2, max(a1, a2));     }     ; }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295