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
     
  • 相关阅读:
    基于node.js+socket.io+html5实现的斗地主游戏(1)概述
    [javascript]switchTab:仿腾讯首页Tab栏切换js插件
    [js]利用闭包向post回调函数传参数
    [CSS]利用伪元素实现一些特殊图形 from baidu校招
    [javascript]模块化&命名污染—from 编程精解
    [随手记]2014前端面试题解
    [IE bug] ajax请求 304解决方案
    [java]基于UDP的Socket通信Demo
    [JQuery]ScrollMe滚动特效插件
    java.lang.OutOfMemoryError及解决方案
  • 原文地址:https://www.cnblogs.com/csushl/p/11306898.html
Copyright © 2011-2022 走看看