题目链接: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. C++中,什么叫类,结构,联合?

    在C++中 class 和 struct 本质上一样 可以互用class的成员默认是private的,struct的成员默认是public的但一般习惯把成员变量隐藏的用class申明, 成员变量可以公 ...

  2. 我是一个线程 - IBM刘欣

    来自:码农翻身(微信号:coderising) 作者:IBM刘欣 我是一个线程,我一出生就被编了个号: 0×3704,然后被领到一个昏暗的屋子里, 这里我发现了很多和我一模一样的同伴. 我身边的同伴0 ...

  3. [NOIP2000] 提高组 洛谷P1022 计算器的改良

    题目背景 NCL是一家专门从事计算器改良与升级的实验室,最近该实验室收到了某公司所委托的一个任务:需要在该公司某型号的计算器上加上解一元一次方程的功能.实验室将这个任务交给了一个刚进入的新手ZL先生. ...

  4. 汕头市赛srm8 C-3

    n<=100000个点m<=300000条边有权无向联通图,给出K<=10000个特殊点求K个点中任意两点最短路的最小值. 方法一:K小,随便搞.先构造最短路树,在最短路树上Dijk ...

  5. List和Map、Set的区别

    首先 List 和 Set 是存储单列数据的集合,Map 是存储键和值这样的双列数据的集合:List 中存储的数据是有顺序,并且允许重复:Map 中存储的数据是没有顺序的,其键是不能重复的,它的值是可 ...

  6. Python---django轻量级框架

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  7. androidstudio Cannot resolve symbol 'xxx'

    Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题.鼠标放上去后显示 “Cannot resolve symbol XXX”,重启 ...

  8. 转 gSOAP中使用TCP协议传输数据

    一  模型 TCP/IP是一个协议族(Internet protocol suite),包含众多的协议,传输控制协议(TCP)和网际协议(IP)分属不同的层次,是保证数据完整传输的两个基本的重要协议. ...

  9. pcre7.0在vc6.0编译

    (0)从http://gnuwin32.sourceforge.net/packages/pcre.htm  (pcre windows)下下载最新的windows平台源代码pcre-7.0-src. ...

  10. xml解析工具mashaller javaee自带解析类

    1.怎样去掉Marshaller的格式化? : JAXBContext context = JAXBContext.newInstance(Entity.class); Marshaller mars ...