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

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
 
Source
 
Recommend
lcy
 
理解一下题意,经过奇奇怪怪的转化以后,得出核心题意:给一个有向图,问最少加几条边可使其成为强连通图。
tarjan缩点以后,统计入度为0和出度为0的点个数,取最大值就是答案。
 
这题总感觉以前做过好多次?
 
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int mxn=;
int top,stack[mxn];
bool inst[mxn];
int cnt,dnow;
int dfn[mxn],low[mxn];
int belone[mxn],in[mxn],out[mxn];
vector<int> e[mxn];
void clear(){
cnt=;dnow=;top=;
memset(dfn,-,sizeof(dfn));
memset(inst,false,sizeof(inst));
memset(in,,sizeof in);
memset(out,,sizeof out);
for(int i=;i<mxn;i++) e[i].clear();
}
int n,m;
void tarjan(int s){
int v=,i;
dfn[s]=++dnow;
low[s]=dfn[s];
inst[s]=true;
stack[++top]=s;
int si=e[s].size();
for(i=;i<si;i++){
v=e[s][i];
if(dfn[v]==-){
tarjan(v);
low[s]=min(low[v],low[s]);
}
else if(inst[v]){
low[s]=min(dfn[v],low[s]);
}
}
if(dfn[s]==low[s]){
cnt++;
do{
v=stack[top--];
belone[v]=cnt;
inst[v]=false;
}while(s!=v);
}
return;
}
void calc(){
if(cnt==){
printf("0\n");return;
}
int i,j;
for(i=;i<=n;i++){
for(j=;j<e[i].size();j++){
int v=e[i][j];
if(belone[i]!=belone[v]){
in[belone[v]]++;
out[belone[i]]++;
}
}
}
int idg=,odg=;
for(i=;i<=cnt;i++){
if(!in[i])idg++;
if(!out[i])odg++;
}
printf("%d\n",max(idg,odg));
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
if(!m){
if(n==)printf("0\n");
else printf("%d\n",n);
continue;
}
clear();
int i,j;
int u,v;
for(i=;i<=m;i++){
scanf("%d%d",&u,&v);
e[u].push_back(v);
}
for(i=;i<=n;i++){
if(dfn[i]==-)tarjan(i);
}
calc();
}
return ;
}

HDU2767 Proving Equivalences的更多相关文章

  1. HDU2767 Proving Equivalences(加边变为强联通图)

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

  2. hdu2767 Proving Equivalences Tarjan缩点

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  3. hdu2767 Proving Equivalences --- 强连通

    给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...

  4. hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法

    点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<c ...

  5. hdu 2767 Proving Equivalences

    Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...

  6. hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】

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

  7. Proving Equivalences(加多少边使其强联通)

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

  8. UVALive - 4287 - Proving Equivalences(强连通分量)

    Problem   UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...

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

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

随机推荐

  1. JAVA JDBC 连接 Oracle

    使用 Junit 测试类编写 public class JdbcTest { private Connection con = null;// 创建一个数据库连接 private PreparedSt ...

  2. mod_deflate模块

    mod_deflate模块 压缩模块,使用mod_deflate模块压缩页面优化传输速度 主要是需要设置 1.针对的内容 2.压缩比是多少 可以忽略排除特定旧版本的浏览器的设置.因为那些都太老了,现在 ...

  3. GNU汇编 程序状态字访问指令

    .text .global  _start _start: mrs r0,cpsr orr r0,#0b100 msr cpsr,r0

  4. DevOps - 监控告警 - Zabbix

    官网3.4版本中文文档 Zabbix documentation in Chinese [Zabbix Documentation 3.4] https://www.zabbix.com/docume ...

  5. tcl之string操作-match/map/大小写转换

  6. PHP array_multisort()函数超详细理解

    项目中用到这个函数了 ,起初对这个函数一直是懵逼状态,文档都看的朦朦胧胧的 网上无意间看到这篇文章 ,写的超级详细,收藏了 . 当然要先放原地址:https://www.cnblogs.com/WuN ...

  7. awk速查手册

    简介awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进 ...

  8. CMDB(资产管理系统) day1

    运维自动化最重要的就是标准化一切 自动化运维则支持以下功能: 1.OS的选择统一化,同一个项目使用同样的OS系统部署其所需要的各类软件.2.软件安装标准化,例如JAVA虚拟机,php,nginx,my ...

  9. Scrapy-redis分布式爬虫爬取豆瓣电影详情页

    平时爬虫一般都使用Scrapy框架,通常都是在一台机器上跑,爬取速度也不能达到预期效果,数据量小,而且很容易就会被封禁IP或者账号,这时候可以使用代理IP或者登录方式爬,然而代理IP很多时候都很鸡肋, ...

  10. Nosql和RDBMS的比较及解释

    概述 传统的关系型数据库以及数据仓库在面对大数据的处理时显得越来越力不从心.因为关系数据库管理系统 (RDBMS)的设计从未考虑过能够处理日益增长且格式多变的数据,以及访问数据并进行分析的用户需求呈爆 ...