Proving Equivalences (hdu 2767 强联通缩点)

原创 2015年07月06日 16:43:41

Proving Equivalences

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


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 
 


题意:n个点m条边,问最少添加多少条边使得整个图联通。

思路:先Tarjan求强联通分量,缩点,再求缩点后的点的入度和出度,入读为0的点的个数为a,出度为0的点的个数为b,ans=max(a,b)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

const int MAXN = 20050;//点数
const int MAXM = 500050;//边数

struct Edge
{
    int to,next;
}edge[MAXM];

int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index,top;
int scc;//强联通分量的个数
bool Instack[MAXN];
int num[MAXN];//各个强联通分量包含的点的个数,数组编号为1~scc
//num数组不一定需要,结合实际情况

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}

void Tarjan(int u)
{
    int v;
    Low[u]=DFN[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    for (int i=head[u];i+1;i=edge[i].next)
    {
        v=edge[i].to;
        if (!DFN[v])
        {
            Tarjan(v);
            if (Low[u]>Low[v]) Low[u]=Low[v];
        }
        else if (Instack[v]&&Low[u]>DFN[v])
            Low[u]=DFN[v];
    }
    if (Low[u]==DFN[u])
    {
        scc++;
        do{
            v=Stack[--top];
            Instack[v]=false;
            Belong[v]=scc;
            num[scc]++;
        }while (v!=u);
    }
}

void solve(int N)
{
    memset(DFN,0,sizeof(DFN));
    memset(Instack,false,sizeof(Instack));
    memset(num,0,sizeof(num));
    Index=scc=top=0;
    for (int i=1;i<=N;i++)      //点的编号从1开始
        if (!DFN[i])
            Tarjan(i);
}

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}

int n,m;
int in[MAXN],out[MAXN];

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
    int i,j,u,v,t;
    sf(t);
    while (t--)
    {
        sff(n,m);
        if(n==1){   //特判1(n==1,m==0)
            printf("0\n");
            continue;
        }
        if(m==0){   //特判2( n==?,m==0)
            printf("%d\n",n);
            continue;
        }
        init();
        for (i=0;i<m;i++)
        {
            sff(u,v);
            addedge(u,v);
        }
        solve(n);
        if(scc==1){   //如果强连通个数为1
            printf("0\n");
            continue;
        }
        mem(in,0);
        mem(out,0);
        for (int u=1;u<=n;u++)
        {
            for (i=head[u];i+1;i=edge[i].next)
            {
                int v=edge[i].to;
                if (Belong[u]!=Belong[v])
                {
                    out[Belong[u]]++;
                    in[Belong[v]]++;
                }
            }
        }
        int ans,a=0,b=0;
        for (i=1;i<=scc;i++)
        {
            if (out[i]==0)
                a++;
            if (in[i]==0)
                b++;
        }
        ans=max(a,b);
        pf("%d\n",ans);
    }
    return 0;
}



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

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

hdu 2767 Proving Equivalences 等价性证明 强连通分量

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
  • cyendra
  • cyendra
  • 2013年04月17日 15:07
  • 662

Proving Equivalences HDU - 2767

点击打开链接 求再加多少边可以使整个图构成一强连通分量 tarjan缩点即可 一开始想找缩点后有多少链 再把链连起来。。发现错的离谱 找入度为零的点和出度为零的点各有多少 取最大值即可 有点贪心...
  • sunyutian1998
  • sunyutian1998
  • 2017年11月07日 10:57
  • 43

HDU 2767 Proving Equivalences

题意: 给你n条声明和m条启示,每条启示表示声明a能推出声明b,但是b不一定能推出a,问你最小需要添加多少条启示才能任选两条启示都能互相推导出来。 思路: 说白了就是给你n个点m条边的无向图,问...
  • w446506278
  • w446506278
  • 2016年07月23日 20:01
  • 111

HDU 2767 Proving Equivalences

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot...
  • hqu_fritz
  • hqu_fritz
  • 2014年09月17日 07:58
  • 391

HDU 2767 Proving Equivalences 增加最小边使图为强连通

题意:数学的稳定证明,也就是一个定义x可以通过若干次的等价变换再次转换到x则表示定义x与在变换过程中用到的定义为稳定的,问至少还要添加多少条等价变换可以使给出的所有定义稳定? 想法:tarj...
  • Triple_WDF
  • Triple_WDF
  • 2016年05月05日 20:25
  • 245

hdu 2767 Proving Equivalences

这道题是问最少添加几条边使得整个图强连通。首先,我们知道强连通是一个等价的关系,那么我们可以将强连通分量缩成一个点,即这个强连通分量的编号,这个过程称为强连通缩点。缩点后图变成一个DAG(有向无环),...
  • Unin88
  • Unin88
  • 2015年03月11日 16:24
  • 271

HDU 2767 Proving Equivalences

强连通分量
  • sky_miange
  • sky_miange
  • 2015年05月10日 22:24
  • 538

HDU 2767 Proving Equivalences(强连通分量)

题目地址:HDU 2767 这题的意思是求再加多少边可以使得图为强连通图。 方法是先缩点,缩点很简单,只要加个数组,在找到一个强连通分量的时候让那些点都标记为该强连通分量的标号即可。 然后再遍历...
  • u013013910
  • u013013910
  • 2014年08月25日 17:02
  • 625

hdoj 2767 Proving Equivalences 【有向图 增加最少的边使图强连通】

Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2015年07月18日 21:04
  • 605
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Proving Equivalences (hdu 2767 强联通缩点)
举报原因:
原因补充:

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