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

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
题解:

    1. /题意:
    2. //给你一个N个点的有向图,问1,最少连接几个点可以直接或间接 连接到所有点。
    3. //                         2,最少增加几条边使图强连通。
    4. //思路SCC + 缩点后。统计出入度为0的SCC数sumin和出度为0的SCC数sumout。
    5. //答案一就是sumin,答案二就是max(sumin, sumout)。注意只有一个SCC时,输出1 0。
代码:
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<stack>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
vector<int>vec[MAXN];
stack<int>S;
int dfn[MAXN],low[MAXN],Instack[MAXN],in[MAXN],out[MAXN];
int dfs_blocks,scc,sccon[MAXN];
void targin(int u,int fa){
S.push(u);
Instack[u]=;
dfn[u]=low[u]=++dfs_blocks;
for(int i=;i<vec[u].size();i++){
int v=vec[u][i];
if(!dfn[v]){
targin(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v]){
low[u]=min(low[u],dfn[v]);
}
}
if(low[u]==dfn[u]){
scc++;
while(){
int v=S.top();
S.pop();
Instack[v]=;
sccon[v]=scc;
if(u==v)break;
}
}
}
int main(){
int N;
while(~scanf("%d",&N)){
scc=;
for(int i=;i<MAXN;i++)vec[i].clear();
while(!S.empty())S.pop();
mem(in,);mem(out,);mem(Instack,);
mem(dfn,);mem(low,);mem(sccon,);
for(int i=;i<=N;i++){
int x;
while(scanf("%d",&x),x!=){
vec[i].push_back(x);
}
}
for(int i=;i<=N;i++){
if(!dfn[i]){
targin(i,-);
}
}
for(int i=;i<=N;i++){
for(int j=;j<vec[i].size();j++){
int v=vec[i][j];
if(sccon[i]!=sccon[v])in[sccon[v]]++,out[sccon[i]]++;
}
}
// printf("%d\n",scc);
int numin=,numout=;
if(scc==){
puts("1\n0");continue;
}
for(int i=;i<=scc;i++){
if(in[i]==)numin++;
if(out[i]==)numout++;
}
printf("%d\n%d\n",numin,max(numin,numout));
}
return ;
}
												

Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)的更多相关文章

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

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

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

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

  3. POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度

    题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Tot ...

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

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

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

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

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

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

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

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

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

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

  9. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

随机推荐

  1. JavaSE复习日记 : 递归函数

    /* * 递归函数 * 什么是递归? * 在一个方法的内部,对自身进行调用,又叫做递归调用 * * 递归和循环的编写都包括三部分: * 1. 初始值; * 2. 终止条件; * 3. 前进步长; * ...

  2. linux install nginx error

    1 2 3 4 5 6 7 8 9 10 11 [mahao01@127.0.0.1 nginx-1.2.9]$ make make -f objs/Makefile make[1]: Enterin ...

  3. perl正则表达式第二周笔记

    1.使用正则表达式修改文本 1.使用正则表达式修改文本 正则表达式的功能不只有查询,还可以对文本进行修改,例如替换 $var=~m/regex/i $var=~s/regex/replacement/ ...

  4. 一周学会Mootools 1.4中文教程:(5)Ajax

    ajax在我们前台的开发中是非常重要的,所以我们单独拿出一节课来讲述,首先我们看一下mootools的ajax构成 语法: var myRequest=new Request([参数]); 参数: u ...

  5. 2)PHP中把读取.txt中内容并转为UTF-8格式

    <?php $filename = "filename.txt"; $handle = fopen($filename, "r");//读取二进制文件时, ...

  6. python读取word表格内容(1)

    1.首页介绍下word表格内容,实例如下: 每两个表格后面是一个合并的单元格

  7. Gulp 之二

    Gulp学习2 之前已经配置过一篇啦, 只不过那次是针对browserify 搬运 http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8 ...

  8. CSS Flex

    关于flex 请看这里  https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 太详细啦!!!  还通俗易懂!!! 没啥好说的 不过上面那篇文 ...

  9. 窗口嵌入到另一个窗口(VC和QT都有)

    1.用vc新建一个dialog1工程.属性默认. 2.insert一个dialog2,改为child. 3.在dialog1中包含dialog2头文件,在一个按钮事件中显示dialog2: Cdial ...

  10. delphi关于文件操作集锦

        关于文件操作集锦 取得该快捷方式的指向EXE关键词:快捷方式 LNK unit Unit1; interface usesWindows, Messages, SysUtils, Varian ...