传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2767

Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)

Memory Limit: 32768/32768 K (Java/Others)

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


解题心得:

  1. 题目说了一大堆废话,其实就是给你一个有向图,问你最少还需要添加多少条有向边可以将整个图变成强联通图。
  2. 其实想想就知道,可以先使用tarjan缩点,缩点之后会形成一个新的图,然后看图中出度为0和入度为0的点,因为这些点必然需要添一条边到图中,所以直接去取出度为0点的数目和入读为0的点的数目的最大值。为啥是最大值?很简单啊,将一条边添在出度为0的点和入度为0的点之间不就解决了两个点了吗,但是最后肯定要添加数目多的度为0的点啊。
  3. 注意一个坑点,那就如果可以直接缩为一个点那是不用添边的。

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<stack>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 2e4+100;
vector <int> ve[maxn],maps[maxn],shrink[maxn];
bool vis[maxn];
int tot,num,indu[maxn],outdu[maxn],n,m,dfn[maxn],low[maxn],pre[maxn];
stack <int> st; void init()//初始化很重要
{
while(!st.empty())
st.pop();
tot = num = 0;
memset(outdu,0,sizeof(outdu));
memset(indu,0,sizeof(indu));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
memset(pre,0,sizeof(pre));
for(int i=0;i<maxn;i++)
{
ve[i].clear();
shrink[i].clear();
maps[i].clear();
}
for(int i=0;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
ve[a].push_back(b);
}
} void tarjan(int x)
{
dfn[x] = low[x] = ++tot;
vis[x] = true;
st.push(x);
for(int i=0;i<ve[x].size();i++)
{
int v = ve[x][i];
if(!dfn[v])
{
tarjan(v);
low[x] = min(low[x],low[v]);
}
else if(vis[v])
low[x] = min(low[x],dfn[v]);
}
if(low[x] == dfn[x])
{
while(1)
{
int now = st.top();
st.pop();
vis[now] = false;
shrink[num].push_back(now);
pre[now] = num;
if(now == x)
break;
}
num++;
}
} void get_new_maps()
{
if(num == 1)
{
printf("0\n");
return;
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<ve[i].size();j++)
{
int a = i;
int b = ve[i][j];
if(pre[a] != pre[b])
{
outdu[pre[a]]++;
indu[pre[b]]++;
}
}
}
int sum_indu,sum_outdu;
sum_indu = sum_outdu = 0;
for(int i=0;i<num;i++)
{
if(!indu[i])
sum_indu++;
if(!outdu[i])
sum_outdu++;
}
printf("%d\n",max(sum_indu,sum_outdu));
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
get_new_maps();
}
return 0;
}

最新文章

  1. jacascript中的原型链以原型
  2. 运用ASP.NET实现
  3. Python之路----------生成器
  4. 关于seajs模块化的搭建
  5. HDU 1403-Longest Common Substring (后缀数组)
  6. Oracle索引失效问题:WHERE C1=&#39;&#39; OR C2 IN(SubQuery),并发请求时出现大量latch: cache buffers chains等待
  7. unity3d基础01
  8. The init method
  9. Ubuntu 16.04 - 64bit 解压 rar 报错 Parsing Filters not supported
  10. S3C2410 ADS实验手册
  11. 首页的sitecontent地址
  12. 在linux下文件转码
  13. 201521123065 《Java程序设计》第3周学习总结
  14. SpringBoot + Spring Security 学习笔记(一)自定义基本使用及个性化登录配置
  15. LeetCode one Two Sum
  16. NAND Flash底层原理,SLC MLC TLC比较【转】
  17. 在IOS应用中打开另外一个应用的解决方案
  18. mysql utf8mb4
  19. Linux驱动:LCD驱动框架分析
  20. jquery之Ajax(一)

热门文章

  1. qt QMessageBox 中文乱码的问题
  2. SSRS-lookupSet-DataSet-分组查询
  3. xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;报错
  4. C#学习笔记:foreach原理
  5. EF批量插入数据耗时对比
  6. filter和map的使 使得数组对象变数组
  7. centos7使用yum安装不了ffmpeg
  8. C# XML序列化/反序列化类XmlSerializer使用示例
  9. ABC3D创客项目:风力小车
  10. What is a meta-class in Objective-C?