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

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 注意:
代码有误,谨慎观看(仍可AC) 思路:
求最大强连通分量,缩点。算出出度为0和入度为0的点的个数。ans1就是入度为0的点的个数,因为他们不能从其他点获取,而其他的点一定可以从其他点获取。
ans2就是入度为0的点和出度为0的点的个数的最大值,因为你需要把所有的点都弄得有入度和出度。
代码
如果我的推理没有错的话,我的代码应该是错的,只是我强行水过去了。因为正常情况下,除非只有一个强连通分量,ans1不会为0,而我测数据测出来0,所有我强行改得符合数据(就是那个max),没想到居然过了
这一切的根源,都是我的Tarjan模板有问题,而HDU竟然让我用那个模板过了一题,看来我要重学Tarjan了。
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
vector<int>u[];
int num[],low[];
bool book[];
int index;
int in[],out[];
bool flag[];
void Tarjan_dfs(int t)
{
index++;
low[t]=num[t]=index;
book[t]=true;
int siz=u[t].size();
for(int i=;i<siz;i++){
if(!book[u[t][i]]){
Tarjan_dfs(u[t][i]);
}
if(!flag[u[t][i]]){low[t]=min(low[t],low[u[t][i]]);}
}
} int main()
{
int n,x;
scanf("%d",&n);
for(int i=;i<=n;i++){
while(true){
scanf("%d",&x);
if(x==){break;}
u[i].push_back(x);
}
}
for(int i=;i<=n;i++){
if(!book[i]){Tarjan_dfs(i);}
for(int i=;i<=n;i++){
if(num[i]!=){flag[i]=true;}
}
}
for(int i=;i<=n;i++){
for(int j=;j<u[i].size();j++){
if(low[i]!=low[u[i][j]]){
out[low[i]]++;
in[low[u[i][j]]]++;
}
}
}
int ans1,ans2;
ans1=ans2=;
int numv=;
for(int i=;i<=n;i++){
if(num[i]==low[i]){
numv++;
if(in[low[i]]==){ans1++;}
if(out[low[i]]==){ans2++;}
}
}
if(numv==){printf("1\n0\n");}
else printf("%d\n%d\n",max(,ans1),max(ans2,ans1));
}

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

  1. POJ 1236 Network of Schools(tarjan)

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

  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. SQL约束(主键约束、外键约束、自动递增、不允许空值、值唯一、值默认、值限制范围)

    NOT NULL 不允许空值约束 NOT NULL 约束强制列不接受 NULL 值(NULL值就是没有值或缺值).NOT NULL 约束强制字段始终包含值,即不向字段添加值,就无法插入新记录或者更新记 ...

  2. 刪除nodejs

    https://www.cnblogs.com/fighxp/p/7410235.html https://www.cnblogs.com/fighxp/p/7411608.html

  3. Hibernate最佳实战

    1:一对一,一对多,多对多双向管理必设mappedBy ,将关系交给乙方维护,不然的话会在双方都建立关系,比如一对一双向的时候双方都会保存对方的id外键管理 具体在项目中采用双向还是单项看实际情况. ...

  4. Jenkins+PowerShell持续集成环境搭建(六)参数化构建

    参数化构建可以应用于动态绑定源码地址等情况. 勾选“This build is parameterized”: 如果需要动态绑定源码地址,参考: 配置完成后构建项目变成:

  5. PlaNet,使用图像输入来学习世界模型

    Google AI团队与DeepMind合作,上周宣布了一个名为PlaNet的新的开源“Deep Planning”网络. PlaNet是一个人工智能代理,它只使用图像输入来学习世界模型,并使用这些模 ...

  6. Codeforces715 B. Complete The Graph

    传送门:>Here< 题意:给出一张带权无向图,其中有一些边权为0.要求将边权为0的边的边权重置为一个任意的正整数,使得从S到T的最短路为L.判断是否存在这种方案,如果存在输出任意一种 解 ...

  7. 将xml文件由格式化变为压缩字符串

    标签:去除xml文件的空格 有些时候解析xml文件,要求读取的字符串必须是压缩后的xml文件,不能有多余的空格.考虑到在<>标签内包含空格和大于号的情况,写了以下的转换方式. 传入的是压缩 ...

  8. MT【264】分式变形

    已知$x,y>0,\dfrac{1}{x}+\dfrac{2}{y}=1$,求$\dfrac{1}{x+1}+\dfrac{2}{y+1}$的最大值____ 解答:令$a=\dfrac{1}{x ...

  9. Treap树 笔记

    预备知识:二叉查找树.堆(heap).平衡二叉树(AVL)的基本操作(左旋右旋) 定义: Treap.平衡二叉树.Tree+Heap.树堆. 每个结点两个键值(key.priority). 性质1. ...

  10. Docker 私有仓库 Harbor registry 安全认证搭建 [Https]

    Harbor源码地址:https://github.com/vmware/harborHarbort特性:基于角色控制用户和仓库都是基于项目进行组织的, 而用户基于项目可以拥有不同的权限.基于镜像的复 ...