题目链接:http://poj.org/problem?id=1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19859   Accepted: 7822

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

 
 
 
题解:
1.利用Tarjan算法求出每个强连通分量,然后进行缩点(以下的分析中,结点是指经过缩点之后的强连通分量)。
2.如果强连通分量的个数为1,即表明题目所给的图为强连通图。故可直接输出答案:1, 0。否则:
首先求出每个强连通分量的入度和出度,然后:
task A:显然,只需要为每个入度为0的结点输入一份资料即可,其余入度不能为0的结点都可以从指向它的结点获取资料。
task B:每增加一条边,图中必有一个的结点入度增加1, 必有一个结点的出度增加1。设图中有a个结点的入度为0, b个结点的出度为0,假设a>=b,那么首先我们可以增加b条边,既能实现图中所有结点的出度都不能为0,但是还剩下a-b个结点的入度为0,此时,我们只需再添加a-b条边,既可以实现图中所有结点的入度都不为0了,所以总共需要添加a条边。当b>a时,需要添加b条边。综上结论:如果图中有a个结点的入度为0, b个结点的出度为0,那么只需添加 max(a,b)条边,即可使原图成为强连通图。前提是原图为简单图,且结点个数大于1。
 
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e2+; struct Edge
{
int to, next;
}edge[MAXN*MAXN];
int head[MAXN], tot; int index, Low[MAXN], DFN[MAXN];
int top, Stack[MAXN], Instack[MAXN];
int scc, Belong[MAXN];
int Indegree[MAXN], Outdegree[MAXN]; void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
} void Tarjan(int u)
{
int v;
Low[u] = DFN[u] = ++index;
Stack[top++] = u;
Instack[u] = ;
for(int i = head[u]; i!=-; i = edge[i].next)
{
v = edge[i].to;
if(!DFN[v])
{
Tarjan(v);
Low[u] = min(Low[u], Low[v]);
}
else if(Instack[v])
Low[u] = min(Low[u], Low[v]);
} if(Low[u]==DFN[u])
{
scc++;
do
{
v = Stack[--top];
Instack[v] = ;
Belong[v] = scc;
}while(v!=u);
}
} void init()
{
tot = ;
memset(head, -, sizeof(head)); index = scc = top = ;
memset(DFN, , sizeof(DFN));
memset(Low, , sizeof(Low));
memset(Instack, , sizeof(Instack)); memset(Indegree, , sizeof(Indegree));
memset(Outdegree, , sizeof(Outdegree));
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
init();
for(int u = ; u<=n; u++)
{
int v;
while(scanf("%d", &v) && v)
addedge(u, v);
} for(int i = ; i<=n; i++)
if(!DFN[i])
Tarjan(i); if(scc==)
{
printf("%d\n%d\n", , );
continue;
} for(int u = ; u<=n; u++)
{
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(Belong[u]==Belong[v]) continue;
Outdegree[Belong[u]]++;
Indegree[Belong[v]]++;
}
} int Innum = , Outnum = ;
for(int i = ; i<=scc; i++)
{
if(Indegree[i]==) Innum++;
if(Outdegree[i]==) Outnum++;
} printf("%d\n%d\n", Innum, max(Innum, Outnum));
}
}

POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度的更多相关文章

  1. poj-1236.network of schools(强连通分量 + 图的入度出度)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27121   Accepted: 10 ...

  2. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  3. Network of Schools(强连通分量缩点(邻接表&矩阵))

    Description A number of schools are connected to a computer network. Agreements have been developed ...

  4. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13801   Accepted: 55 ...

  5. POJ1236 Network of Schools (强连通分量,注意边界)

    A number of schools are connected to a computer network. Agreements have been developed among those ...

  6. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  7. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  8. [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)

    nocow上的题解很好. http://www.nocow.cn/index.php/USACO/schlnet 如何求强连通分量呢?对于此题,可以直接先用floyd,然后再判断. --------- ...

  9. POJ1236:Network of Schools(tarjan+缩点)?

    题目: http://poj.org/problem?id=1236 [题意] N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1 ...

随机推荐

  1. exports和moudle. exports

    http://zihua.li/2012/03/use-module-exports-or-exports-in-node/ https://github.com/seajs/seajs/issues ...

  2. F5 TCP Traffic Flow v0.5

    300dpi高清版下载地址 http://down.51cto.com/data/2332253

  3. bzoj1004 [HNOI2008]Cards 置换群+背包

    [bzoj1004][HNOI2008]Cards 2014年5月26日5,3502 Description 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿 ...

  4. git push ‘No refs in common and none specified’doing nothing问题解决

    git push ‘No refs in common and none specified’doing nothing问题解决 输入git push origin master即可解决问题

  5. SpringBoot Beans定义 连接池

    SpringBoot Beans定义 原有Spring框架,定义Bean方法如下 xml配置 组件扫描.@Controller.@Service... 原有Spring框架,参数注入方法如下 常用的参 ...

  6. [NOIP2002] 提高组 洛谷P1031 均分纸牌

    题目描述 有 N 堆纸牌,编号分别为 1,2,…, N.每堆上有若干张,但纸牌总数必为 N 的倍数.可以在任一堆上取若于张纸牌,然后移动. 移牌规则为:在编号为 1 堆上取的纸牌,只能移到编号为 2 ...

  7. Http、TCP/IP、Socket的区别

    网络由下往上分为 物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 通过初步的了解,我知道IP协议对应于网络层,TCP协议对应于传输层,而HTTP协议对应于应用层, 三者从本质上来说没有可 ...

  8. js面试题总结

    1.typeof和Object.prototype.toString typeof是js里面判断变量类型的一种方法,但这种方法没有Object.prototype.toString准确,前者有6种判断 ...

  9. Java实现敏感词过滤代码

    原文:http://www.open-open.com/code/view/1445762764148 import java.io.BufferedReader; import java.io.Fi ...

  10. 【转载】epoll与select/poll的区别总结

    因为这道题目经常被问到.干脆总结一下,免得遗漏了. 参考文章:http://www.cnblogs.com/qiaoconglovelife/p/5735936.html 1 本质上都是同步I/O 三 ...