LA 4287
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:
- A is invertible.
- Ax = b has exactly one solution for every n × 1 matrix b.
- Ax = b is consistent for every n × 1 matrix b.
- 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
#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的更多相关文章
- LA 4287 等价性证明
题目链接:http://vjudge.net/contest/141990#overview 题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题. 现在问你至少在增加多少个递推关系 ...
- LA 4287 等价性证明(强连通分量缩点)
https://vjudge.net/problem/UVALive-4287 题意: 给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通. 思路:强连通分量缩点,然后统计缩点后的图的每个结 ...
- LA 4287 有相图的强连通分量
大白书P322 , 一个有向图在添加至少的边使得整个图变成强连通图, 是计算整个图有a个点没有 入度, b 个点没有出度, 答案为 max(a,b) ; 至今不知所云.(求教) #include &l ...
- Book---强连通分量
这几天一直在做强连通,现在总结一小下 1.定义 在一个有向图中,如果任意的两个点都是相互可达的,就说这个图是强连通的,有向图的极大强连通子图,称为强连通分量 2.求法 学的是白书上的tarjan算法 ...
- 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 ...
- 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 ...
- Mac Pro 使用 ll、la、l等ls的别名命令
在 Linux 下习惯使用 ll.la.l 等ls别名的童鞋到 mac os 可就郁闷了~~ 其实只要在用户目录下建立一个脚本“.bash_profile”, vim .bash_profile 并输 ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- HDU 4287 Intelligent IME(字典树数组版)
Intelligent IME Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
随机推荐
- [css filter]filter在界面实现滤镜效果
最近逛当当,发现当当尾品会的首页推荐最底端的商品链接是灰色的图片,然后鼠标hover之后就会变成正常的彩色 肯定不是通过img来改变的,然后直接看了一下源码,其实是用的filter属性 _(:з」∠) ...
- .NET开源工作流RoadFlow-流程设计-流转条件设置(路由)
当一个步骤后面有多个步骤时,可以设置为根据设置条件系统自动判断该流向哪些步骤,也叫路由. roadflow没有单独的路由步骤来设置条件,流程条件通过双击连线弹出条件设置框来设置. 1.sql条件 即通 ...
- gravity、layout_gravity、ayout_weight 区别及用法
layout_gravity 表示组件自身在父组件中的位置. gravity 表示组件的子组件在组件中的位置. weight 意思是权重.比重,即当前控件在画布中所占的空间 ...
- R Tools for Visual Studio
https://www.visualstudio.com/en-us/features/rtvs-vs.aspx https://www.microsoft.com/en-us/cloud-platf ...
- meteor 实现 微信分享
Template.hello.events({ 'click button': function () { // increment the counter when button is clicke ...
- c++基础(三):多态
virtual:虚函数.C++通过虚函数实现多态."无论发送消息的对象属于什么类,它们均发送具有同一形式的消息,对消息的处理方式可能随接手消息的对象而变"的处理方式被称为多态性.& ...
- iOS学习之C语言函数指针
通过函数名调用函数: int max = maxValue(4, 5); printf("max = %d\n", max); 函数类型:int (int, int) 1. ...
- 3. 戏说VHDL之入门游戏一:流水灯
一. 流水灯 1.1流水灯原理 流水灯是每个学电子的入门“游戏” ,示意图如图1,其原理极其简单,但是可玩性却极强,可以就8个LED写出不同花样的程序.在1.2中我们列出两个不同思路的代码作为VH ...
- 2.Modelsim打开时出现的Error
Modelsim之error “unable to check out a viewer license necessary for use of the modelsim graph.Vsim is ...
- Python实现Apriori
Python实现Apriori 运行环境 Pyhton3 计算过程 st=>start: 开始 e=>end: 结束 op1=>operation: 读入数据 op2=>ope ...