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

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?

InputOn 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.OutputPer 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 题意: 
  给定一张有向图,问最少添加几条边使得有向图成为一个强连通图。 题解:
  缩完点的图是一个DAG,变成强联通就是,一个点至少一个出度一个入度
  所以只需要输出缩完点后的图入度和出度最大值既可。
   这个真的很好想,自己瞎比比搞了半天,浪费了许多时间。
  真的菜。
  HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

  想到后怒删代码,修改就过了。

 #include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#define N 20007
#define M 50007
using namespace std; int n,m,tim,sc,totalin,totalout;
int top,dfn[N],low[N],stack[N],ins[N],bel[N],chu[N],ru[N],boo[N];
int cnt,head[N],Next[M],rea[M];
struct Node
{
int ru,chu;
void init()
{
ru=chu=;
}
}zhi[N]; void add(int u,int v)
{
Next[++cnt]=head[u];
head[u]=cnt;
rea[cnt]=v;
}
void Tarjan(int u)
{
dfn[u]=low[u]=++tim;
stack[++top]=u,ins[u]=true;
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];
if (!dfn[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if (ins[v]) low[u]=min(low[u],dfn[v]);
}
if (low[u]==dfn[u])
{
sc++;int x=-;
while(x!=u)
{
x=stack[top--];
ins[x]=;
bel[x]=sc;
}
}
}
void rebuild()
{
for (int u=;u<=n;u++)
{
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i];
if (bel[v]!=bel[u])
{
chu[bel[u]]++;
ru[bel[v]]++;
}
}
}
for (int i=;i<=sc;i++)
{
if (!chu[i]) totalout++;
if (!ru[i]) totalin++;
}
}
int main()
{
int T;scanf("%d",&T);
while (T--)
{
cnt=sc=,top=,totalin=totalout=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(boo,,sizeof(boo));
memset(chu,,sizeof(chu));
memset(ru,,sizeof(ru));
scanf("%d%d",&n,&m);
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
}
for (int i=;i<=n;i++)
if (!dfn[i]) Tarjan(i);
rebuild();
int ans=max(totalout,totalin);
if (ans==) ans=;
printf("%d\n",ans);
}
}
上一篇:LeetCode题解: LRU Cache 缓存设计


下一篇:jquery实现ajax提交表单数据或json数据