C - Network of Schools

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d
& %I64u

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

这个题最大的难点在于任务二。任务二的意思就是加入最少的边使一个有向图变成强连通图,有一个定理是max(n,m)当中n是出度为0的点的个数,m是入度为0的点的个数,当然,假设这个图是强连通图的话。就须要讨论了,这时答案就是0了。将第二个问题解决掉。这个题就是一个模板题了,可是我如今仍没有证明这个命题的正确性!有大神说显然。我认为应该能够证出来。希望看到这个博客的大神能够帮帮忙。谢谢。


#include<stdio.h>
#include<stack>
#include<string.h>
#include<algorithm>
using namespace std;
int dfn[120];
int belong[120],bnt,instack[120];
int index,out[120],in[120],low[120];
int map[120][120];
stack<int>S;
void tarjan(int i){
dfn[i] = low[i] = ++index;
S.push(i);
instack[i] = 1;
for(int j = 1;j<=map[i][0];j++){
int k = map[i][j];
if(!dfn[k]){
tarjan(k);
low[i] = min(low[i],low[k]);
}
else if(instack[k])
low[i] = min(low[i],dfn[k]);
}
if(low[i] == dfn[i]){
bnt++;
int j;
do{
j = S.top();
S.pop();
instack[j] = 0;
belong[j] = bnt;
}while(i!=j);
}
}
int main(){
int n,m,ans,ans1;
while(~scanf("%d",&n)){
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(instack,0,sizeof(instack));
memset(out,0,sizeof(out));
memset(in,0,sizeof(in));
memset(map,0,sizeof(map));
bnt = index = 0;
ans = ans1 = 0;
for(int i=1;i<=n;i++){
while(scanf("%d",&m),m)
if(m)map[i][++map[i][0]] = m;
}
for(int i=1;i<=n;i++)
if(!dfn[i])tarjan(i);
for(int i=1;i<=n;i++){
for(int j = 1;j<=map[i][0];j++){
if(belong[i]!=belong[map[i][j]]){
out[belong[i]]++;
in[belong[map[i][j]]]++;
}
}
}
for(int i=1;i<=bnt;i++){
if(out[i]==0)ans++;
if(in[i]==0)ans1++;
}
printf("%d\n",ans1);
if(bnt ==1)printf("0\n");
else printf("%d\n",max(ans1,ans));
}
}

tarjan+缩点+强连通定理的更多相关文章

  1. Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)

    Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...

  2. POJ 1236 Network of Schools(强连通 Tarjan+缩点)

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

  3. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

  4. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

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

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

  6. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  7. 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率

    2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1638  Solved: 433[Submit][Statu ...

  8. 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1685  Solved: 724[Submit] ...

  9. [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA

    考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...

随机推荐

  1. bzoj 1674: [Usaco2005]Part Acquisition -- dijkstra(堆优化)

    1674: [Usaco2005]Part Acquisition Time Limit: 5 Sec  Memory Limit: 64 MB Description The cows have b ...

  2. Unity UGUI之Text

    下图是Text组件的内容. Character(字符) Text--输入要显示的文本 Font--要渲染文本的字体类型(例如:黑体.宋体) FontStyle--是否要加粗,倾斜等. Normal-- ...

  3. sklearn中的超参数调节

    进行参数的选择是一个重要的步骤.在机器学习当中需要我们手动输入的参数叫做超参数,其余的参数需要依靠数据来进行训练,不需要我们手动设定.进行超参数选择的过程叫做调参. 进行调参应该有一下准备条件: 一个 ...

  4. StringFormat

    public class StringFormatDemo { public static void main(String[] args) { String str = null; str = St ...

  5. JVM -XX: 参数介绍

    功能开关: 参数 默认值或限制 说明 参数 默认值 功能 -XX:-AllowUserSignalHandlers 限于Linux和Solaris,默认不启用 允许为java进程安装信号处理器,信号处 ...

  6. 单向可控硅(SCR)双向可控硅(TRIAC)

    双向可控硅工作原理与特点 从理论上来讲,双向可控硅可以说是有两个反向并列的单向可控硅组成,理解单向可控硅的工作原理是理解双向可控硅工作原理的基础 单向可控硅 单向可控硅也叫晶闸管,其组成结构图如图1- ...

  7. linux shell 正则表达式(BREs,EREs,PREs)差异比较(转,当作资料查)

    转载: 在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或 替换那些符合某个模式的文本内容.许多程序设计语 ...

  8. Android开发中适配多种 ROM 的快捷方式是如何实现的?

    在安卓开发中,要提高开发效率,掌握一些快捷方式是必不可少的,特别是对于android入门阶段的童鞋而言,非常重要.今天小编在安卓开发教程网站上,搜罗了一些常用的Android 适配多种 ROM 的快捷 ...

  9. Netty框架

    Netty框架新版本号:3.0.2.GA,于2008年11月19日公布.Netty项目致力于提供一个异步的.事件驱动的网络应用框架和工具,用于高速开发可维护的.高性能的.高扩展性的server和cli ...

  10. https://github.com/wytings

    博客中写了很多比较杂乱的东西,有时候可能一时看不出效果,毕竟代码问题确实是 “Talk is cheap. Show me the code” 所以,就开了一个github,把一些日常开发和使用的工具 ...