hdoj2767Proving Equivalences【scc+缩点】

原创 2015年11月20日 21:21:33

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
 
题意 :同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;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

HDU 2767 Proving Equivalences(强连通 Tarjan+缩点)

HDU 2767 Proving Equivalences(强连通 Tarjan+缩点) ACM 题目地址:HDU 2767 题意:  给定一张有向图,问最少添加几条边使得有向图成...
  • hcbbt
  • hcbbt
  • 2014年07月30日 15:40
  • 2155

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

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

hdu 2767 强连通缩点

补最少的边成强连通图。 缩点后成DAG,max{ 入度为0点数, 出度为零点数}即为所求。 #include #include #include #define Mn 20020 #defi...

hdu2767之强联通缩点

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

hdu2767Proving Equivalences【STL版SCCTarjan+缩点】(有注释)

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

hdu2767(图的强连通)

//题意:问需要添加几条边使得这张图成为每个点都等价(强连通图)我们先把图中的强连通分量缩点 可能他本身就是满足条件,那么直接输出0 经过缩点后,就可以把强连通分量看成一个个独立的点,在这张图上搞...

HDU 2767 强联通分量

点击打开链接 题意:问加多少边后图会变成强联通分量为1的图 思路:简单的强联通,缩点后找入度和出度就行了,水题#include #include #include #include #in...
  • Dan__ge
  • Dan__ge
  • 2016年05月23日 21:24
  • 4084

强连通缩点学习小结-附加两个强连通缩点题poj2186、hdu2767

相信很多刚接触强连通的人都会接触到强连通缩点这个词,也知道缩点是什么意思,但是不知道怎么求解这个缩点,缩点的最大好处在于把一个杂乱无章的有向图变成一个有向无环图,这里给出详细分析,希望对刚接触的人有帮...

hdu2767 Proving Equivalences【强连通】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:给你n个矩阵,问你要证明多少次,才能证明这n个矩阵之间相互相等,比如要证明a,b,c,d...

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
  • 780
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:hdoj2767Proving Equivalences【scc+缩点】
举报原因:
原因补充:

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