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 ...
随机推荐
- 微信破解,解密?How To Decrypt WeChat EnMicroMsg.db Database?
20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送) 国内私募机构九鼎控股打造,九鼎投资是在全国股 ...
- endnote插入参考文献后的对齐方式和缩进空格
原文链接 https://jingyan.baidu.com/article/63acb44a3f0f6161fcc17ed0.html 1 2 3 4 5 6 7 分步阅读 1.endnote 导入 ...
- IDEA 快速将spring boot项目打包成jar包,简单快速有效
原文地址;https://blog.csdn.net/chen846262292/article/details/80701101 https://www.cnblogs.com/chrischen ...
- 「BZOJ」「3262」陌上花开
CDQ分治 WA :在solve时,对y.z排序以后,没有处理「y.z相同」的情况,也就是说可能(1,2,3)这个点被放到了(2,2,3)的后面,也就是统计答案在前,插入该点在后……也就没有统计到! ...
- c#利用SWIG调用c++dll学习总结【转】
开发环境: 操作系统:windows 7 IDE:Microsoft Visual Studio Professional 2015 SWIG: 3.0.12 swig的介绍 详细介绍可看官网,一下贴 ...
- [leetcode]Remove Duplicates from Sorted List II @ Python
原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ 题意: Given a sorted link ...
- [leetcode]Unique Binary Search Trees @ Python
原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/ 题意: Given n, how many structurally ...
- GIST特征描述符使用
来源:http://www.cnblogs.com/justany/archive/2012/12/06/2804211.html 一种场景特征描述 场景特征描述? 通常的特征描述符都是对图片的局部特 ...
- 分布式高并发物联网(车联网-JT808协议)平台架构方案
技术支持QQ:78772895 1.车载终端网关采用mina/netty+spring架构,独立于其他应用,主要负责维护接入终端的tcp链接.上行以及下行消息的解码.编码.流量控制,黑白名单等安全控制 ...
- mono touch登录设计
需要对MonoTouch.Dialog-1进行引用: using System; using System.Collections.Generic; using System.Linq; using ...