Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7346    Accepted Submission(s): 2539

Problem Description
Consider the following exercise, found in a generic linear algebra textbook.

Let A be an n × n matrix. Prove that the following statements are equivalent:

1. A is invertible.
2. Ax = b has exactly one solution for every n × 1 matrix b.
3. Ax = b is consistent for every n × 1 matrix b.
4. Ax = 0 has only the trivial solution x = 0.

The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d), and finally that (d) implies (a). These four implications show that the four statements are equivalent.

Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d). However, this way requires proving six implications, which is clearly a lot more work than just proving four implications!

I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this?

 
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase:

* One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤ 50000): the number of statements and the number of implications that have already been proved.
* m lines with two integers s1 and s2 (1 ≤ s1, s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved that statement s1 implies statement s2.

 
Output
Per testcase:

* One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.

 
Sample Input
2
4 0
3 2
1 2
1 3
 
Sample Output
4
2
              找出所有的强连通分量, 然后缩成一个点,然后统计缩点之后的新图的出度为0的点的个数(记为cntOut),和入度为0的点的个数(记为cntIn)

    那么要加边的条数就是max(cntOut,cntIn)

    这个为什么呢?? 因为,如果一个点的入度为0,那么说明这个点是不可达的,如果一个点的出度为0,那么说明这个点到其它点是不可达的。

    为了解决这个情况,那么只要在出度为0的点(设为u)和入度为0的点之间连一条u-->v的边,那么就解决了这种情况。

    不断的连边,只要一个点问题没解决就要连边, 所以是在两者之间取max   此段论述http://www.cnblogs.com/justPassBy/p/4678192.html转自这里

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
int head[N],bl[N],q[N],dfn[N],low[N];
int tot,scnt,cnt,l,n,m;
bool instack[N],ru[N],out[N];
struct node{
int to,next;
}e[M];
void init(){
for(int i=;i<=n;++i) {
head[i]=-;
dfn[i]=instack[i]=;
ru[i]=out[i]=;
}
l=tot=scnt=cnt=;
}
void add(int u,int v){
e[tot].to=v;
e[tot].next=head[u];
head[u]=tot++;
}
void Tajan(int u){
dfn[u]=low[u]=++cnt;
instack[u]=;
q[l++]=u;
for(int i=head[u];i+;i=e[i].next){
int v=e[i].to;
if(!dfn[v]) {
Tajan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v]&&dfn[v]<low[u])
low[u]=dfn[v];
}
if(low[u]==dfn[u]){
int t;
++scnt;
do{
t=q[--l];
instack[t]=;
bl[t]=scnt;
}while(t!=u);
}
}
int main(){
int u,v,T;
for(scanf("%d",&T);T--;){
scanf("%d%d",&n,&m);init();
for(int i=;i<=m;++i)
{
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;++i)
if(!dfn[i]) Tajan(i);
if(scnt==) {puts("");continue;}
for(int i=;i<=n;++i){
for(int j=head[i];j+;j=e[j].next){
int v=e[j].to;
if(bl[i]==bl[v]) continue;
else {
ru[bl[v]]=;
out[bl[i]]=;
}
}
}
int ans1=,ans2=;
for(int i=;i<=scnt;++i){
if(!out[i]) ++ans1;
if(!ru[i]) ++ans2;
}
printf("%d\n",max(ans1,ans2));
}
}

