HDU 2767 Proving Equivalences (HDU 3836) 至少加几条边让整个图变成强连通+邻接表建图

来源:互联网 发布:矩阵行列式计算公式 编辑:程序博客网 时间:2024/06/05 18:47

点击打开链接

 

Proving Equivalences

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


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
24 03 21 21 3
 


 

Sample Output
42
 


 

Source
NWERC 2008
 


 

Recommend
lcy

 

题意是说给你一个有向图,让你加最少的边使任意两点想通。

强连通缩点,求出所有点入度为0的个数和出度为0的个数,然后取较大的那个即可,当只有一个点时,输出0。

一开始用直接map[][]连边结果MLE。所以用邻接表建图效果更好。

 

#include<stdio.h>#include<string.h>#define min(a,b) (a<b?a:b)#define max(a,b) (a>b?a:b)#define M 20017bool vis[M];int head[M],low[M],dfn[M],indegree[M],outdegree[M];int stack[M],belong[M];int n,m,cnt,top,count,num;struct node{    int s,e,pre;}g[M*3];void insert(int a,int b){    g[num].s=a;    g[num].e=b;    g[num].pre=head[a];    head[a]=num++;}void init(){    cnt=top=count=num=0;    memset(head,-1,sizeof(head));    memset(vis,false,sizeof(vis));    memset(dfn,0,sizeof(dfn));    memset(indegree,0,sizeof(indegree));    memset(outdegree,0,sizeof(outdegree));}void tarjan(int x){    dfn[x]=low[x]=++cnt;    stack[top++]=x;    vis[x]=true;    for(int i=head[x];i!=-1;i=g[i].pre)    {        int u=g[i].e;        if(!dfn[u])        {            tarjan(u);            low[x]=min(low[x],low[u]);        }        else if(vis[u])        {            low[x]=min(low[x],dfn[u]);        }    }    if(low[x]==dfn[x])    {        count++;        int tmp;        while(1)        {            tmp=stack[--top];            belong[tmp]=count;            vis[tmp]=false;            if(tmp==x)                break;        }    }}void output(){    int inde=0,outde=0;    for(int i=1;i<=n;i++)    {        int v=belong[i];        for(int j=head[i];j!=-1;j=g[j].pre)        {            int u=belong[g[j].e];            if(v!=u)            {                indegree[u]++;                outdegree[v]++;            }        }    }    for(int i=1;i<=count;i++)    {        if(indegree[i]==0)inde++;        if(outdegree[i]==0)outde++;    }    int ans=max(inde,outde);    printf("%d\n",ans);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        init();        scanf("%d%d",&n,&m);        int a,b;        for(int i=0;i<m;i++)        {            scanf("%d%d",&a,&b);            insert(a,b);        }        for(int i=1;i<=n;i++)            if(!dfn[i])                tarjan(i);        if(count==1)        {            printf("0\n");            continue;        }        output();    }    return 0;}


 

原创粉丝点击