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?

InputOn 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.OutputPer 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 题意: 
  给定一张有向图,问最少添加几条边使得有向图成为一个强连通图。 题解:
  缩完点的图是一个DAG,变成强联通就是,一个点至少一个出度一个入度
  所以只需要输出缩完点后的图入度和出度最大值既可。
   这个真的很好想,自己瞎比比搞了半天,浪费了许多时间。
  真的菜。
  

  想到后怒删代码,修改就过了。

 #include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define N 20007
#define M 50007
using namespace std; int n,m,tim,sc,totalin,totalout;
int top,dfn[N],low[N],stack[N],ins[N],bel[N],chu[N],ru[N],boo[N];
int cnt,head[N],Next[M],rea[M];
struct Node
{
int ru,chu;
void init()
{
ru=chu=;
}
}zhi[N]; void add(int u,int v)
{
Next[++cnt]=head[u];
head[u]=cnt;
rea[cnt]=v;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++tim;
stack[++top]=u,ins[u]=true;
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];
if (!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (ins[v]) low[u]=min(low[u],dfn[v]);
}
if (low[u]==dfn[u])
{
sc++;int x=-;
while(x!=u)
{
x=stack[top--];
ins[x]=;
bel[x]=sc;
}
}
}
void rebuild()
{
for (int u=;u<=n;u++)
{
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];
if (bel[v]!=bel[u])
{
chu[bel[u]]++;
ru[bel[v]]++;
}
}
}
for (int i=;i<=sc;i++)
{
if (!chu[i]) totalout++;
if (!ru[i]) totalin++;
}
}
int main()
{
int T;scanf("%d",&T);
while (T--)
{
cnt=sc=,top=,totalin=totalout=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(boo,,sizeof(boo));
memset(chu,,sizeof(chu));
memset(ru,,sizeof(ru));
scanf("%d%d",&n,&m);
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
}
for (int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
rebuild();
int ans=max(totalout,totalin);
if (ans==) ans=;
printf("%d\n",ans);
}
}

HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)的更多相关文章

  1. hdu 2767 Proving Equivalences(tarjan缩点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...

  2. HDU 2767 Proving Equivalences (Tarjan)

    Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  3. HDU 2767:Proving Equivalences(强连通)

    题意: 一个有向图,问最少加几条边,能让它强连通 方法: 1:tarjan 缩点 2:采用如下构造法: 缩点后的图找到所有头结点和尾结点,那么,可以这么构造:把所有的尾结点连一条边到头结点,就必然可以 ...

  4. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  5. HDU 2767 Proving Equivalences (强联通)

    pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...

  6. hdu 2767 Proving Equivalences

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

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

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

  8. HDU 2767.Proving Equivalences-强连通图(有向图)+缩点

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

  9. hdu - 2667 Proving Equivalences(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=2767 求至少添加多少条边才能变成强连通分量.统计入度为0的点和出度为0的点,取最大值即可. #include & ...

随机推荐

  1. 03.Java多线程并发库API使用2

    1.多个线程之间共享数据的方式探讨 1.如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 2.如果每个线程执行的代 ...

  2. sass 常用用法笔记

    最近公司开发的h5项目,需要用到sass,所以领导推荐让我去阮一峰大神的SASS用法指南博客学习,为方便以后自己使用,所以在此记录. 一.代码的重用 1.继承:SASS允许一个选择器,继承另一个选择器 ...

  3. RecyclerView 缓存机制学习笔记1

    盗用别人图片 获取VIew的方法的流程 最先调用 其次调用 这个方法调用会先去缓存 这个是是否有动画,有动画就去里面取. 如果取不到就接着调用 如果在没有继续调用 都取不到就去实例化 调用的次数取决于 ...

  4. SQL Server2012 T-SQL对分页的增强尝试

    简介 SQL Server 2012中在Order By子句之后新增了OFFSET和FETCH子句来限制输出的行数从而达到了分页效果.相比较SQL Server 2005/2008的ROW_Numbe ...

  5. Monkeyrunner介绍

    Monkeyrunner概述 Monkeyrunner是由Google开发.用于android系统的自动化测试工具,由android系统自带,存在于android sdk中(sdk:software ...

  6. axios的简单封装及在组件内使用

    /**第一步 * 配置编译环境和线上环境之间的切换 * baseUrl: 域名地址 * routerMode: 路由模式 * imgBaseUrl: 图片所在域名地址 * */ let Host = ...

  7. swift版本拼图游戏项目源码

    现学现做的第一个swift版本拼图游戏demo 常规模式,对换模式任你选择, 用到了花瓣的API,各种萌妹子~

  8. 50个Bootstrap扩展插件

    Bootstap这个框架本身已经包含了开发网页的众多要素,包括了常用的工具以及扩展组件,如果你在开发页面时觉得在某些方面还不够的话,不妨看看最新收集的50个Bootstrap扩展插件,这些插件在我们平 ...

  9. autoHeight # 动态高度添加 用 window.addEventListener('resize', function () {

    动态高度添加 用 window.addEventListener('resize', function () { mounted () { this.init() window.addEventLis ...

  10. python爬虫---实现项目(二) 分析Ajax请求抓取数据

    这次我们来继续深入爬虫数据,有些网页通过请求的html代码不能直接拿到数据,我们所需的数据是通过ajax渲染到页面上去的,这次我们来看看如何分析ajax 我们这次所使用的网络库还是上一节的Reques ...