zoukankan      html  css  js  c++  java
  • HDU2767 Proving Equivalences(加边变为强联通图)

    Proving Equivalences

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


    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
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2768 2766 2769 2773 2772 

    题解:题目问你最少加多少遍使得图中的任意两点之间乐意互相到达。
    当一个有向图的强连通分量为一时,满足条件。怎样变成强连通分量为一的有向图呢?
    先用Tarjan缩点,然后在新图中统计入度为零的点数和出度为零的点数,取最大值就是需要加的最少的边。
     
    参考代码:
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define pii pair<int,int>
    #define pil pair<int,ll>
    #define fi first
    #define se second
    #define mkp make_pair
    #define pb push_back
    #define mem(a,b) memset(a,b,sizeof(a))
    #define PI acos(-1.0)
    const int INF=0x3f3f3f3f;
    const ll inf=0x3f3f3f3f3f3f3f3fll;
    inline int read()
    {
        int x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-') f=-1;char ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
        return x*f;    
    }
    const int maxn=20010;
    const int maxm=50010;
    vector<pii> vec;
    int T,n,m,head[maxn],cnt;
    int dfn[maxn],lown[maxn],Stack[maxn];
    int InStack[maxn],Belong[maxn],Blocks,top,tot;
    int ind[maxn],outd[maxn];
    struct Edge{
        int to,nxt;
    } edge[maxm];
    
    void Init()
    {
        vec.clear();
        mem(head,-1);mem(dfn,0);
        mem(ind,0);mem(outd,0);
        Blocks=tot=top=cnt=0;    
    }
    
    void AddEdge(int u,int v)
    {
        edge[cnt].to=v;
        edge[cnt].nxt=head[u];
        head[u]=cnt++;    
    }
    
    void Tarjan(int u)
    {
        dfn[u]=lown[u]=++tot;
        InStack[u]=1;
        Stack[top++]=u;
        for(int e=head[u];~e;e=edge[e].nxt)
        {
            int v=edge[e].to;
            if(!dfn[v])
            {
                Tarjan(v);
                lown[u]=min(lown[u],lown[v]);
            }
            else if(InStack[v]&&dfn[v]<lown[u])
                lown[u]=dfn[v];
        }
        if(dfn[u]==lown[u])
        {
            int t; Blocks++;
            do{
                t=Stack[--top];
                Belong[t]=Blocks;
                InStack[t]=0;
            } while(t!=u);        
        }
    }
    void solve()
    {
        for(int i=1;i<=n;++i)
            if(!dfn[i])    Tarjan(i);
    }
    
    
    int main()
    {
        T=read();
        while(T--)
        {
            n=read();m=read();
            Init();
            for(int i=1;i<=m;++i) 
            {
                int u,v;
                u=read();v=read();
                vec.pb(mkp(u,v));
                AddEdge(u,v);
            }
            solve();
            if(Blocks==1) {puts("0");continue;}
            
            for(int i=0,len=vec.size();i<len;++i)
            {
                int x=vec[i].fi,y=vec[i].se;
                if(Belong[x]!=Belong[y]) 
                    outd[Belong[x]]=1,ind[Belong[y]]=1;    
            }
            int ans,res1=0,res2=0;
            for(int i=1;i<=Blocks;++i)
            {
                if(!ind[i]) ++res1;
                if(!outd[i]) ++res2;
            }
            ans=max(res1,res2);
            
            printf("%d
    ",ans);
        }
        
        return 0;    
    }
    View Code
     
  • 相关阅读:
    sqlserver数据导入问题:报错“对COM组件的调用返回了错误HRESULT E_FAIL”
    删除文件夹提示”该项目不在请确认该项目位置“
    delphi三层DCOM架构
    sqlserver 将店铺表转换成可以用in查询的字符串
    Delphi如何获取一个字符串再另一个字符串中最后一次出现的位置
    c# 通过地址下载流然后保存文件到本地
    使用SQL语句清空数据库所有表的数据
    SqlServer 查询的时候过滤条件有参数导致速度很慢的问题-参数嗅探
    delphi和C# 保存exe文件到数据库
    DELPHI 多线程(TThread类的实现)
  • 原文地址:https://www.cnblogs.com/csushl/p/11306898.html
Copyright © 2011-2022 走看看