有向图变为强连通图 hdu2767的更多相关文章

  1. VijosP1595:学校网络(有向图变强连通图)

    描述 一些学校的校园网连接在一个计算机网络上.学校之间存在软件支援协议.每个学校都有它应支援的学校名单(学校a支援学校b,并不表示学校b一定支援学校a).当某校获得一个新软件时,无论是直接得到的还是从 ...

  2. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. 有向图的强连通图——Kosaraju

    有向图的强连通分量: 相互可达关系,每一个集合都是有向图的一个强连通分量SCC. 把一个集合看成一个点,SCC就形成了一个有向无环图——DAG;       如果DFS选择不好,从A点开始DFS,就会 ...

  4. HDU 4635 (完全图 和 有向图缩点)

    题目链接:HDU  4635 题目大意: 给你一个有向图,加有向边,使得这个图是简单有向图.问你最多加多少条有向边. 简单有向图: 1.不存在有向重边. 2.不存在图循环.(注意是不存在 “图” 循环 ...

  5. Equivalent Sets HDU - 3836 2011多校I tarjan强连通分量

    题意: 给一些集合 要求证明所有集合是相同的 证明方法是,如果$A∈B$,$B∈A$那么$A=B$成立 每一次证明可以得出一个$X∈Y$ 现在已经证明一些$A∈B$成立 求,最少再证明多少次,就可以完 ...

  6. poj 3352Road Construction(无向双连通分量的分解)

    /* 题意:给定一个连通的无向图G,至少要添加几条边,才能使其变为强连通图(指的是边强联通). 思路:利用tarjan算法找出所有的双联通分量!然后根据low[]值的不同将双联通分量 进行缩点,最后图 ...

  7. PHP算法 《图》 之 理论基础

    转载自:http://www.cnblogs.com/skywang12345/p/3691463.html Ⅰ 图的基本概念 1. 图的定义 定义:图(graph)是由一些点(vertex)和这些点 ...

  8. POJ 1236 Network Of Schools 【Targan】+【缩点】

    <题目链接> 题目大意: 有N个学校,每个学校之间单向可以发送软件,现在给你一些学校之间的收发关系.问你下面两个问题:至少要给多少个学校发送软件才能使得最终所有学校都收到软件:至少要多加多 ...

  9. 【C#数据结构系列】图

    一:图 图状结构简称图,是另一种非线性结构,它比树形结构更复杂.树形结构中的结点是一对多的关系,结点间具有明显的层次和分支关系.每一层的结点可以和下一层的多个结点相关,但只能和上一层的一个结点相关.而 ...

随机推荐

  1. 初篇:我与Linux

        据悉,红帽认证将于本年的8月份更换Rhel7为Rhel8.所以我想趁这次机会搏一搏.     我个人是初中就神仰Linux已久,只不过那个时候的我只知道Linux系统,不知道有什么区分.奈何那 ...

  2. 解决从dockerhub上下载debian:jessie失败

    解决从dockerhub上下载debian:jessie失败 笔者使用docker build 构建镜像出现下面的错误 Step 1/12 : FROM debian:jessie Get https ...

  3. 每天认识几个HTTP 响应码

    HTTP 响应状态代码指示特定 HTTP 请求是否已成功完成. 1xx信息响应 1xx 的响应结果表明接收的请求正在处理 2xx成功响应 2XX 的响应结果表明请求被正常处理了 3xx重定向 3xx ...

  4. 【Linux题目】第六关

    [定时任务规则] 1. 如果在某用户的crontab文件中有以下记录,该行中的命令多久执行一次(RHCE考试题)?( ) 30 4 * * 3 mycmd A. 每小时. B. 每周. C. 每年三月 ...

  5. 【Linux常见命令】cat命令

    cat - concatenate files and print on the standard output cat 命令用于连接文件并打印到标准输出设备上. 用法: 1. cat file 查看 ...

  6. mac OS 安装 Node.js

    打开Node.js官网https://nodejs.org/zh-cn/选择你需要的版本 下载安装 安装完成 输入命令 node -v 查看版本号 输入命令 npm -v 查看版本号

  7. 《高性能Linux服务器构建实战》——第1章轻量级HTTP服务器Nginx

    第1章 轻量级HTTP服务器Nginx本章主要介绍Nginx的配置管理和使用.作为一个轻量级的HTTP服务器,Nginx与Apache相比有以下优势:在性能上,它占用很少的系统资源,能支持更多的并发连 ...

  8. Ceph 12.2.0 实践osd 智能分组功能

    以前我们需要对ssd和hdd进行分组的时候,需要大量的修改crush map,然后绑定不同的存储池到不同的 crush 树上面,现在这个逻辑简化了很多.以上是官方宣传听起来很不错等到12.2.0稳定版 ...

  9. C#时间与时间戳格式互相转化

    C#时间格式转换为时间戳(互转) 时间戳定义为从格林威治时间 1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数. using UnityEn ...

  10. VMware15.5.0安装MacOS10.15.0系统 安装步骤(下)

    VMware15.5.0安装MacOS10.15.0系统安装步骤(下)超详细! 接上文第5条如果没看过上篇的话传送门:https://www.cnblogs.com/Top-chen/p/128024 ...