Network of Schools

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19326   Accepted: 7598

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:对于有向图,问选多少个点出发,可以走遍所有点。
   问题2:对于有向图,问最少加几条边,使得图强连通
 
思路:问题1:强连通分量缩点后得到DAG,入度为0的连通分量的数量即为答案。
   问题2:强连通分量缩点后得到DAG,出度为0的连通分量连向入度为0的连通分量,答案为max(出度为0的连通分量数,入度为0的连通分量数)
        但是当只有一个强连通分量的时候,答案为0。
 
 //2017-08-20
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int N = ;
int n, in_degree[N], out_degree[N];
bool vis[N];
vector<int> G[N];
vector<int> rG[N];
vector<int> vs;
int cmp[N]; void add_edge(int u, int v){
G[u].push_back(v);
rG[v].push_back(u);
} //input: u 顶点
//output: vs 后序遍历顺序的顶点列表
void dfs(int u){
vis[u] = true;
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
if(!vis[v])
dfs(v);
}
vs.push_back(u); } //input: u 顶点编号; k 拓扑序号
//output: cmp[] 强连通分量拓扑序
void rdfs(int u, int k){
vis[u] = true;
cmp[u] = k;
for(int i = ; i < rG[u].size(); i++){
int v = rG[u][i];
if(!vis[v])
rdfs(v, k); } } //Strongly Connected Component 强连通分量
//input: n 顶点个数
//output: k 强连通分量数;
int scc(){
memset(vis, , sizeof(vis));
vs.clear();
for(int u = ; u <= n; u++)
if(!vis[u]){
dfs(u);
}
int k = ;
memset(vis, , sizeof(vis));
for(int i = vs.size()-; i >= ; i--)
if(!vis[vs[i]])
rdfs(vs[i], k++);
return k;
} void solve(){
int k = scc();
int ans = ;
memset(in_degree, , sizeof(in_degree));
memset(out_degree, , sizeof(out_degree));
for(int u = ; u <= n; u++){
memset(vis, , sizeof(vis));
for(int i = ; i < G[u].size(); i++){
int v = G[u][i];
if(vis[v])continue;
vis[v] = ;
if(cmp[u] != cmp[v]){
out_degree[cmp[u]]++;
in_degree[cmp[v]]++;
}
}
}
int a = , b = ;
for(int i = ; i < k; i++){
if(in_degree[i] == )a++;
if(out_degree[i] == )b++;
}
ans = max(a, b);
if(k == )ans = ;
printf("%d\n%d\n", a, ans);
} int main()
{
while(scanf("%d", &n)!=EOF){
for(int u = ; u <= n; u++){
G[u].clear();
rG[u].clear();
}
int v;
for(int u =; u <= n; u++){
while(scanf("%d", &v)== && v){
add_edge(u, v);
}
}
solve();
} return ;
}

POJ1236(KB9-A 强连通分量)的更多相关文章

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

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

  2. [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

    题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...

  3. poj1236 Network of Schools【强连通分量(tarjan)缩点】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html  ---by 墨染之樱花 [题目链接]http://poj.org/pr ...

  4. poj-1236(强连通分量)

    题意:给你n个点,每个点可能有指向其他点的单向边,代表这个点可以把软件传给他指向的点,然后解决两个问题, 1.问你最少需要给几个点,才能使所有点都能拿到软件: 2.问你还需要增加几条单向边,才能使任意 ...

  5. poj1236 Network of Schools ,有向图求强连通分量(Tarjan算法),缩点

    题目链接: 点击打开链接 题意: 给定一个有向图,求: 1) 至少要选几个顶点.才干做到从这些顶点出发,能够到达所有顶点 2) 至少要加多少条边.才干使得从不论什么一个顶点出发,都能到达所有顶点   ...

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

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

  7. Proving Equivalences UVALive - 4287(强连通分量 水题)

    就是统计入度为0 的点 和 出度为0 的点  输出 大的那一个,, 若图中只有一个强连通分量 则输出0即可 和https://www.cnblogs.com/WTSRUVF/p/9301096.htm ...

  8. HDU5934 强连通分量

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...

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

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

随机推荐

  1. 使用Squid部署代理服务

    Squid是Linux系统中最为流行的一款高性能代理服务软件,通常用作Web网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存.简单来说,Squid服务程序会按照收到的用户请求向网站源 ...

  2. 10_python_函数进阶

    一.函数参数-动态参数 形参:位置参数.默认值参数.动态参数 动态参数分为两种:动态接收位置参数 *args  .动态接收关键字参数 *kwargs     1. *args def chi(*foo ...

  3. linux 使用进程管理工具 supervisor

    1.supervisor是使用python进行开发的运行在linux服务器上的进程管理工具 老版本的supervisor需要运行在python2环境,如果需要使用supervisor管理python3 ...

  4. word2vec的原理(一)

    最近上了公司的新员工基础培训课,又对NLP重新产生的兴趣.NLP的第一步大家知道的就是不停的写正则,那个以前学的还可以就不看了.接着就是我们在把NLP的词料在传入神经网络之前的一个预处理,最经典的就是 ...

  5. CSS选择器之兄弟选择器(~和+)

    今天在改以以前人家写的网页的样式的时候,碰到这个选择器,‘~’,当时我是懵逼的,傻傻分不清 ‘+’ 跟 ‘~’的区别,虽然我知道他们都是兄弟选择器. 后来网上查了下,也许是我查找的方式不对,没有找到我 ...

  6. POJ 2612

    #include<iostream> #include<stdio.h> #include<algorithm> #define MAXN 11 using nam ...

  7. C# SqlHelper类的数据库操作

    #region 私有构造函数和方法 private SqlHelper() { } /// <summary> /// 将SqlParameter参数数组(参数值)分配给SqlComman ...

  8. odoo开发环境搭建(二):安装Ubuntu 17虚拟机

    odoo开发环境搭建(二):安装Ubuntu 17虚拟机 下载镜像文件: 配置网络: 安装vmware tools: 配置共享文件夹: 选中虚拟机,右键编辑设置里边配置共享文件夹,指定windows本 ...

  9. 集合框架_DAY17

    1:五种数据结构: 数组:长度固定,有序,查找方便   链表:添加删除方便   栈:先进后出    队列:先进先出   树结构:完成排序动作 2:泛型(了解) (1)是一种把明确数据类型的工作推迟到创 ...

  10. (转)CentOS一键安装Nginx脚本

    原文:https://www.xiaoz.me/archives/10301 https://blog.slogra.com/post-676.html-----centos7一键安装nginx脚本