hdu 2767 Proving Equivalences【scc缩点+搭桥】
来源:互联网 发布:maven 打包java环境 编辑:程序博客网 时间:2024/05/22 01:29
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:
- 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
题意:
给出m条有向边,求至少搭多少桥使其成为强连通分量。
思路:
肯定要Tarjan缩点,缩点之后搭桥,把入度为0和出度为0的分别统计,取最大值。这样就可已使所有点有进有出,做后构成一个强连通图。以后遇到这种题,先进行缩点,方便考虑。
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>const int maxn = 100010;using namespace std;int head[maxn], in[maxn], out[maxn]; int stack[maxn]; bool instack[maxn];int belong[maxn]; int dfn[maxn];int low[maxn];int index;int top; int scc;int tot;struct Edge { int to; int next;}edge[maxn << 3];void add_edge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}void tarjan(int u) { int v; low[u] = dfn[u] = ++index; stack[top++] = u; instack[u] = true; for(int i = head[u]; i != -1; i = edge[i].next) { v = edge[i].to; if(!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if(instack[v] && low[u] > dfn[v]) { low[u] = dfn[v]; } } if(low[u] == dfn[u]) { scc++; do { v = stack[--top]; instack[v] = false; belong[v] = scc; } while(v != u); }}void solve(int n) { memset(dfn, 0, sizeof(dfn)); memset(instack, false, sizeof(instack)); index = scc = top = 0; for(int i = 1; i <= n; i++) { if(!dfn[i]) tarjan(i); } for(int i = 1; i <= n; i++) { //scc缩点 for(int j = head[i]; j != -1; j = edge[j].next) { int v = edge[j].to; if(belong[i] != belong[v]) { in[belong[v]] = 1; out[belong[i]] = 1; } } } int ans2 = 0, ans1 = 0; if(scc == 1) { printf("0\n"); return; } for(int i = 1; i <= scc; i++) { if(!in[i]) ans1++; if(!out[i]) ans2++; } printf("%d\n", max(ans1, ans2)); }int main() { int n, m, A, B, t; scanf("%d", &t); while(t--) { memset(head, -1, sizeof(head)); memset(in, 0, sizeof(in)); memset(out, 0, sizeof(out)); tot = 0; scanf("%d %d", &n, &m); while(m--) { scanf("%d %d", &A, &B); add_edge(A, B); } solve(n); } return 0;}
- hdu 2767 Proving Equivalences【scc缩点+搭桥】
- hdoj--2767--Proving Equivalences (scc+缩点)
- HDU 2767 Proving Equivalences (tarjan scc)
- hdu 2767 Proving Equivalences (tarjan + 缩点)
- hdu 2767 Proving Equivalences (Kosaraju+缩点)
- hdu 2767 Proving Equivalences (Kosaraju+缩点)
- 【缩点】HDU 2767 Proving Equivalences
- hdu 2767 Proving Equivalences //tarjan+缩点
- HDU 2767--Proving Equivalences【scc缩点构图 && 求向图中最少增加多少条边才可以使新图强连通】
- hdu 2767 Proving Equivalences 强连通 缩点 求度
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
- HDU 2767-Proving Equivalences(强联通+缩点)
- Proving Equivalences (hdu 2767 强联通缩点)
- hdu 2767 Proving Equivalences【强连通Kosaraju+缩点染色】
- 【HDU 2767】Proving Equivalences (Tarjan 缩点)
- hdu 2767 Proving Equivalences(强连通分量+缩点)
- HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)
- hdu 2767 Proving Equivalences(强连通分量+缩点)
- hdu 6216 A Cubic number and A Cubic Number
- 算法这一站是新的起点
- 8.数据的可视化-绘制简单的线与点
- AS新建项目卡在Building这里怎么办?
- laydate时间框架的运用
- hdu 2767 Proving Equivalences【scc缩点+搭桥】
- jvm性能相关(jvisualvm远程连接配置)
- 利用BeanMap进行对象与Map的相互转换 (在hibernate中 map集合转对象 用得到<重要>)
- 条款05:了解C++默认编写调用哪些函数
- C语言之 snprintf()函数 用法
- UVA 12166 Equilibrium Mobile (二叉树遍历+贪心)
- 13:图像模糊处理(1.8编程基础之多维数组)
- LTE-TDD随机接入过程(1)-目的和分类
- RUP、xp、敏捷过程的含义