POJ1236_A - Network of Schools _强连通分量::Tarjan算法
| Time Limit: 1000MS | Memory Limit: 10000K |
Description
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
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
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
题意
有向图上有 N 个点,若干有向边。
第一问:至少给几个点传递信息,才能保证信息传遍整个图。
第二问:至少添加几条边,才能使任意选择点,都能传遍整个图。
思路
强连通分量的裸题。
强连通分量内的任意一点收到消息,内部其他各点必定都能收到消息。因此,可以把每个强连通分量缩成一个点。只需要考察入度为 0 的强连通分量的个数,就是第一问的答案。
对于第二问,是把图连接成一个强连通分量,同样可以在缩点后的图中操作。这里的做法是统计图中入度为0、出度为0的强连通分量的个数,取较大值即为第二问的答案。 本题中原图只有一个强连通分量的情况需要特判。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector> using namespace std; const int maxn = + ; int N;
int In[maxn], Out[maxn]; /***************************Tarjan算法模板***************************/
vector<int> G[maxn];
int Mark[maxn], Root[maxn], Stack[maxn];//时间戳,根(当前分量中时间戳最小的节点),栈
bool Instack[maxn]; //是否在栈中标记
int Ssc[maxn]; //每个节点所在的强连通分量的编号
int Index, Ssc_n, Top; //搜索时用的时间戳,强连通分量总数,栈顶指针 void Tarjan(int u) //u 当前搜索到的点
{
Mark[u] = Root[u] = ++ Index; //每找到一个点,对时间戳和根初始化
Stack[Top ++] = u; //压栈
Instack[u] = true; //在栈中标记 int v; for(int i= ; i< G[u].size(); i++) //向下搜索
{
v = G[u][i];
if(Mark[v] == ) //没到过的点
{
Tarjan(v); //先向下搜索
if(Root[u] > Root[v]) Root[u] = Root[v];//更新根
}
else if(Instack[v] && Root[u] > Mark[v]) Root[u] = Mark[v]; //到过的点且点仍在栈中,试着看这个点能不能成为根
}
/*对当前点的搜索结束*/
if(Mark[u] == Root[u]) //当前点本身时根
{
Ssc_n ++; //更新强连通分量数 do{ //栈中比它后入栈的元素在以它为根的强连通分量中
v = Stack[-- Top];
Instack[v] = false;
Ssc[v] = Ssc_n;//把同一个强连通分支的点做上相同标记
}while(v != u); //直到它自己
}
} void SSC()
{
memset(Mark, , sizeof Mark); //初始化时间戳和栈内标记
memset(Instack, false, sizeof Instack);
Index = Ssc_n = Top = ; //初始化时间戳,强连通分量数,栈顶指针 for(int i= ; i<= N; i++) //保证图上所有点都访问到
if(Mark[i] == ) Tarjan(i);
}
/***************************Tarjan算法模板***************************/ int main()
{
//freopen("in.txt", "r", stdin); scanf("%d", &N);
for(int i= ; i<= N; i++)
{
int x;
while(scanf("%d", &x), x)
G[i].push_back(x);
} SSC(); if(Ssc_n == ) //只有一个强连通分量的情况
{
cout << "1\n0\n";
return ;
} memset(In, , sizeof In); //求每个强连通分量的入度和出度
memset(Out, , sizeof Out);
for(int u= ; u<= N; u++)
{
for(int i= ; i< G[u].size(); i++)
{
int v = G[u][i];
if(Ssc[u] != Ssc[v])//u,v两点不在同一个强连通分支
Out[Ssc[u]] ++, In[Ssc[v]] ++;
}
} int S1 = , S2 = ;//找入度为0、出度为0的点的数目
for(int i= ; i<= Ssc_n; i++)
{
if(In[i] == ) S1 ++;
if(Out[i] == ) S2 ++;
} cout << S1 << endl << max(S1, S2) << endl; return ;
}
POJ1236_A - Network of Schools _强连通分量::Tarjan算法的更多相关文章
- POJ 1236 Network of Schools(强连通分量/Tarjan缩点)
传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...
- 有向图强连通分量Tarjan算法
在https://www.byvoid.com/zhs/blog/scc-tarjan中关于Tarjan算法的描述非常好,转述如下: 首先解释几个概念: 有向图强连通分量:在有向图G中,如果两个顶点间 ...
- poj1236 Network of Schools【强连通分量(tarjan)缩点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4316263.html ---by 墨染之樱花 [题目链接]http://poj.org/pr ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools (强连通分量+缩点)
题目大概: 每个学校都可以把软件复制好,交给它名单上的学校. 问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件. 问题B:添加几条边,能使得这个图变成强连通图. 思路: 找出所有的 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- 有向图强连通分量 Tarjan算法
[有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连通图.非强连通图有向图的极 ...
- [有向图的强连通分量][Tarjan算法]
https://www.byvoid.com/blog/scc-tarjan 主要思想 Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树.搜索时,把当前搜索树中未处理的 ...
- 图的连通性:有向图强连通分量-Tarjan算法
参考资料:http://blog.csdn.net/lezg_bkbj/article/details/11538359 上面的资料,把强连通讲的很好很清楚,值得学习. 在一个有向图G中,若两顶点间至 ...
随机推荐
- js常用函数汇总(不定期更新)
1.图片按比例压缩 function setImgSize(){ var outbox_w=imgbox.width(), outbox_h=imgbox.height(); imgbox.find( ...
- June 06th 2017 Week 23rd Tuesday
At the touch of love, everyone becomes a poet. 一谈到爱,每个人都变成了一位诗人. Sweet words always have the power o ...
- 建立自己的git账户并保存资料的重要性
Github是一个代码托管平台和开发者社区,开发者可以在Github上创建自己的开源项目并与其他开发者协作编码.创业公司可以用它来托管软件项目,开源项目可以免费托管,私有项目需付费.[2] GitHu ...
- NEUACM1132: Renew MST Quickly 增量最小生成树
题目链接:http://acm.neu.edu.cn/hustoj/problem.php?id=1132 和UVa11354很类似 题意: 原先有一棵树,每次加一条边,看最小生成树大小: 这个和增量 ...
- 生理周期,POJ(1006)
题目链接:http://poj.org/problem?id=1006 解题报告: 1.枚举天数的时候可以根据前面的结果直接跳过一些错误的答案. ///三个周期是23,28,33, #include ...
- CF25E Test
嘟嘟嘟 因为只有三个字符串,所以就有一个比较暴力的做法:枚举这三个串所有排列,然后对于每一个排列,减去这三个串两两的公共部分的长度,更新答案. 求公共部分自然想到kmp:比如s[1]接在s[0]后面, ...
- 【洛谷P1118】数字三角形
数字三角形 题目链接 4 16 3 1 2 4 3 1 2 4 (3+1) (1+2) (2+4)(3+1+1+2) (1+2+2+4) (3+1+1+1+2+2+2+4)16=1*3+3*1+3*2 ...
- 【luogu P2002 消息扩散】 题解
题目链接:https://www.luogu.org/problemnew/show/P2002 缩点把原图变为DAG,再在DAG上判断找入度为0的点的个数. 注意一点出度为0的点的个数不等于入度为0 ...
- iOS程序的启动原理
一.在一些老的项目中,有很多不是通过代码添加窗口和跟控制器.是通过设置MainInterface(设置最主要的xib),在xib中通过图形化的操作设置应用程序的代理->设置窗口->设置根控 ...
- 我的wmware
1.vmware 网络连接方式 NAT 模式: 虚拟机的IP 是由NAT分配的,电脑环境无论如何变化,都不会影响虚拟机 好处:在家.学校.公司,连接虚拟机都可以使用相同的ip地址 桥接模式: 只要更换 ...