关闭

HDOJ--2767--Proving Equivalences

186人阅读 评论(0) 收藏 举报
分类:


Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3897    Accepted Submission(s): 1409


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 (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
 
等价命题:a <=> b  即 a可以推出b 且 b可以推出a。

题意:有n个命题,现已给出m次推导即由a可以推出b(根据该条件b不能推出a),问最少还需要多少次推导才可以证明n个命题之间是等价的。

分析   --   把每个命题虚拟成一个节点,a推导b当作一条a到b的有向边,问题就变成:在有向图中最少增加多少条边才可以使新图强连通。
AC代码:
//容易忘记的问题就是对各种值进行初始化。
//边数比较多,不能用邻接表,可以用容器来做。 
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f
#define MAXN 20000+50
#define MAXM 50000+50
using namespace std;
vector<int>mp[MAXN];
vector<int>NEW[MAXN];
vector<int>scc[MAXN];
stack<int>q;
bool Instack[MAXN];
int dfn[MAXN],low[MAXN],sccno[MAXN];
int in[MAXN],out[MAXN]; 
int dfs_clock,scc_cnt,n,m;
void get_map(){
	for(int i=1;i<=n;i++)
		mp[i].clear();
	for(int i=0;i<m;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		mp[u].push_back(v);
	}	
}
void tarjan(int u){//这个算法有错误。 
	int v;
	low[u]=dfn[u]=++dfs_clock;
	q.push(u);
	Instack[u]=true;
	for(int i=0;i<mp[u].size();i++){
		 v=mp[u][i];
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(Instack[v])
			low[u]=min(low[u],dfn[v]);//一定要注意细节问题。 
	}
		if(low[u]==dfn[u]){
			scc_cnt++;
			scc[scc_cnt].clear(); 
			while(1){
				v=q.top();
				q.pop();
				sccno[v]=scc_cnt;//这个点属于哪一个强连通 
				scc[scc_cnt].push_back(v);//这个SCC里有哪些点。 
				Instack[v]=false;
				if(u==v)
					break;	
			}
		
		} 
}
void find_cut(){
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(Instack,false,sizeof(Instack));
	dfs_clock=scc_cnt=0;
	for(int i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i);
}
void SCC(){
	for(int i=1;i<=scc_cnt;i++){
		NEW[i].clear();
		in[i]=0;
		out[i]=0;
}		
	for(int i=1;i<=n;i++){//用容器如何实现邻接表的遍历。 
		for(int j=0;j<mp[i].size();j++){
			int u=sccno[i];
			int v=sccno[mp[i][j]];
			if(u!=v){
				in[v]++;
				out[u]++;
			} 
		}
		
	}
} 
void solve(){
	if(scc_cnt==1){//注意特殊情况的考虑,如果本身就是一个强连通图的话,就不会构造出新图了。 
		printf("0\n");
			return ;
	} 
	int sumin=0,sumout=0;
		for(int i=1;i<=scc_cnt;i++){
			if(in[i]==0)
				sumin++;
			if(out[i]==0)
				sumout++;	
		}
		int ans=max(sumin,sumout);
		printf("%d\n",ans);
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&m);
		get_map();
		find_cut();
		SCC();
		solve();
	}
	return 0;
} 


Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3897    Accepted Submission(s): 1409


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 (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
 
等价命题:a <=> b  即 a可以推出b 且 b可以推出a。

题意:有n个命题,现已给出m次推导即由a可以推出b(根据该条件b不能推出a),问最少还需要多少次推导才可以证明n个命题之间是等价的。

分析   --   把每个命题虚拟成一个节点,a推导b当作一条a到b的有向边,问题就变成:在有向图中最少增加多少条边才可以使新图强连通。
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    座右铭
    “要坚持梦想,要坚持梦想,要坚持梦想”重要的事情说三遍。没有谁生来就是神牛,而千里之行,始于足下!
    个人资料
    • 访问:67842次
    • 积分:2436
    • 等级:
    • 排名:第15242名
    • 原创:178篇
    • 转载:2篇
    • 译文:0篇
    • 评论:18条
    最新评论