题目链接: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. jenkins里跑selenium webdriver,Chrome浏览器不能打开&&unknown error: unable to discover open pages

    在windows的cmd里面执行 “python test.py”,毫无问题,浏览器正常打开,测试结果也正常. 问题: 但如果是在jenkins里,选择 “execute windows batch ...

  2. Run-time Settings--General--Run Logic

    LR单用户,重复操作日志 案例:假如你想在一个脚本中,实现登录执行1次,查询执行2次,插入执行3次,怎么办?录3个脚本?每个事务分别在脚本中复制N次? 当然不用,LR早就想到了你的需求,下面让我们隆重 ...

  3. dataTables中固定表头

    dataTables中固定表头 加入  bAutowidth:false, <style> #dayReveiveMoney_payment_list_table_wrapper .dat ...

  4. php 面向对象 (类 对象)

    //面向对象//什么是面向对象//面向过程//什么是对象?//一切皆是对象//类//由对象抽象化//造类//class Ren//{ //构造方法 - - 写不写都存在//类的初始化方法 //构造方法 ...

  5. [codeforces551E]GukiZ and GukiZiana

    [codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...

  6. [HDU3062]Party(2-sat)

    传送门 2-sat问题,只需要判断yes或no 所以可以直接连边,缩点,判断同一组的是否在同一个块中. #include <cstdio> #include <stack> # ...

  7. 45个android实例源码分享

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码 http://www.apkbus.c ...

  8. 【HDOJ6315】Naive Operations(线段树,树状数组)

    题意: 两个序列a和b,初始a[i]=0,b[i]给定且为一个1到n的排列,要求维护以下两种操作:1.区间[L,R]内a[i]加1 2.询问[L,R]内a[i]/b[i](下取整)之和 n,q< ...

  9. python-web apache mod_python 模块的安装

    安装apache 下载mod_python 编译安装 测试 下载mod_python,下载地址:mod_python 在GitHub 上面, 下载之后:目录结构如下: 安装依赖: #查找可安装的依赖 ...

  10. ubuntu 14.04 安装docker,docker-compose

    通过阿里的镜像安装 curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | s ...