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

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


题意:
给定一个n (n<=100)个点的有向图,问:

  Q1、最少需要选择多少个点,使得从这些点出发能遍历完整个图;
  Q2、最少需要添加多少条有向边,使得整个图成为强连通图;

强连通分量+缩点
Q1:入度为0
Q2:max(入度为0,出度为0)
注意只有一个scc时
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n;
struct edge{
int v,ne;
}e[N*N];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
} int dfn[N],low[N],belong[N],dfc,scc;
int st[N],top=;
void dfs(int u){
dfn[u]=low[u]=++dfc;
st[++top]=u;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!dfn[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(!belong[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
scc++;
while(true){
int x=st[top--];
belong[x]=scc;
if(x==u) break;
}
}
}
int outd[N],ind[N];
void point(){
for(int u=;u<=n;u++)
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(belong[u]!=belong[v]) outd[belong[u]]++,ind[belong[v]]++;
}
}
int main(){
n=read();
for(int u=;u<=n;u++){
int v=read();
while(v!=){ins(u,v);v=read();}
}
for(int i=;i<=n;i++) if(!dfn[i]) dfs(i);
point();
int cnt1=,cnt2=;
for(int i=;i<=scc;i++){
if(ind[i]==) cnt1++;
if(outd[i]==) cnt2++;
}
if(scc==) printf("1\n0");
else printf("%d\n%d",cnt1,max(cnt1,cnt2));
}

POJ1236Network 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. Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

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

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

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

  6. POJ1236Network of Schools(强连通分量 + 缩点)

    题目链接Network of Schools 参考斌神博客 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后 ...

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

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

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

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

  9. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

随机推荐

  1. 【Java每日一题】20161214

    package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1214 { public s ...

  2. php rsa加密解密实例

    1.加密解密的第一步是生成公钥.私钥对,私钥加密的内容能通过公钥解密(反过来亦可以) 下载开源RSA密钥生成工具openssl(通常Linux系统都自带该程序),解压缩至独立的文件夹,进入其中的bin ...

  3. golang中如何使用http,socket5代理

    Golang Http use socket5 proxy 因为最近想爬取一些网站上的视频,无奈网站在墙外,只能通过代理进行爬取,因此在网上搜索关于golang使用代理的方法. 功夫不负有心人,最后我 ...

  4. big-endian和little-endian

    1.故事的起源 "endian"这个词出自<格列佛游记>.小人国的内战就源于吃鸡蛋时是究竟从大头(Big-Endian)敲开还是从小头(Little-Endian)敲开 ...

  5. [翻译]Java HashMap工作原理

    大部分Java开发者都在使用Map,特别是HashMap.HashMap是一种简单但强大的方式去存储和获取数据.但有多少开发者知道HashMap内部如何工作呢?几天前,我阅读了java.util.Ha ...

  6. CSS 优先级判断

    在面试中被问到这个问题 来记录下  发现自己之前之所以会忘记还是缺少理解的记忆 参考 CSS权威指南 一个CSS选择器的特殊性值表述为4个部分  0 0 0 0 对于选择器中给定的各个ID的属性值   ...

  7. SharePoint 2013 自定义扩展菜单(二)

    接博文<SharePoint 2013 自定义扩展菜单>,多加了几个例子,方便大家理解. 例七 列表设置菜单扩展(listedit.aspx) 扩展效果 XML描述 <CustomA ...

  8. 基于Ruby的watir-webdriver自动化测试方案与实施(四)

    接着基于Ruby的watir-webdriver自动化测试方案与实施(三) http://www.cnblogs.com/Javame/p/4159468.html 继续 ... ...   首先回忆 ...

  9. Java中的Atomic包

    Atomic包的作用 方便程序员在多线程环境下,无锁的进行原子操作 Atomic包核心 Atomic包里的类基本都是使用Unsafe实现的包装类,核心操作是CAS原子操作: 关于CAS compare ...

  10. mysql与oracle的日期/时间函数小结

    前言 本文的日期/时间全部格式化为”2016-01-01 01:01:01“形式: MONITOR_TIME为数据库表字段: 字符串与日期/时间相互转换函数 Oracle 日期/时间转字符串函数:to ...