HDU2767(Proving Equivalences)

Proving Equivalences

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:

  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 ©, that © 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 ©, and that © 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

思路

大概意思就是給定一張有向圖,問最少加入多少條邊才能變成強連通圖。Tarjan強連通算法 + 縮點
先求強連通分量,求完之後將每個強連通分量壓縮成一個點,這樣就變成一個簡單圖。最後去判斷一個簡單圖需要加入多少條邊才能變成強連通。加入的邊數是一個結論,在畫板上推了一段時間才發現。

  1. 先分別統計每個強連通分量的入度和出度
  2. 再統計出度爲0的強連通分量數目a 以及 入度爲0的強連通分量數目b。
  3. max(a,b)就是答案。
  4. 小坑點就是圖本身就是一個強連通圖那麼就輸出0;(WA幾發沒找到)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
#include <algorithm>
using namespace std;
const int maxn = 20005;
struct edge{
	int from;
	int to;
	int next;
}e[maxn*3];
stack<int>s;
int head[maxn];
int scc[maxn];
int dfn[maxn];
int low[maxn];
int in[maxn];				//入度 
int out[maxn];				//出度 
int cnt,dfs,tot;
inline void clear_set()
{
	cnt = tot = dfs = 0;
	memset(head,-1,sizeof(head));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(scc,0,sizeof(scc));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	while(!s.empty())	s.pop();
}
inline void addedge(int x,int y)
{
	e[tot].from = x;
	e[tot].to = y;
	e[tot].next = head[x];
	head[x] = tot++;
}
inline void tarjan(int x)
{
	dfn[x] = low[x] = ++cnt;
	s.push(x);
	for(int i = head[x]; ~i ;i = e[i].next){
		int y = e[i].to;
		if(!dfn[y]){
			tarjan(y);
			low[x] = min(low[x],low[y]);
		}
		else if(!scc[y]){
			low[x] = min(low[x],dfn[y]);
		}
	}
	if(low[x] == dfn[x]){
		dfs++;
		while(true){
			int p = s.top();
			s.pop();
			scc[p] = dfs;
			if(x == p)	break;
		}
	}
}
int main()
{
	int n,m,t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		clear_set();
		for(int i = 0;i < m;i++){
			int x,y;
			scanf("%d%d",&x,&y);
			addedge(x,y);
		}
		for(int i = 1;i <= n;i++){
			if(!dfn[i]){
				tarjan(i);
			}
		}
		if(dfs == 1){				//只有一個scc答案就是0
			printf("0\n");
			continue;
		}
		for(int i = 0;i < m;i++){
			int x = e[i].from;
			int y = e[i].to;
			if(scc[x] != scc[y]){
				out[scc[x]]++;	in[scc[y]]++; 
			}
		}
		int ans = 0,res = 0;
		for(int i = 1;i <= dfs;i++){
			if(!in[i])		ans++;
			if(!out[i])		res++;
		}
		printf("%d\n",max(ans,res));
	}
	return 0;
}

願你走出半生歸來仍是少年~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章