POJ 1236 tarjan

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

5
2 4 3 0
4 5 0
0
0
1 0

Sample Output

1
2

Source

IOI 1996

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

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

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define ms(a, b) memset(a, b, sizeof(a))
 6 const int N = 101, M = 10001;
 7 int h[N], p[M], a[M], fa[N], out[N], in[N], dfn[N], low[N];
 8 int scc, top, ts, cnt, sta[N], insta[N];
 9 void add(int x, int y) { p[++cnt] = h[x]; a[cnt] = y; h[x] = cnt; }
10
11 void tarjan(int u) {
12     int i;
13     dfn[u] = low[u] = ++ts;
14     sta[++top] = u; insta[u] = 1;
15     for (i = h[u]; i; i = p[i])
16         if (!dfn[a[i]]) {
17             tarjan(a[i]);
18             low[u] = min(low[u], low[a[i]]);
19         } else if (insta[a[i]])
20             low[u] = min(low[u], dfn[a[i]]);
21     if (dfn[u] == low[u]) {
22         ++scc;
23         do {
24             i = sta[top--];
25             insta[i] = 0;
26             fa[i] = scc;
27         } while (i != u);
28     }
29 }
30
31 int main() {
32     int i, j, n, m, x, b;
33
34     while (scanf("%d", &n) != EOF) {
35         ms(dfn, 0); ms(out, 0); ms(h, 0); ms(in, 0);
36         ts = cnt = top = scc = 0;
37         for (i = 1; i <= n; ++i)
38             while (scanf("%d", &x) && x) add(i, x);
39         for (i = 1; i <= n; ++i)
40             if (!dfn[i]) tarjan(i);
41         for (i = 1; i <= n; ++i)
42             for (j = h[i]; j; j = p[j])
43                 if (fa[i] != fa[a[j]])
44                     ++out[fa[i]], ++in[fa[a[j]]];
45         int a1=0,a2=0;
46         for (i = 1; i <= scc; ++i) {
47             if (!out[i]) ++a1;
48             if (!in[i])  ++a2;
49         }
50         if (scc == 1) puts("1\n0");
51         else printf("%d\n%d\n", a2, max(a1, a2));
52     }
53     return 0;
54 }
时间: 2024-10-25 10:05:30

POJ 1236 tarjan的相关文章

POJ 1236 tarjan缩点+度数

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11441   Accepted: 4554 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li

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

POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入?几条边使得有向图成为一个强连通图. 分析: 跟HDU 2767 Proving Equivalences(题解)一样的题目,只是多了个问题,事实上转化成DAG后就不难考虑了,事实上仅仅要选择入度为0的点即可了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> *

Poj 1236 Network of Schools (Tarjan)

题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个网络才能够让所有学校被网络覆盖?2:至少要加几条线路就能做到在任意一个学校安装网络都可以覆盖全部学校? 解题思路: 先用Tarjan对强连通分量进行缩点,然后对缩点以后的图进行处理,统计图中节点出度为零的有多少,入度为零的有多少个? 因为入度为零的点不能由其他的点到达,在每个入度为零的节点安装网络可

POJ 1236 Network of Schools(强连通分量)

题目地址:POJ 1236 这个题的大意是求最少往多少点发送消息可以使任意一个点都能收到消息和最少增加多少条边可以使图为连通图.对于第一个问题,可以求入度为0的强连通块的块数,因为只有入度为0的强连通块是无法从外界接受信息的,而只要有一个入度的话,那整个连通块就都可以接收到信息.第二个问题则是求入度为0的强连通块与出度为0的强连通块的个数的最大值. 代码如下: #include <iostream> #include <cstdio> #include <string>

[POJ 1236][IOI 1996]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

POJ 1236 Network of Schools - 缩点

POJ 1236 :http://poj.org/problem?id=1236 参考:https://www.cnblogs.com/TnT2333333/p/6875680.html 题意: 有好多学校,每个学校可以给其他特定的学校发送文件.第一个问题是最少要给几个学校发文件,可以使得全部的学校收到文件.第二个问题是最少要加几条线路,使得随意挑一个学校发文件,也能使得全部的学校收到文件. 思路: 第一个问题,可以用tarjan给图中先缩点,因为强连通的环相互可达.所以只要数出缩完点后图中入度

POJ 1236——Network of Schools——————【加边形成强连通图】

Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1236 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: e

POJ 1236 Network of Schools(强连通分量)

POJ 1236 Network of Schools 链接:http://poj.org/problem?id=1236 题意:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B 在学校A 的列表中,则A 不一定在B 的列表中. 任务A:计算为使得每个学校都能通过网络收到软件,你至少需要准备多少份软件拷贝. 任务B:考虑一个更长远的任务,想确保给任意一个学校发放一个新的软件拷贝,该软件拷贝能发布到网络中的每个学

POJ 1236

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10500   Accepted: 4189 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a li