Proving Equivalences

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

NWERC 2008
 
题目大意:
a 证明 b,且,b 证明 a,说明a和b相等
a证明b,b证明c,可以得出 a 证明 c。 
求最少再证明几次才能使得这些题都能互相证明
题解:
用Tarjan缩点,然后答案就是 max(入度=0,出度=0)。
#include<bits/stdc++.h>
using namespace std;
int n,T,m,index,team_num;
int low[],dfn[],team[],in[],out[];
bool instack[];
vector<int> mp[];
stack<int> S;
void Tarjan ( int u )
{
dfn[u]=low[u]=++index;
S.push(u);
instack[u]=;
for ( int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if (!dfn[v])
{
Tarjan (v) ;
low[u]=min(low[u],low[v]);
}
else if (instack[v]) low[u]=min(low[u],dfn[v]);//是否在栈中
}
if (dfn[u]==low[u]) //构成强连通分量
{
team_num++; //组数
while () //同一组标号
{
int v=S.top(); S.pop();
instack[v]=;
team[v]=team_num;
if (v==u) break;
}
}
} void dfs()
{
memset(team,,sizeof(team));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(instack,,sizeof(instack));
team_num=;
index=;
for(int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) mp[i].clear();
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
mp[x].push_back(y);
}
dfs(); //缩点
/*for(int i=1;i<=n;i++)
printf("%d:%d\n",i,team[i]);*/ for(int i=;i<=team_num;i++) in[i]=out[i]=;
for(int i=;i<=n;i++)
for(int j=;j<mp[i].size();j++)
{
if (team[i]!=team[mp[i][j]])
{
out[ team[i] ]++;
in[ team[mp[i][j]] ]++;
}
}
int innum=,outnum=;
for(int i=;i<=team_num;i++)
{
if (!in[i]) innum++;
if (!out[i]) outnum++;
}
if (team_num==) printf("0\n");
else printf("%d\n",max(innum,outnum));
}
return ;
}

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(至少增加多少条边使得有向图变成强连通图)

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

  3. HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

  4. HDU 2767 Proving Equivalences (强联通)

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

  5. hdu 2767 Proving Equivalences

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

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给出n个点m条边,问在m条边的基础上,最小再添加多少条边可以让图变成强连通.思路:强连通分量缩点后找 ...

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

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

  8. hdu 4635 Strongly connected (tarjan)

    题意:给一个n个顶点m条弧的简单有向图(无环无重边),求最多能够加入多少条弧使得加入后的有向图仍为简单有向图且不是一个强连通图.假设给的简单有向图本来就是强连通图,那么输出-1. 分析: 1.用tar ...

  9. hdu 2767 Proving Equivalences 强连通缩点

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

随机推荐

  1. C#对两种类型动态库的使用

    一.托管:如果一个动态库本身是基于.NET的,那么可以直接在工程引用里右键添加引用,如微软的COM技术[因为你依托的是微软的框架,所以需要regsvr32注册] 二.非托管:如果不是基于.NEt的,那 ...

  2. BIOS、MBR、UEFI和GPT关系

    很多用户在新买电脑,或是给已有电脑重装系统时都出现过怎么都无法引导U盘安装的情况.究其原因,还是没能搞清楚BIOS.MBR.UEFI和GPT的复杂关系.所以,今天小编就和大家分享一下它们之间的爱恨情仇 ...

  3. NOI 16 买房子

    买房子(NOI 16) 总时间限制: 1000ms 内存限制: 65536kB 描述 某程序员开始工作,年薪N万,他希望在中关村公馆买一套60平米的房子,现在价格是200万,假设房子价格以每年百分之K ...

  4. JDK tools之jps和jstack诊断Java程序

    大部分Java开发者可能知道有这么个工具,但是没怎么用过,每次还得百度一下.我也是之一 -_-!!. 每次遇到

  5. MongoVUE的Collections数据不显示的解决方法

    问题描述: 使用 mongoDB数据库, 数据添加成功了,使用命令行能查询出来,但在MongoVUE 中数据却不显示  (我使用的是 mongoDB 3.4 的版本) 原因:引擎问题,只要降到2.X版 ...

  6. A start job is running for Raise network interface(5min 13s )问题解决方法

    命令:sudo vim /etc/systemd/system/network-online.target.wants/networking.service将里面的TimeoutStartSec=5m ...

  7. 【转】C/C++ 函数指针与类函数指针

    转自:http://blog.csdn.net/iamshaofa/article/details/17614615 C函数指针 int numAdd(int a, int b) { return a ...

  8. DDD领域模型和充血对象

    DDD领域模型 官方说法 领域驱动设计,它是对面向对象的的分析和设计(OOAD,Object Orient Analysis Design)的一个补充,对技术框架进行了分层规划,同时对每个类进行了策略 ...

  9. Redis<一> 数据结构:String

    ). set key value : 将字符串值 value 关联到 key .如果 key 已经持有其他值, SET 就覆写旧值,无视类型. ). get key : 返回 key 所关联的字符串值 ...

  10. HDU 6114 Chess

    Chess 思路:求C(n,m),除法取余用乘法逆元算. 代码: #include<bits/stdc++.h> using namespace std; #define ll long ...