hdoj--2767--Proving Equivalences (scc+缩点)

Proving Equivalences

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
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
 

/*刚开始是强连通小白,现在稍微明白一点了,入度与出度有点难理解;
现在看来,应该是这样的,入度表示当前的scc上边有几个scc,出度是
当前scc下边有几个scc,如果上边或者下边没有点,这就说明,缩点后
这是一个叶子节点或者根节点。对于这道题来说,我们应该分别统计一下
根节点和叶子节点的个数,取其最大值*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 50010
int in[MAX],out[MAX],sumin,sumout;
vector<int>G[MAX];
vector<int>scc[MAX];
struct node
{
	int u,v;
	int next;
}edge[MAX];
int head[MAX],cnt,scc_cnt,dfs_clock;
int sccno[MAX],low[MAX],dfn[MAX];
bool Instack[MAX];
int m,n;
stack<int>s;
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
}
void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
void getmap()
{
	int a,b;
	while(m--)
	{
		scanf("%d%d",&a,&b);
		add(a,b);
	}
}
void suodian()
{
	for(int i=1;i<=scc_cnt;i++)
	G[i].clear(),in[i]=0,out[i]=0;//缩点时清空G[i],存放每一个scc 
	for(int i=0;i<cnt;i++)
	{
		int u=sccno[edge[i].u];
		int v=sccno[edge[i].v];
		if(u!=v)
		//原图中的u--v现在变成了新图中的u--v,这条路变成了两个scc的链接 
		G[u].push_back(v),out[u]++,in[v]++;
	}
}
void tarjan(int u,int fa)
{
	int v;
	low[u]=dfn[u]=++dfs_clock;//更新时间戳 
	s.push(u);//标记u进栈 
	Instack[u]=true;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{//遍历u下边的每一个点,同时判断是否遍历过,是否在栈里 
		v=edge[i].v;
		if(!dfn[v])
		{
			tarjan(v,u);
			low[u]=min(low[u],low[v]);
		}
		else if(Instack[v])
		low[u]=min(low[u],dfn[v]);
	}
	if(dfn[u]==low[u])
	//新的scc出现的标志,不好理解的话,可以想象一下一个普通的叶子节点 
	{
		scc_cnt++;//由此可以看出,scc的编号从1开始 
		scc[scc_cnt].clear();//存放新发现的scc 
		for(;;)
		{
			v=s.top();
			s.pop();//取出当前scc中的每一个点,放入vector 
			Instack[v]=false;
			sccno[v]=scc_cnt;
			scc[scc_cnt].push_back(v);
			if(u==v) break;
		}
	}
}
void solve()
{
	sumin=sumout=0;
	if(scc_cnt==1)
	{
		printf("0
");
	}
	else
	{
		for(int i=1;i<=scc_cnt;i++)
		{
			if(in[i]==0) sumin++;
			if(out[i]==0) sumout++;
		}
		int sum=max(sumin,sumout);
		printf("%d
",sum);
	}
}
void find(int l,int r)
{
	memset(low,0,sizeof(low));
	memset(dfn,0,sizeof(dfn));
	memset(sccno,0,sizeof(sccno));
	dfs_clock=scc_cnt=0;
	for(int i=l;i<=r;i++)
	if(!dfn[i])
	tarjan(i,-1);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		init();
		getmap();
		find(1,n);
		suodian();
		solve();
	}
	return 0;
}


原文地址:https://www.cnblogs.com/playboy307/p/5273782.html