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 题目大意:有n个学校,每个学校能够单向到达某些学校,1.求出最少要给几个学校发软件才能使每个学校都有软件用 2.求出最少需要连接多少条边才能使任意学校出发都能到达其他学校
思路:第一个问的话,我们先求出该图中的所有强连通分量,将每个强连通分量看成一个点,求出入度为0的强连通分量的个数num1即可;第二问则还需要求出强连通分量中出度为0的点的个数num2,最后取max(num1,num2)
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack> using namespace std;
const int maxn = ;
int low[maxn],dfn[maxn];
int vis[maxn],f[maxn],num[maxn];
int in[maxn],out[maxn];
vector<int>edge[maxn];
int n,cnt,color;//cnt为low数组的节点数
stack<int>Q;
void tarjan(int u)
{
low[u] = dfn[u] = ++cnt;
vis[u] = ;
Q.push(u);
for(int i=;i<edge[u].size();i++){
int t = edge[u][i];
if(!dfn[t]){
tarjan(t);
low[u] =min(low[u],low[t]);
}else if(vis[t])
low[u] = min(low[u],dfn[t]);
}
if(dfn[u]==low[u]){
vis[u] = ;
f[u] = ++color;//染色缩点
while((Q.top()!=u) && Q.size()){
f[Q.top()] = color;
vis[Q.top()] = ;
Q.pop();
}
Q.pop();
}
}
void init()
{
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(f,,sizeof(f));
cnt = ;color=;
}
int main()
{
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++)edge[i].clear();
for(int x,i=;i<=n;i++){
while(scanf("%d",&x)&&x)
edge[i].push_back(x);
}
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=;i<=n;i++){
for(int j=;j<edge[i].size();j++){
int v = edge[i][j];
if(f[i]!=f[v]){//若不属于同一个强连通分量
in[f[v]]++;
out[f[i]]++;
}
}
}
int ans1=,ans2=;
for(int i=;i<=color;i++){
if(in[i]==)ans1++;
if(out[i]==)ans2++;
}
if(color==)printf("1\n0\n");
else printf("%d\n%d\n",ans1,max(ans1,ans2));
}
return ;
}

POJ 1236 Network of Schools(tarjan)的更多相关文章

  1. POJ 1236 Network of Schools (Tarjan)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22745   Accepted: 89 ...

  2. POJ 1236 Network of Schools(tarjan)题解

    题意:一个有向图.第一问:最少给几个点信息能让所有点都收到信息.第二问:最少加几个边能实现在任意点放信息就能传遍所有点 思路:把所有强连通分量缩成一点,然后判断各个点的入度和出度 tarjan算法:问 ...

  3. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

  4. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  5. POJ 1236 Network of Schools(tarjan求强连通分量+思维)

    题目链接:http://poj.org/problem?id=1236 题目大意: 给你一个网络(有向图),有两个任务: ①求出至少同时需要几份副本可以使得整个网络都获得副本 ②至少添加多少信息表(有 ...

  6. POJ 1236 Network of Schools(tarjan算法 + LCA)

    这个题目网上有很多答案,代码也很像,不排除我的.大家的思路应该都是taijan求出割边,然后找两个点的LCA(最近公共祖先),这两个点和LCA以及其他点构成了一个环,我们判断这个环上的割边有几条,我们 ...

  7. POJ 1236 Network of Schools (tarjan算法+缩点)

    思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...

  8. poj 1236 Network of Schools(连通图)

    题目链接:http://poj.org/problem?id=1236 题目大意:有一些学校,学校之间可以进行收发邮件,给出学校的相互关系,问:1.至少 要向这些学校发送多少份才能使所有的学校都能获得 ...

  9. POJ 1236.Network of Schools (强连通)

    首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...

随机推荐

  1. font-weight

    font-weight 属性设置文本的粗细. 该属性用于设置显示元素的文本中所用的字体加粗.数字值 400 相当于 关键字 normal,700 等价于 bold. 每个数字值对应的字体加粗必须至少与 ...

  2. Spring_自动组件扫描和 基于注解配置bean

    自动组件扫描 启用Spring组件扫描功能. 使用@Component注释来表示这是类是一个自动扫描组件.  package com.tanlei.dao; import org.springfram ...

  3. 初识Django(DNS原理及web框架)

    DNS的原理 假设www.abc.com的主机要查询www.xyz.abc.com的服务器ip地址. 知识点 1.hosts文件:以静态映射的方式提供IP地址与主机名的对照表,类似ARP表 2.域:a ...

  4. iOS 检测耳机插入/拔出

    http://www.verydemo.com/demo_c134_i28481.html 开发过程中录音和播放这块碰到了一些问题,麻烦的主要有三个: 检测是否有声音输入设备 当有多个声音输出设备时, ...

  5. 跨域知识(一)——CORS

    CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...

  6. 2017校赛 问题 D: 我知道了,你知道了吗?【递归】

    题目描述 Alice和Bob走在去学校的路上,听到两个路人的对话: 路人甲:我知道了, 你知道了吗? 路人乙:我知道你知道了,你知道了吗? 路人甲:我知道你知道我知道了,你知道了吗? 路人乙:我知道你 ...

  7. Oracle 11g Pivot函数实现行转列

    先上语法规范: SELECT .... FROM <table-expr> PIVOT ( aggregate-function(<column>) FOR <pivot ...

  8. Nuxt.js打造旅游网站第1篇_项目环境搭建

    1. 安装 使用官网提供的脚手架工具 create-nuxt-app,创建一个nuxtjs项目. npx create-nuxt-app xianyun 注意:在NPM版本5.2.0默认安装了npx, ...

  9. 从零学React Native之08Image组件

    开发过程中, 几乎每个项目都会用到图片. RN就是通过Image组件显示图片.既可以加载网络图片,也可以加载本地资源图片. Image组件必须在样式中声明图片的款和高.如果没有声明,则图片将不会被呈现 ...

  10. docker images列出镜像

    命令:docker images Usage: docker images [OPTIONS] [REPOSITORY[:TAG]] List images Options: -a, --all Sh ...