TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量
It's not Floyd Algorithm
描述
When a directed graph is given, we can solve its transitive closure easily using the well-known Floyd algorithm.
But if you're given a transitive closure, can you give a corresponding directed graph with minimal edges?
输入
About 100 test cases, seperated by blank line.
First line of each case is an integer N (1<=N<=200). The followingN lines represent the given transitive closure in 0-1 matrix form, each line hasN numbers.
输出
For each case, just output the number of minimal edges of a directed graph which has a given transitive closure.
样例输入
1
1 2
1 0
0 1 2
1 1
1 1 3
1 1 1
0 1 1
0 0 1
样例输出
0
0
2
2
提示
Transitive closure can be presented as a matrix T, where Ti,j is true if and only if there is a path from vertexi toj.
首先缩点 在建图 对于每个强连通分量如果有n个点那么最少只需n条边就可以联通(n = 1 除外)
然后对于缩点后的图咋做一次反闭包 去掉多余的边 在统计一下
#include<stdio.h>
#include<string.h>
int n;
int dfn[210];
int low[210];
bool instack[210];
int stack[210];
int cnt,num,top;
int a[210][210];
int count[210];
int belong[210];
int map[210][210];
void floyd()
{
int i,j,k;
for(k = 1;k <= cnt; k++)
for(i = 1;i <= cnt; i++)
for(j = 1;j <= cnt; j++)
if(map[i][k] && map[k][j] && map[i][j])
map[i][j] = 0;
}
void tarjan(int i)
{
int j,k;
dfn[i] = low[i] = ++num;
instack[i] = true;
stack[++top] = i;
for(j = 1;j <= n; j++)
{
k = a[i][j];
if(!k)
continue;
if(!dfn[j])
{
tarjan(j);
if(low[i] > low[j])
low[i] = low[j];
}
else if(instack[j] && low[i] > dfn[j])
low[i] = dfn[j];
}
if(low[i] == dfn[i])
{
cnt++;
do
{
j = stack[top--];
instack[j] = false;
belong[j] = cnt;
count[cnt]++;
}
while(i != j);
}
}
int main()
{
int i,j,sum;
while(scanf("%d",&n)!=EOF)
{
for(i = 1;i <= n; i++)
for(j = 1; j<= n; j++)
scanf("%d",&a[i][j]);
num = top = cnt = 0;
memset(dfn,0,sizeof(dfn));
memset(instack,false,sizeof(instack));
memset(count,0,sizeof(count));
memset(map,0,sizeof(map));
for(i = 1;i <= n; i++)
if(!dfn[i])
tarjan(i);
for(i = 1; i<= n; i++)
for(j = 1; j<= n; j++)
if(a[i][j])
if(belong[i] != belong[j])
map[belong[i]][belong[j]] = 1;
sum = 0;
floyd();
for(i = 1;i <= cnt; i++)
if(count[i] != 1)
sum += count[i];
for(i = 1;i <= cnt; i++)
for(j = 1; j<= cnt; j++)
if(map[i][j])
sum++;
printf("%d\n",sum);
}
return 0;
}
TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量的更多相关文章
- ZOJ 3232 It's not Floyd Algorithm --强连通分量+Floyd
题意:给你一个传递闭包的矩阵,mp[u][v] = 1表示u可以到达v,为0代表不可到达,问你至少需要多少条边组成的传递闭包符合这个矩阵给出的关系 分析:考虑一个强连通分量,如果这个分量有n个节点,那 ...
- zoj 3232 It's not Floyd Algorithm(强联通分量,缩点)
题目 /******************************************************************/ 以下题解来自互联网:Juny的博客 思路核心:给你的闭包 ...
- [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)
题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
- ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)
In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches f ...
- ZOJ 3795 Grouping 强连通分量-tarjan
一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- zoj 2760(网络流+floyed)
How Many Shortest Path Time Limit: 10 Seconds Memory Limit: 32768 KB Given a weighted directed ...
- 最短路径之Floyd算法
Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...
随机推荐
- 有关Delphi RTTI的一些文章
Delphi RTTI 资料 Delphi 的RTTI机制浅探 Delphi XE的RTTI增强,动态Hook某些内部事件
- ArcGIS Pro 切割打印
ArcGIS Pro 切割打印
- excel 鼠标上下左右移动
.Offset用法:(如果是多选单元格,偏移后选定的依然是区域) Selection.Offset(-1).select 'up Selection.Offset(1).select 'down ...
- 关于面试总结4-python笔试题
前言 现在面试测试岗位,一般会要求熟悉一门语言(python/java),为了考验求职者的基本功,一般会出2个笔试题,这些题目一般不难,主要考察基本功. 要是给你一台电脑,在编辑器里面边写边调试,没多 ...
- Java从网络批量读取图片并保存至本网站服务器后再插入文章中
先来看效果: 查看一下系统操作日志 删除
- 关于websocket集群中不同服务器的用户间通讯问题
最近将应用部署到集群时遇到一个问题,即用户命中不同的服务器导致的用户间无法进行websocket通讯,在网上搜索到类似问题但都没有具体解决方案. 于是用redis的订阅发布功能解决了该问题,具体流程如 ...
- Android之代码创建布局
大概描述一下效果:最外层是一个 RelativeLayout 里面有自定义个LinearLayout,每个LinearLayout有两个TextView.that's it !!! private v ...
- SIFT(Scale-invariant feature transform) & HOG(histogram of oriented gradients)
SIFT :scale invariant feature transform HOG:histogram of oriented gradients 这两种方法都是基于图像中梯度的方向直方图的特征提 ...
- jQuery中attr和prop方法的区别说明
jquery中attr和prop的基本区别可以理解为:如果是内置属性,建议用prop,如果是自定义的建议用attr. 例如 <input type=check node=123 id=ck & ...
- ASP.NET Razor 简介
ylbtech-.NET: ASP.NET Razor 简介 Razor 不是一种编程语言.它是服务器端的标记语言. 1. 什么是 Razor?返回顶部 Razor 是一种标记语法,可以让您将基于服务 ...