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. 将 tomcat 安装成 windows 服务

    1.下载 tomcat 的windows 压缩包,一般以 .zip ,而且文件名中有 bin 的文件就是 2.解压下载的文件到某一个目录下,eg: TOMCAT_HOME 3.打开 cmd ,运行 % ...

  2. JS 判断 undefined 类型

    typeof 返回的是字符串,有六种可能:"number"."string"."boolean"."object".&q ...

  3. python测试开发django-30.发送附件EmailMessage

    前言 Django的 send_mail() 和 send_mass_mail() 函式事实上是对 EmailMessage 类使用方式 的一个轻度封装.send_mail() 和相关的其他封装函式并 ...

  4. Windows下LuaJIT的编译和使用

    1.下载LuaJIT,download>> 2.编译 开始 –> 程序 –> Microsoft Visual Studio xx –> Visual Studio To ...

  5. XCode的The argument is invalid

      google查了一下,没找到解决的方法.有一篇blog应该有写怎么解决,可惜是wordpress.com的站点,打不开,网页快照也看不了-   后来回忆了一下操作步骤,只是添加了cocos2dx的 ...

  6. cocos2d-x绑lua的开发环境

    2013年是手游开发井喷的一年,也是手游市场竞争最为激烈的一年,ios市场除了刷榜.刷榜,还是刷榜,而android有点像黑市的感觉,水太深(很多渠道商已经从上游控制了流量的入口).而cocos2d- ...

  7. Android之2次打开添加友盟统计代码,后缀会添加广告

    这里首先列明步骤, 做一个标识仅此而已. 1. 首先使用apktool来反编译你待需要加入友盟统计的apk包, 具体如何使用与配置apktool, 请参考我关于apktool配置的文章. 2. 然后自 ...

  8. Python数据分析笔记

    最近在看Python数据分析这本书,随手记录一下读书笔记. 工作环境 本书中推荐了edm和ipython作为数据分析的环境,我还是刚开始使用这种集成的环境,觉得交互方面,比传统的命令行方式提高了不少. ...

  9. 使用Logstash创建ES映射模版并进行数据默认的动态映射规则

    本文配置为 ELK 即(Elasticsearch.Logstash.Kibana)5.5.1. Elasticsearch 能够自动检测字段的类型并进行映射,例如引号内的字段映射为 String,不 ...

  10. 硬盘杀手!Windows版Redis疯狂占用C盘空间!

    关键词:Redis占用C盘,Windows Redis,64位Windows版Redis疯狂占用C盘空间,redis启动后创建RedisQFolk_****.dat文件.redis-server.ex ...