It's not Floyd Algorithm

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte
 

描述

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 / 强连通分量的更多相关文章

  1. ZOJ 3232 It's not Floyd Algorithm --强连通分量+Floyd

    题意:给你一个传递闭包的矩阵,mp[u][v] = 1表示u可以到达v,为0代表不可到达,问你至少需要多少条边组成的传递闭包符合这个矩阵给出的关系 分析:考虑一个强连通分量,如果这个分量有n个节点,那 ...

  2. zoj 3232 It's not Floyd Algorithm(强联通分量,缩点)

    题目 /******************************************************************/ 以下题解来自互联网:Juny的博客 思路核心:给你的闭包 ...

  3. [POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

    题目链接:http://poj.org/problem?id=1236 这题本来是个强连通分量板子题的,然而弱很久不写tarjan所以生疏了一下,又看这数据范围觉得缩点这个事情可以用点到点之间的距离来 ...

  4. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  5. ZOJ 17届校赛 Knuth-Morris-Pratt Algorithm( 水题)

    In computer science, the Knuth-Morris-Pratt string searching algorithm (or KMP algorithm) searches f ...

  6. ZOJ 3795 Grouping 强连通分量-tarjan

    一开始我还天真的一遍DFS求出最长链以为就可以了 不过发现存在有向环,即强连通分量SCC,有向环里的每个点都是可比的,都要分别给个集合才行,最后应该把这些强连通分量缩成一个点,最后保证图里是 有向无环 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. zoj 2760(网络流+floyed)

    How Many Shortest Path Time Limit: 10 Seconds      Memory Limit: 32768 KB Given a weighted directed ...

  9. 最短路径之Floyd算法

    Floyd算法又称弗洛伊德算法,也叫做Floyd's algorithm,Roy–Warshall algorithm,Roy–Floyd algorithm, WFI algorithm. Floy ...

随机推荐

  1. TXMLDocument use case (Delphi)

    Description This example illustrates the basic operations on an XML document. Code procedure CreateD ...

  2. SQLCE使用本地数据库优化

    一.数据绑定 1.使用数据虚拟化和SKIP/TAKE 使用 Skip 和 Take 方法可确保直到需要在 ListBox 控件中显示数据时才将数据库中的数据加载到内存中. 例如,以下代码显示了如何从数 ...

  3. redis实现异步任务队列

    redis实现异步任务队列 先说思路: 将任务对象序列为JSON字符串,然后推入REDIS缓存,这叫入队. 通过独立的工作线程从REDIS拉出一个任务,这叫出队,工作线程将JSON字符串还原为任务对象 ...

  4. Xcode 5.1.1 与 Xcode 6.0.1 的共存之路(建议大家在升级Xcode 6.0.1 的时候保留Xcode 5.1.1)

    最近升级了Xcode 6.0.1 与原有项目有不少冲突.建议大家谨慎升级,同时做好备份.二者共存推荐如下帖子. http://jingyan.baidu.com/article/1612d500457 ...

  5. python测试开发django-39.xadmin详情页面布局form_layout

    前言 xadmin的详情页面默认是一行展示一个字段,可以使用form_layout对详情页面的布局重新设计. 可以设置必填和非必填字段,也可以设置不显示,不可以编辑的字段. models模块 先在mo ...

  6. 将应用升级到了 Spring 4.1.0+quartz-2.2.1 还是很给力一样的兼容

    应用采用了集群3个tomcat,一个weblogic12c 后台数据应用

  7. SVN更新的问题

    更新的时候一直遇到"Base checksum mismatch on"或者"Checksum mismatch while updating",其它文件可以提 ...

  8. linux find 10天内改动过的文件

    find . -name "*.h" -mtime -10 -type f -print find . -regex ".*\.\(c\|h\)" -mtime ...

  9. 解决tensorflow在训练的时候权重是nan问题

    搭建普通的卷积CNN网络. nan表示的是无穷或者是非数值,比如说你在tensorflow中使用一个数除以0,那么得到的结果就是nan. 在一个matrix中,如果其中的值都为nan很有可能是因为采用 ...

  10. Inferred type 'S' for type parameter 'S' is not within its bound; should extend

    在使用springboot 方法报错: Inferred type 'S' for type parameter 'S' is not within its bound; should extends ...