有向图变为强连通图 hdu2767
Proving Equivalences
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7346 Accepted Submission(s): 2539
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?
* 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.
* One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.
那么要加边的条数就是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的更多相关文章
- VijosP1595:学校网络(有向图变强连通图)
描述 一些学校的校园网连接在一个计算机网络上.学校之间存在软件支援协议.每个学校都有它应支援的学校名单(学校a支援学校b,并不表示学校b一定支援学校a).当某校获得一个新软件时,无论是直接得到的还是从 ...
- HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 有向图的强连通图——Kosaraju
有向图的强连通分量: 相互可达关系,每一个集合都是有向图的一个强连通分量SCC. 把一个集合看成一个点,SCC就形成了一个有向无环图——DAG; 如果DFS选择不好,从A点开始DFS,就会 ...
- HDU 4635 (完全图 和 有向图缩点)
题目链接:HDU 4635 题目大意: 给你一个有向图,加有向边,使得这个图是简单有向图.问你最多加多少条有向边. 简单有向图: 1.不存在有向重边. 2.不存在图循环.(注意是不存在 “图” 循环 ...
- Equivalent Sets HDU - 3836 2011多校I tarjan强连通分量
题意: 给一些集合 要求证明所有集合是相同的 证明方法是,如果$A∈B$,$B∈A$那么$A=B$成立 每一次证明可以得出一个$X∈Y$ 现在已经证明一些$A∈B$成立 求,最少再证明多少次,就可以完 ...
- poj 3352Road Construction(无向双连通分量的分解)
/* 题意:给定一个连通的无向图G,至少要添加几条边,才能使其变为强连通图(指的是边强联通). 思路:利用tarjan算法找出所有的双联通分量!然后根据low[]值的不同将双联通分量 进行缩点,最后图 ...
- PHP算法 《图》 之 理论基础
转载自:http://www.cnblogs.com/skywang12345/p/3691463.html Ⅰ 图的基本概念 1. 图的定义 定义:图(graph)是由一些点(vertex)和这些点 ...
- POJ 1236 Network Of Schools 【Targan】+【缩点】
<题目链接> 题目大意: 有N个学校,每个学校之间单向可以发送软件,现在给你一些学校之间的收发关系.问你下面两个问题:至少要给多少个学校发送软件才能使得最终所有学校都收到软件:至少要多加多 ...
- 【C#数据结构系列】图
一:图 图状结构简称图,是另一种非线性结构,它比树形结构更复杂.树形结构中的结点是一对多的关系,结点间具有明显的层次和分支关系.每一层的结点可以和下一层的多个结点相关,但只能和上一层的一个结点相关.而 ...
随机推荐
- js 函数的防抖(debounce)与节流(throttle) 带 插件完整解析版 [helpers.js]
前言: 本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽. 函数防抖与节流是做什么的?下面进行通俗的讲解. 本文借鉴:h ...
- QML-AES加解密小工具
Intro 为了解码网课视频做的小工具,QML初学者可以参考一下. 项目地址 Todo 在插入新条目时,ListView不会自动根据section进行重排,因此出现同一个文件夹重复多次的现象.目测强行 ...
- c语言-----劫持系统03
1. 回顾 在前2节我们已经实现了劫持原理.函数指针等一些概念,下面进行系统劫持 2. 工具 vs2017 Detours 3. windows如何创建一个进程? (1)创建进程函数 CreatePr ...
- 1309:【例1.6】回文数(Noip1999)
传送门:http://ybt.ssoier.cn:8088/problem_show.php?pid=1309 [题目描述] 若一个数(首位不为零)从左向右读与从右向左读都是一样,我们就将其称之为回文 ...
- RxJava--Buffer,GroupBy 对比
Buffer 设定收集n个元素为一组,以下方代码为例,三个为一组,则当组满三个元素时,返回一次List数据 没组满三个元素时,如果调用onComplete,直接发送剩余元素,没调用onComplete ...
- Java_Web--JDBC 增加记录操作模板
如果不能成功链接数据库,我的博客JAVA中有详细的介绍,可以看一下 import java.sql.Connection; import java.sql.DriverManager; import ...
- java基于socket的网络通信,实现一个服务端多个客户端的群聊,传输文件功能,界面使用Swing
最近在复习java的io流及网络编程.但复习写那些样板程序总是乏味的.便准备写个项目来巩固.想来想去还是聊天项目比较好玩.如果日后完成的比较好自己也可以用(哈哈哈).并且自己后面也要继续巩固java多 ...
- 编写简单的内核模块及内核源码下载,内核模块Makefile编写
CentOS的内核源码默认是没有下载的,需要自己下载,首先安装linux的时候就应该知道linux的版本,我装的是Centos7的 下面查一下内核的版本,使用下面的命令 [scut_lcw@local ...
- 阿里云服务器连接AWS-S3
1.找到一个路径下载 aws-cli (使用离线包安装) wget -P /usr/local/software https://s3.amazonaws.com/aws-cli/awscli-bu ...
- 使用Redis构建电商网站
涉及到的key: 1. login,hash结构,存储用户token与用户ID之间的映射. 2. recent_tokens,存储最近登陆用户token,zset结构 member: token,sc ...