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. 微信小程序分析见解

    前两天朋友圈都快被小程序给刷爆了: 对于小程序这方面,  由于没有公测的资格.所以翻阅了许许多多的资料,来了解一下小程序: 微信小程序: 小程序是一种不需要下载安装即可使用的应用,它实现了应用&quo ...

  2. 【JZOJ4899】【NOIP2016提高A组集训第17场11.16】雪之国度

    题目描述 雪之国度有N座城市,依次编号为1到N,又有M条道路连接了其中的城市,每一条道路都连接了不同的2个城市,任何两座不同的城市之间可能不止一条道路.雪之女王赋予了每一座城市不同的能量,其中第i座城 ...

  3. 从零学React Native之14 网络请求

    通过HTTP或者HTTPS协议与网络侧服务器交换数据是移动应用中常见的通信方式. node-fetch是RN推荐的请求方式. React Native框架在初始化项目时, 引入了node-fetch包 ...

  4. ZOJ 3956 Course Selection System [01背包]

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3956 题意:就是给你Hi,Ci的值,问怎么取使得下面那个式子的值最大: 理 ...

  5. 阿里云数据库自研产品亮相国际顶级会议ICDE 推动云原生数据库成为行业标准

    4月9日,澳门当地时间下午4:00-5:30,阿里云在ICDE 2019举办了主题为“云时代的数据库”的专场分享研讨会. 本次专场研讨会由阿里巴巴集团副总裁.高级研究员,阿里云智能数据库产品事业部负责 ...

  6. day13 SQLAlchemy

    ORM:也叫关系对象映射 本篇要点: 原生模块 pymsql ORM框架 SQLAchemy pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 需 ...

  7. python列表、元组、字典、集合的简单操作

    一.列表.元组 1.常用操作函数 #Author:CGQ import copy #列表 ''' names=["ZhangYang","XiaoHei",&q ...

  8. 数组工具类 Day07

    package com.sxt.arraytest2; /* * 数组的工具类 */ import java.util.Arrays; public class TestArrays { public ...

  9. LeetCode63 Unique Paths II

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...

  10. @codeforces - 418D@ Big Problems for Organizers

    目录 @description@ @solution@ @accepted code@ @details@ @description@ n 个点连成一棵树,经过每条边需要花费 1 个单位时间. 现给出 ...