tarjan+缩点+强连通定理
来源:互联网 发布:淘宝旗舰店是不是正品 编辑:程序博客网 时间:2024/05/01 02:04
C - Network of Schools
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64uDescription
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.
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
52 4 3 04 5 0001 0
Sample Output
12
这个题最大的难点在于任务二,任务二的意思就是添加最少的边使一个有向图变成强连通图,有一个定理是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));}}
1 1
- tarjan+缩点+强连通定理
- POJ2186 Tarjan强连通分量+缩点
- Tarjan求强连通(缩点)
- hdu3639 强连通+缩点 tarjan算法
- Tarjan求强连通分量 缩点
- tarjan强连通分量缩点笔记
- poj 2553 (tarjan求强连通分量+缩点)
- PKU2186(Popular Cows)+强连通分支Tarjan算法+缩点
- HDU4635(Strongly connected)Tarjan算法,强连通+缩点
- 强连通分量及缩点tarjan算法解析
- HDU1827 Summer Holiday 强连通 Tarjan 缩点 统计
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
- NYOJ-120校园网络【强连通分量缩点&&tarjan】
- POJ 2186 -- Popular Cows【强连通分支 && Tarjan缩点】
- POJ 2762 强连通分量 Tarjan + 缩点
- [HDU2767]Proving Equivalences(Tarjan缩点+强连通分量)
- [HDU1827]Summer Holiday(Tarjan缩点+强连通分量)
- Codeforces 427C Checkposts【Tarjan强连通+缩点染色】
- Android中的按键和触屏事件
- linux按照日期生成文件
- 25款漂亮的CSS3特效代码
- 如何在移动视频开发中使用ip组播技术
- 第二周 计算器项目总结
- tarjan+缩点+强连通定理
- 一个Java activemq的简单例子
- 【大家说英语】 WHAT ARE YOU AFRAID OF?
- Eclipse : cannot open output file(c++)
- Swift 编程语言入门教程
- 详谈UI线程和Windows消息队列
- 深入浅出K-Means算法
- eclipse创建maven项目报错解决办法
- 必须掌握的八个【cmd 命令行】