Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19613   Accepted: 7725

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

思路:求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。

缩点以后,显然入度为0的点的个数就是第一问的答案。 
然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。

代码:

 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e4 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int low[N], dfn[N],head[N],beg[N];
bool ins[N];
int in[N],out[N];
int n;
int cnt, id, num;
stack<int> s; struct P {
int to, nxt;
} e[ * N]; void add(int u, int v) {
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
void tarjan(int u)
{
low[u]=dfn[u]=++id; ins[u]=;
s.push(u);//入栈
for(int i=head[u];~i;i=e[i].nxt){
int v=e[i].to;
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
int v;
do{
v=s.top();s.pop();
beg[v]=num;//缩点
ins[v]=;
}while(u!=v);
num++;
}
} void init() {
memset(ins, , sizeof(ins));
memset(head, -, sizeof(head));
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(beg, , sizeof(beg));
memset(in, , sizeof(in));
memset(out, , sizeof(out));
while(!s.empty()) s.pop();
cnt = id = num = ;
}
int main() {
while (scanf("%d",&n) != EOF) {
init();
for(int i=;i<=n;i++){
int x;
while(scanf("%d",&x)&&x) add(i,x);
}
for (int i = ; i <= n; i++) if (!dfn[i]) tarjan(i);
for (int i = ; i <= n; i++) {
for (int j = head[i]; j != -; j = e[j].nxt) {
int v=e[j].to;
if(beg[i]!=beg[v]) out[beg[i]]++,in[beg[v]]++;
}
}
int I=,O=;
for(int i=;i<num;i++){
if(!in[i]) I++;
if(!out[i]) O++;
}
if(num==) printf("1\n0");
else pi(I),pi(max(I,O));
}
return ;
}

POJ1236 tarjan的更多相关文章

  1. Network of Schools --POJ1236 Tarjan

    Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...

  2. poj1236 Tarjan算法模板 详解

    思想: 做一遍DFS,用dfn[i]表示编号为i的节点在DFS过程中的访问序号(也可以叫做开始时间)用low[i]表示i节点DFS过程中i的下方节点所能到达的开始时间最早的节点的开始时间.初始时dfn ...

  3. 最近切的两题SCC的tarjan POJ1236 POJ2186

    两题都是水题,1236第一问求缩点后入度为0的点数,第二问即至少添加多少条边使全图强连通,属于经典做法,具体可以看白书 POJ2186即求缩点后出度为0的那个唯一的点所包含的点数(即SCC里有多少点) ...

  4. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

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

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

  6. POJ1236【Tarjan+缩点】

    题目大意:有向关系体现在电脑可以通过网络单向的传输文件,并规定一旦有电脑存在该文件,那么所有它能传输的电脑就能在第一时间得到这个文件,题目有两个问题,第一个是最少向网络中的几台电脑投放文件,能使得整个 ...

  7. poj1236/luogu2746 Network of Schools (tarjan)

    tarjan缩点后,第一问答案显然是入度为零的点得个数第二问:考虑到 没有入度或出度为0的点 的图强连通, 所以答案就是max{入度为零的个数,出度为零的个数} (把出度为零的连到入度为零的点,然后剩 ...

  8. POJ1236:Network of Schools(tarjan+缩点)?

    题目: http://poj.org/problem?id=1236 [题意] N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1 ...

  9. POJ1236学校网络——tarjan

    题目:http://poj.org/problem?id=1236 Tarjan+缩点.温习一下Tarjan的写法. 1.在缩点后的TAG中,有几个联通块等价于有几个入度为0的点! 2.把它们都联通相 ...

随机推荐

  1. 面向切面编程-AOP的介绍

    AOP简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象 ...

  2. django视图函数解析(三)

    1 视图views概述 1 作用: 视图接受web请求并响应web请求 2 本质: 视图就是python中的处理函数 3 响应: 一般是一个网页的HTML内容.一个重定向.错误信息页面.json格式的 ...

  3. .NET部分知识点整理

    .Net学习 Visual Studio2018 企业版:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF 专业版:KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 开发工具常用V ...

  4. Python3基本数据类型(六、字典)

    一.集合介绍 Python set是基本数据类型的一种集合类型,它有可变集合(set())和不可变集合(frozenset)两种.集合是一个无序的,不重复的数据组合,主要作用为:去重(把一个列表变成集 ...

  5. html题型

    1.doctype的意义是什么 这个是有历史背景的,在很久以前,IE有一些自己的渲染模式,最典型的就是盒子模型,包括边距.这就造成了不兼容模式,所以他的意义 1)让浏览器以标准模式渲染 2)让浏览器知 ...

  6. sparkStreamming原理

    一.Spark Streamming 是基于spark流式处理引擎,基本原理是将实时输入的数据以时间片(秒级)为单位进行拆分,然后经过spark引擎以类似批处理的方式处理每个时间片数据. 二.Spar ...

  7. 【题解】洛谷P2532 [AHOI2012]树屋阶梯(卡特兰数+高精)

    洛谷P2532:https://www.luogu.org/problemnew/show/P2532 思路 来自Sooke大佬的推导: https://www.luogu.org/blog/Sook ...

  8. MVC5 模型 生成EF

    在看本篇之前请先去了解一下EF以及如何利用模型生成数据库  https://i.cnblogs.com/posts?categoryid=1107227 看Code First就可以了. 等你了解了E ...

  9. 发布Android程序

    这个选项的意思是说,要使用.NET 2.0的完整版本,而非其子集. 下午发布Apk,一直报错,解决好了,忘记选这个了,以前都记得,明天再去公司发布去

  10. CSS 实战1

    1.CSS 初始化 @charset "UTF-8"; /*css 初始化 */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3 ...