您的位置:首页 > 产品设计 > UI/UE

hdoj2767Proving Equivalences【scc+缩点】

2015-11-20 21:21 381 查看


Proving Equivalences

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4470    Accepted Submission(s): 1577


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

 

Source

NWERC 2008

 
题意 :同hdoj3836
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=100010;
int dfs_clock,scc_cnt;
int head[maxn];
int low[maxn];
int dfn[maxn];
int in[maxn];
int out[maxn];
int sccno[maxn];
bool instack[maxn];
stack<int>S;
vector<int>scc[maxn];
vector<int>G[maxn];
struct Node{
int from,to,next;
}A[maxn];
int MAX(int a,int b){
return a>b?a:b;
}
int MIN(int a,int b){
return a<b?a:b;
}
void init(){
dfs_clock=scc_cnt=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
memset(instack,false,sizeof(instack));
memset(head,-1,sizeof(head));
memset(sccno,0,sizeof(sccno));
}
void tarjan(int u,int pre){
int v;
dfn[u]=low[u]=++dfs_clock;
instack[u]=true;S.push(u);
for(int k=head[u];k!=-1;k=A[k].next){
v=A[k].to;
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(low[u]==dfn[u]){
scc_cnt++;scc[scc_cnt].clear();
G[scc_cnt].clear();
while(1){
v=S.top();S.pop();
instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(u==v)break;
}
}
}
void suodian(int m){
for(int i=0;i<m;++i){
int u=sccno[A[i].from];
int v=sccno[A[i].to];
if(u!=v){
G[u].push_back(v);
in[v]++;out[u]++;
}
}
}
int main()
{
int i,j,k,n,m,t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&m);
for(i=0;i<m;++i){
scanf("%d%d",&A[i].from,&A[i].to);
A[i].next=head[A[i].from];
head[A[i].from]=i;
}
for(i=1;i<=n;++i){
if(!dfn[i]){
tarjan(i,-1);
}
}
if(scc_cnt==1){
printf("%d\n",0);
continue;
}
suodian(m);
int ans1=0,ans2=0;
for(i=1;i<=scc_cnt;++i){
if(in[i]==0)ans1++;
if(out[i]==0)ans2++;
}
printf("%d\n",MAX(ans1,ans2));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdoj2767Proving Equi