HDOJ--2767--Proving Equivalences

原创 2015年11月19日 21:00:22


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的有向边,问题就变成:在有向图中最少增加多少条边才可以使新图强连通。
版权声明:本文为小小呆原创文章,欲转载,请在后台勾搭本呆。

hdoj2767Proving Equivalences【scc+缩点】

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...

HDOJ 2767 Proving Equivalences

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给你一个些点的关系,然后问你还需要加多少条边可以使这个图变成强连通,也就是每个点都可以通...

LA-4287 & HDOJ-2767 Proving Equivalences 解题报告

强连通分量题。
  • JZQT_T
  • JZQT_T
  • 2014年11月24日 20:46
  • 708

HDU 2767--Proving Equivalences【scc缩点构图 && 求向图中最少增加多少条边才可以使新图强连通】

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
  • hpuhjh
  • hpuhjh
  • 2015年08月19日 17:35
  • 821

hdu2767&&hdu3836 Proving Equivalences(Tarjan+缩点)

http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给你n个命题,m个推导,求增加多少条推导就能使命题两两等价。 ps:这里终于看懂九野巨的模板了...

hdu 2767 Proving Equivalences (tarjan + 缩点)

/* 题目大意:给定一张有向图,问最少添加几条边使得有向图成为一个强连通图。 解题思路:缩点后找入度为0的点和出度为0的点,统计个数,选择大的那个数就是答案。 如果出度为0的个数n...
  • LiWen_7
  • LiWen_7
  • 2012年08月25日 01:33
  • 347

HDU 2767 Proving Equivalences(强连通分量)

题目地址:HDU 2767 这题的意思是求再加多少边可以使得图为强连通图。 方法是先缩点,缩点很简单,只要加个数组,在找到一个强连通分量的时候让那些点都标记为该强连通分量的标号即可。 然后再遍历...

HDU 2767 Proving Equivalences(强连通分量)

HDU 2767 Proving Equivalences(强连通分量) http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给你一个有向图,问你在图中...

HDU 2767-Proving Equivalences(强联通+缩点)

欢迎使用Markdown编辑器写博客本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和...

hdu2767 Proving Equivalences

scc缩点+计数
  • sdfzyhx
  • sdfzyhx
  • 2016年08月06日 15:50
  • 286
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDOJ--2767--Proving Equivalences
举报原因:
原因补充:

(最多只允许输入30个字)