LA4287--Proving Equivalences(强连通)

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, s2n and s1s2) 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

题意:在数学里面有两种关系,一种是充分条件,即对于集合p,q,p => q,另一种是等价关系,p => q && q =>p, 这两种关系都具有传递性,p => q 可以对应到有节点p到节点q有一条边。 问:给定一些集合的充分性关系,确定出若让所有集合都等价,还需在添加最少的充分性条件。

思路:首先找出强连通分量,然后把强连通分量缩成一个点。得到一个DAG。接下来,设有a个节点的入度为0,b个节点的出度为0,则max{a,b}就是答案。注意原图已经强连通要特判一下。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define maxn 50080
#define maxm 200080
int first[maxn],S[maxn];
int vv[maxm],nxt[maxm];
int low[maxn],dfn[maxn],vis[maxn],c[maxn];
int rudu[maxn],chudu[maxn];
int e,cnt,t,p;
void addedge(int u,int v)
{
	vv[e] = v;	nxt[e] = first[u];	first[u] = e++;
	//vv[e] = u;  nxt[e] = first[v];  first[v] = e++;
}

void init()
{
	e = cnt = t = p = 0;
	memset(vis,0,sizeof(vis));
	memset(first,-1,sizeof(first));
	memset(dfn,0,sizeof(dfn));
	memset(rudu,0,sizeof(rudu));
	memset(chudu,0,sizeof(chudu));
}

void Tarjan(int u)
{
	vis[u] = 1;S[++p] = u;
	dfn[u] = low[u] = ++cnt;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(!dfn[v])
		{
			Tarjan(v);
			low[u] = min(low[u],low[v]);
		}
		else if(vis[v])	low[u] = min(low[u],dfn[v]);
	}
	if(low[u] == dfn[u])
	{
		++t;
		while(S[p] != u)
		{
			vis[S[p]] = 0;
			c[S[p]] = t;
			p--;
		}
		c[S[p]] = t;	p--;    vis[u] = 0;
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int T;	scanf("%d",&T);
	while(T--)
	{
		int n,m;
		scanf("%d%d",&n,&m);
		init();
		for(int i = 1;i <= m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			addedge(u,v);
		}
		for(int i = 1;i <= n;i++)
			if(!dfn[i])
			{
				Tarjan(i);
			}
		if(t == 1)	puts("0");
		else
		{
			for(int i = 1;i <= n;i++)
			{
				for(int j = first[i];j != -1;j = nxt[j])
				{
					int v = vv[j];
					if(c[i] == c[v])	continue;///同一个强连通分量
					else
					{
						chudu[c[i]]++;
						rudu[c[v]]++;
					}
				}
			}
			int ans0 = 0,ans1 = 0;
			for(int i = 1;i <= t;i++)
			{
				if(chudu[i] == 0)	ans0++;
				if(rudu[i] == 0)	ans1++;
			}
			printf("%d\n",max(ans0,ans1));
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值