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 ≤ s1s2 ≤ 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
The 2008 ACM Northwestern European Programming Contest
 
强连通缩点,求最少添加多少条边使图强连通,设in 入度为0的数目 ou 出度, 答案便是max(in,ou) 特殊情况是 当只有一个点时为0
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack> using namespace std; const int MAX_N = ;
const int edge = 5e4 + ;
int N,M;
int low[MAX_N],pre[MAX_N],cmp[MAX_N];
int first[MAX_N],Next[edge],v[edge];
int ind[MAX_N],oud[MAX_N];
int dfs_clock,scc_cnt;
stack <int > S; void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} void dfs(int u) {
low[u] = pre[u] = ++dfs_clock;
S.push(u);
for(int e = first[u]; e != -; e = Next[e]) {
if(! pre[ v[e] ]) {
dfs(v[e]);
low[u] = min(low[u],low[ v[e] ]);
} else if(!cmp[ v[e] ]) {
low[u] = min(low[u],pre[ v[e] ]);
}
} if(low[u] == pre[u]) {
++scc_cnt;
for(;;) {
int x = S.top(); S.pop();
cmp[x] = scc_cnt;
if(x == u) break;
}
}
} void scc() {
int dfs_clock = scc_cnt = ;
memset(cmp,,sizeof(cmp));
memset(pre,,sizeof(pre)); for(int i = ; i <= N; ++i) {
if(!pre[i]) dfs(i);
} for(int i = ; i <= N; ++i) {
for(int e = first[i]; e != -; e = Next[e]) {
if(cmp[i] == cmp[ v[e] ]) continue;
ind[ cmp[ v[e] ] ]++;
oud[ cmp[i] ]++;
}
} int in = ,ou = ;
for(int i = ; i <= scc_cnt; ++i) {
in += !ind[i];
ou += !oud[i];
}
printf("%d\n",scc_cnt == ? : max(in,ou));
}
int main()
{
//freopen("sw.in","r",stdin);
int t;
scanf("%d",&t);
while(t--) {
memset(ind,,sizeof(ind));
memset(oud,,sizeof(oud)); scanf("%d%d",&N,&M);
for(int i = ; i <= N; ++i) first[i] = -;
for(int i = ; i <= M; ++i) {
int u;
scanf("%d%d",&u,&v[i]);
add_edge(i,u);
} scc(); }
return ;
}

LA 4287的更多相关文章

  1. LA 4287 等价性证明

    题目链接:http://vjudge.net/contest/141990#overview 题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题. 现在问你至少在增加多少个递推关系 ...

  2. LA 4287 等价性证明(强连通分量缩点)

    https://vjudge.net/problem/UVALive-4287 题意: 给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通. 思路:强连通分量缩点,然后统计缩点后的图的每个结 ...

  3. LA 4287 有相图的强连通分量

    大白书P322 , 一个有向图在添加至少的边使得整个图变成强连通图, 是计算整个图有a个点没有 入度, b 个点没有出度, 答案为 max(a,b) ; 至今不知所云.(求教) #include &l ...

  4. Book---强连通分量

    这几天一直在做强连通,现在总结一小下 1.定义 在一个有向图中,如果任意的两个点都是相互可达的,就说这个图是强连通的,有向图的极大强连通子图,称为强连通分量 2.求法 学的是白书上的tarjan算法 ...

  5. leggere la nostra recensione del primo e del secondo

    La terra di mezzo in trail running sembra essere distorto leggermente massima di recente, e gli aggi ...

  6. Le lié à la légèreté semblait être et donc plus simple

    Il est toutefois vraiment à partir www.runmasterfr.com/free-40-flyknit-2015-hommes-c-1_58_59.html de ...

  7. Mac Pro 使用 ll、la、l等ls的别名命令

    在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...

  8. Linux中的动态库和静态库(.a/.la/.so/.o)

    Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...

  9. HDU 4287 Intelligent IME(字典树数组版)

    Intelligent IME Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  2. Java 控制台执行带自定义包定义的类,出现“Exception in thread "main" java.lang.NoClassDefFoundError: ConnectSQLServer (wrong name: sine/ConnectSQLServer)”

    1.先说明一下代码实现:自定义package sine, 源代码保存路径为:E:\JSP\HibernateDemo\HibernateDemoProject\src\sine\ConnectSQLS ...

  3. 机器学习相关——协同过滤

    在现今的推荐技术和算法中,最被大家广泛认可和采用的就是基于协同过滤的推荐方法.本文将带你深入了解协同过滤的秘密.下面直接进入正题 1 什么是协同过滤 协同过滤是利用集体智慧的一个典型方法.要理解什么是 ...

  4. Android Cookie共享到WebView避免再次登录(保持登录状态)

    最近在做项目时用到了webview打开指定链接的网页,可已经把webview设置了cookie但始终跳转到登录页面,这明显是cookie没有设置成功导致webview没有将设置好的cookie发送出去 ...

  5. random_names随机名字生成

    // 先从txt文件中获取姓和名数组 - (void)getNames{ NSString *resourcePath1 = [[NSBundle mainBundle] pathForResourc ...

  6. Objective-C中Block语法、Block使用以及通过Block实现数组排序

    Block:语法块,本质上是匿名函数(没有名称的函数) 标准C里面没有Block,C语言的后期扩展版本,加入了匿名函数 在C++.JS.Swift等语言有类似语法,叫做闭包 Block语法和C语言里的 ...

  7. MVC 中如何将带有标签的字符串转换为HTML 标签 显示出来?

    出现问题的现象:

  8. 扒一扒编辑距离(Levenshtein Distance)算法

    最近由于工作需要,接触了编辑距离(Levenshtein Distance)算法.赶脚很有意思.最初百度了一些文章,但讲的都不是很好,读起来感觉似懂非懂.最后还是用google找到了一些资料才慢慢理解 ...

  9. SQL-LINQ-Lambda语法对照,好记性不如烂笔头

    忘记的时候就翻阅翻阅吧~~ SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Empl ...

  10. 1.总结---tr()和QTextCodec对象

    1. 关于Qt 中的tr()函数-------http://tscsh.blog.163.com/blog/static/200320103201310213312518/ 在论坛中漂,经常遇到有人遇 ...