hdu 2767

来源:互联网 发布:搞基漫画软件 编辑:程序博客网 时间:2024/05/29 02:36

Proving Equivalences

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


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
 

类似poj1236,不解释。

wa的原因:vector没有清空。

#include<iostream>#include<string.h>#include<stdio.h>#include<vector>using namespace std;#define max_n 20005#define max_e 50002int stack[max_n],top;//栈int isInStack[max_n];//是否在栈内int low[max_n],dfn[max_n],tim;//点的low,dfn值;time从1开始int node_id;int head[max_n],s_edge;//邻接表头  s_edge从1开始int gro_id[max_n];int n,m;int in[max_n],out[max_n];struct Node{    int to;    int next;} edge[max_e];void init(){    top=0;    node_id=0;    memset(isInStack,0,sizeof(isInStack));    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    tim=0;    memset(in,0,sizeof(in));    memset(out,0,sizeof(out));    memset(head,0,sizeof(head));    s_edge=0;    memset(edge,0,sizeof(edge));}void addedge(int u,int v){    s_edge++;    edge[s_edge].to=v;    edge[s_edge].next=head[u];    head[u]=s_edge;}int max(int a,int b){    if(a>b)return a;    else return b;}void tarjan(int u){    //low值为u或u的子树能够追溯到得最早的栈中节点的次序号    stack[top++]=u;    isInStack[u]=1;    dfn[u]=++tim; //记录点u出现的记录,并放在栈中    low[u]=tim;    int e,v;    for(e=head[u]; e; e=edge[e].next) //如果是叶子节点,head[u]=0,edge[e].next=0;    {        v=edge[e].to;        if(!dfn[v])        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(isInStack[v])            low[u]=min(low[u],dfn[v]);    }    int j;    if(dfn[u]==low[u])    {        node_id++;        while(j=stack[--top])        {            isInStack[j]=0;            gro_id[j]=node_id;            if(j==u)break;        }    }}void find(){    for(int i = 1 ; i <=n ; ++i)    {        if(!dfn[i])        {            tarjan(i);        }    }}vector<int> vec[max_n];int main(){    int a,b,cas;    cin>>cas;    while(cas--)    {        init();        scanf("%d%d",&n,&m);        for(int i=1; i<=n; i++)        {            vec[i].clear();        }        for(int i = 0 ; i <m ; ++i)        {            scanf("%d%d",&a,&b);            vec[a].push_back(b);            addedge(a,b);        }        find();        for(int i=1; i<=n; i++)        {            for(int j=0; j<vec[i].size(); j++)//求入度为零的点            {                if(gro_id[i]!=gro_id[vec[i][j]])                {                    out[gro_id[i]]++;                    in[gro_id[vec[i][j]]]++;                }            }        }        if(node_id==1)        {            cout<<"0"<<endl;        }        else        {            int sum=0,sum1=0;            for(int i=1; i<=node_id; i++)            {                if(in[i]==0)                    sum++;                if(out[i]==0)                    sum1++;            }            printf("%d\n",max(sum,sum1));        }    }    return 0;}



 

原创粉丝点击