tarjan+缩点+强连通定理
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d
& %I64u
Description
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
of school i. Each list ends with a 0. An empty list contains a 0 alone in the line.
Output
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+缩点+强连通定理的更多相关文章
- Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载)
Tarjan算法应用 (割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)问题)(转载) 转载自:http://hi.baidu.com/lydrainbowcat/blog/item/2 ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- King's Quest —— POJ1904(ZOJ2470)Tarjan缩点
King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...
- 【BZOJ-2438】杀人游戏 Tarjan + 缩点 + 概率
2438: [中山市选2011]杀人游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1638 Solved: 433[Submit][Statu ...
- 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
- [Usaco2015 Jan]Grass Cownoisseur Tarjan缩点+SPFA
考试的时候忘了缩点,人为dfs模拟缩点,没想到竟然跑了30分,RB爆发... 边是可以重复走的,所以在同一个强连通分量里,无论从那个点进入从哪个点出,所有的点一定能被一条路走到. 要使用缩点. 然后我 ...
随机推荐
- [转]php-fpm - 启动参数及重要配置详解
约定几个目录/usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini 一,php-fpm ...
- [CF126D]Fibonacci Sums/[BJOI2012]最多的方案
[CF126D]Fibonacci Sums/[BJOI2012]最多的方案 题目大意: 将\(n(n\le10^9)\)表示成若干个不同斐波那契数之和的形式,求方案数. 思路: 如果不考虑\(0\) ...
- bzoj 3809 莫队
收获: 1.分块时顺便记录每个位置所属的块,然后一次排序就OK了. 2.要权衡在“区间移动”与“查询结果”之间的时间,莫队算法一般区间移动频率远大于查询结果,所以我们选择的辅助数据结构时就要注意了,我 ...
- Selenium自动化工具工作原理
http://blog.csdn.net/five3/article/details/6790925 原文作者信息如下,需要更多信息请去原作者博客查看: 作者:hyddd 出处:http://www. ...
- 普通免费QQ客服在PC、手机端解决方案
注意:以下测试 浏览器在Chrome,手机在iphone6 营销QQ.企业QQ(http://b.qq.com/)跟普通免费QQ(http://shang.qq.com/v3/widget.html) ...
- POJ 3384 Feng Shui (半平面交)
Feng Shui Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3743 Accepted: 1150 Speci ...
- LT1961 升压型稳压器造就了兼具升压和降压能力的扁平状SEPIC
http://cds.linear.com/docs/cn/design-note/DN317%20sim_chs.pdf http://cds.linear.com/docs/en/datashee ...
- IDA 中文字符串
http://www.pediy.com/kssd/pediy05/pediy50528.htm Ida Pro 的默认设置里对中文字串的支持比较差,对于首字节大于'E0'的都显示成?了.其实... ...
- JavaScript在IE6下超级链接window.location.href不跳转的bug 及 解决方案
今天遇到个很诡异的问题,就是<a href="javascript:void(0);" onclick="window.location.href=url" ...
- GCC降级
前阵子将Ubuntu升级到了12.04,原来装得virtualbox也可以正常使用.后来几次内核升级之后,virtualbox突然不能用了.virtualbox提示进行/etc/init.d/vbox ...