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. [CODECHEF]EASYEX

    题意:有一个$k$面的骰子,上面的数字为$1\cdots k$,现在要丢$n$次骰子,设$n$次中有$a_i$次扔到数字$i$,给定$l,f$,求$\prod\limits_{i=1}^la_i^f$ ...

  2. [转载]C++内存管理

    [导语] 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了更好的性能,更大的自由,C++菜鸟的收获则是一遍一遍的检查代码和对C++的痛恨,但内存管理在C++中无处不 ...

  3. Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形

    题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...

  4. poj2117 Electricity

      试题描述 求一个图删除一个点之后,联通块最多有多少. 输入 多组数据.第一行两个整数 P,C 表示点数和边数.接下来 C 行每行两个整数 p1,p2,表示 p1 与 p2 有边连接,保证无重边.读 ...

  5. 关于图表第三方Charts的一些理解与总结

    最近项目中用到了很多的图表,如柱状图,线状图,饼状图等等.接触到了一个新的第三方Charts,在做图方面确实非常强大,在使用了一段时间后,今天对他进行一个小的总结,也是自己的一点小理解. 关于char ...

  6. Get just enough boost voltage - current-mirror circuit - VOUT tracks VIN varies

    Adding a current-mirror circuit to a typical boost circuit allows you to select the amount of boost ...

  7. LCD Backlight circuit

  8. matlab画直线,指定斜率与x坐标范围

    闲话不说,直接上代码与图的效果!

  9. Unity5.0与Android交互

    1. 目标 1) Unity3D可调用Android Java函数(在.jar中) 2) Java可调用Unity3D函数 3) Unity3D可调用Android C函数(在.so中) 2. 测试环 ...

  10. 关于fmri数据分析的两大类,四种方法

    关于fmri数据分析的两大类,四种方法: 数据驱动: tca:其实这种方法,主要是提取时间维的特征.如果用它来进行数据的分析,则必须要利用其他的数据方法,比如结合ICA. ica:作为pca的一般化实 ...