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. NOIP 2018 提高组初赛解题报告

    单项选择题: D 进制转换题,送分: D 计算机常识题,Python是解释运行的: B 常识题,1984年小平爷爷曰:“娃娃抓起”: A 数据结构常识题,带进去两个数据就可以选出来: D 历年真题没有 ...

  2. CSS 笔记——导航栏、下拉菜单、提示工具

    8. 导航栏.下拉菜单.提示工具 (1)导航栏 垂直导航栏 <!DOCTYPE html> <html lang="en"> <head> &l ...

  3. 升级到php7和安装拓展(mac centos)

    Mac升级到php7 使用homebrew安装php7 brew update #更新源 brew search php #查找源中的php,发现有php7.1版本,安装最新的php7.1 brew ...

  4. Jenkins使用jenkins-cli.jar进行远程调用时出现“ERROR: No such job 'test'”或者权限不够等问题解决(Windows)

    网上最提倡的解决办法是用SSH的key进行登录,但是我发觉Linux上非常容易实现,但是Windows压根不知道在哪里设置. 原文:https://issues.jenkins-ci.org/brow ...

  5. 设计模式 - 观察者模式(Observer Pattern) Java内置 用法

    观察者模式(Observer Pattern) Java内置 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26601659 ...

  6. HDFS API基本操作

    对HDFS API基本操作都是通过 org.apache.hadoop.fs.FileSystem类进行的,以下是一些常见的操作: package HdfsAPI; import java.io.Bu ...

  7. Python中用MacFSEvents模块监视MacOS文件系统改变一例

    最近一个项目中用gulp-watch不能满足需求,于是想到了用Python来解决问题.在安装了MacFSEvents模块后,写了下面一个小程序. #!/usr/bin/env python2 #-*- ...

  8. Spring内部bean实例

    在Spring框架中,一个bean仅用于一个特定的属性,这是提醒其声明为一个内部bean.内部bean支持setter注入“property”和构造器注入"constructor-arg“. ...

  9. Windows Embedded Compact 7网络编程概述(上)

    如今,不论是嵌入式设备.PDA还是智能手机,网络都是必不可少的模块.网络使人们更方便地共享设备上的信息和资源.而且,利用智能手机浏览互联网,也逐渐成为生活中的常见手段.物联网所倡导的物物相联,也离不开 ...

  10. spring boot配置springMVC拦截器

    spring boot通过配置springMVC拦截器 配置拦截器比较简单, spring boot配置拦截器, 重写preHandle方法. 1.配置拦截器: 2重写方法 这样就实现了拦截器. 其中 ...