Poj 1236 Network of Schools (Tarjan)

题目链接:

  Poj 1236 Network of Schools

题目描述:

  有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个网络才能够让所有学校被网络覆盖?2:至少要加几条线路就能做到在任意一个学校安装网络都可以覆盖全部学校?

解题思路:

  先用Tarjan对强连通分量进行缩点,然后对缩点以后的图进行处理,统计图中节点出度为零的有多少,入度为零的有多少个?

  因为入度为零的点不能由其他的点到达,在每个入度为零的节点安装网络可以让所有学校被网络覆盖。

  全部学校在一个强连通图里面,就可以实现第二问,因为出度为零的节点不能到达其他节点,入度为零的点不能由其他节点进来,所以要对出度为零加出度,入度为零的便加入度即可。PS:当全图只有一个连通块的时候应该不用加边的。

 1 #include <cstring>
 2 #include <cstdio>
 3 #include <string>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 110;
 7 struct node
 8 {
 9     int to ,next;
10 } edge[maxn*maxn];
11 int head[maxn], dfn[maxn], low[maxn], in[maxn], out[maxn], id[maxn];
12 int tot, cnt, top, time, instack[maxn], stack[maxn];
13 void init ()
14 {
15     tot = top = 0;
16     cnt = time = 0;
17     memset (head, -1, sizeof(head));
18     memset (dfn, 0, sizeof(dfn));
19     memset (low, 0, sizeof(low));
20     memset (in, 0, sizeof(in));
21     memset (out, 0, sizeof(out));
22     memset (id, 0, sizeof(id));
23     memset (instack, 0, sizeof(instack));
24     memset (stack, 0, sizeof(stack));
25 }
26 void Add (int from, int to)
27 {
28     edge[tot].to = to;
29     edge[tot].next = head[from];
30     head[from] = tot ++;
31 }
32 void Tarjan (int u)
33 {
34     dfn[u] = low[u] = ++time;
35     instack[u] = 1;
36     stack[top ++] = u;
37     for (int i=head[u]; i!=-1; i=edge[i].next)
38     {
39         int v = edge[i].to;
40         if (!dfn[v])
41             Tarjan (v);
42         if (instack[v])
43             low[u] = min (low[u], low[v]);
44     }
45     if (dfn[u] == low[u])
46     {
47         cnt ++;
48         while (1)
49         {
50             int v = stack[--top];
51             instack[v] = 0;
52             id[v] = cnt;
53             if (v == u)
54                 break;
55         }
56     }
57 }
58 int main ()
59 {
60     int n;
61     while (scanf ("%d", &n) != EOF)
62     {
63         init ();
64         for (int i=1; i<=n; i++)
65         {
66             int to;
67             while (scanf ("%d", &to), to)
68                 Add (i, to);
69         }
70         for (int i=1; i<=n; i++)
71             if (!dfn[i])
72                 Tarjan (i);
73         for (int i=1; i<=n; i++)
74             for (int j=head[i]; j!=-1; j=edge[j].next)
75             {
76                 int v = id[i];
77                 int u = id[edge[j].to];
78                 if (v != u)
79                 {
80                     out[v] ++;
81                     in[u] ++;
82                 }
83             }
84         int In, Out;
85         In = Out = 0;
86         for (int i=1; i<=cnt; i++)
87         {
88             if (!in[i])
89                 In ++;
90             if (!out[i])
91                 Out ++;
92         }
93         if (cnt == 1)
94             printf ("1\n0\n");
95         else
96             printf ("%d\n%d\n", In, max(In, Out));
97     }
98     return 0;
99 }
时间: 2024-10-18 03:32:12

Poj 1236 Network of Schools (Tarjan)的相关文章

POJ 1236 Network of Schools (Tarjan + 缩点)

Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 4871 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(强连通分量)

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

poj 1236 Network of Schools 【强连通图】

题目:poj 1236 Network of Schools 类似题目hdoj 2767 3836 /*******以下kuang大神的解释,写的很好就不解释了*************************/ 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任

[tarjan] poj 1236 Network of Schools

题目链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11433   Accepted: 4551 Description A number of schools are connected to a computer network. Agreements have been developed among thos

poj 1236 Network of Schools(连通图入度,出度为0)

http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13046   Accepted: 5215 Description A number of schools are connected to a computer network. Agreements have been developed among those scho

poj 1236 Network of Schools(tarjan+缩点)

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

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