【题目链接】hdu-2767

【题目描述】

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?

【输入格式】

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.

【输出格式】

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.

【分析】

就是要求最少加多少条边可以使这个图变成强连通的。做完tarjan后,我们就找入度和出度为0的点,取最大值。

【代码】

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cctype>
#include <cmath>
#include <time.h> using namespace std; const int maxm=50010;
const int maxn=20010; struct Edge{
int to,next;
}edge[maxm<<1]; int nedge,sum,dep,top,n,m,cnt;
int head[maxn],dfn[maxn],stack[maxm],low[maxm],od[maxm],vis[maxm],id[maxm];
int belong[maxn]; inline int read()
{
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
} void tarjan(int u)
{
dfn[u]=low[u]=++dep;
stack[top++]=u;
vis[u]=1;
for (int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if (!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else
{
if (vis[v]) low[u]=min(low[u],dfn[v]);
}
}
int j;
if (dfn[u]==low[u])
{
sum++;
do{
j=stack[--top];
belong[j]=sum;
vis[j]=0;
}while (u!=j);
}
} void add_edge(int a,int b)
{
edge[nedge]=(Edge){b,head[a]}; head[a]=nedge++;
} int main()
{
int cas=read();
while (cas--)
{
nedge=0;
memset(head,-1,sizeof(head));
memset(dfn,0,sizeof(dfn));
memset(od,0,sizeof(od));
memset(id,0,sizeof(id));
memset(vis,0,sizeof(vis));
memset(belong,0,sizeof(belong));
sum=0,dep=0,top=0,cnt=0;
n=read(),m=read();
for (int i=1;i<=m;i++)
{
int a=read(),b=read();
add_edge(a,b);
}
for (int i=1;i<=n;i++)
{
if (!dfn[i]) tarjan(i);
}
if (sum==1)
{
printf("0\n");
}
else
{
for (int i=1;i<=n;i++)
{
for (int j=head[i];j!=-1;j=edge[j].next)
{
int v=edge[j].to;
if (belong[i]!=belong[v]) od[belong[i]]++,id[belong[v]]++;
}
}
int idnum=0,odnum=0;
for (int i=1;i<=sum;i++)
{
if (id[i]==0) idnum++;
if (od[i]==0) odnum++;
}
int ans=max(idnum,odnum);
printf("%d\n",ans);
}
}
return 0;
}

【强连通分量】Proving Equivalences的更多相关文章

  1. UVa 12167 & HDU 2767 强连通分量 Proving Equivalences

    题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. #i ...

  2. Proving Equivalences(缩点+有环图变强连通分量)【tarjian算法】

    Proving Equivalences 题目链接(点击) 参考博客(点击) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768 ...

  3. UVALive Proving Equivalences (强连通分量,常规)

    题意: 给一个有向图,问添加几条边可以使其强连通. 思路: tarjan算法求强连通分量,然后缩点求各个强连通分量的出入度,答案是max(入度为0的缩点个数,出度为0的缩点个数). #include ...

  4. UVALive-4287 Proving Equivalences 有向图的强连通分量+缩点

    题意:有n个命题,已知其中的m个推导,要证明n个命题全部等价(等价具有传递性),最少还需要做出几次推导. 思路:由已知的推导可以建一张无向图,则问题变成了最少需要增加几条边能使图变成强连通图.找出所有 ...

  5. UVALive - 4287 - Proving Equivalences(强连通分量)

    Problem   UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...

  6. Proving Equivalences UVALive - 4287(强连通分量 水题)

    就是统计入度为0 的点 和 出度为0 的点  输出 大的那一个,, 若图中只有一个强连通分量 则输出0即可 和https://www.cnblogs.com/WTSRUVF/p/9301096.htm ...

  7. UVALive-4287 Proving Equivalences (有向图的强连通分量)

    题目大意:有n个命题,已知其中的m个推导,要证明n个命题全部等价(等价具有传递性),最少还需要做出几次推导. 题目分析:由已知的推导可以建一张无向图,则问题变成了最少需要增加几条边能使图变成强连通图. ...

  8. UVALIVE 4287 Proving Equivalences (强连通分量+缩点)

    题意:给定一个图,问至少加入多少条边能够使这个图强连通. 思路:首先求出这个图的强连通分量.然后把每个强连通分量缩成一个点.那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的 ...

  9. HDU2767Proving Equivalences[强连通分量 缩点]

    Proving Equivalences Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  10. HD2767Proving Equivalences(有向图强连通分量+缩点)

    题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...

随机推荐

  1. 【NX二次开发】Block UI 选择单元

    属性说明 属性   类型   描述   常规           BlockID    String    控件ID    Enable    Logical    是否可操作    Group    ...

  2. 拦截导弹(CDQ分治,DP)

    很好的题,值得细细说,(果然又是个假期望)....... 首先我们提取信息,显然这是个三维偏序问题 用简单的DP式子表示需要满足 f[i]=max(f[1--j]+1)(v[j]<v[i],h[ ...

  3. 飞(fly)(数学推导,liu_runda的神题)

    大概看了两三个小时的题解,思考量很大,实现简单........ 20分: 明显看出,每个点的贡献是x*(x-1)/2;即组合数C(x,2),从x个线段中选出2个的方案数,显然每次相交贡献为1,n^2枚 ...

  4. 源码学习之void 0

    今天看源码的时候看到 void 0 这样的写法,平时在业务代码里基本没有这样的写法,于是学习了一下. 在控制台运行了一下void 0,得到返回值是undefined. 在MDN上搜了一下void,了解 ...

  5. Java字符串比较(3种方法)以及对比 C++ 时的注意项

    字符串比较是常见的操作,包括比较相等.比较大小.比较前缀和后缀串等.在 Java 中,比较字符串的常用方法有 3 个:equals() 方法.equalsIgnoreCase() 方法. compar ...

  6. CMD批处理(2)——批处理常用符号总结

    @ 一般在它之后紧跟一条语句,则命令或语句本身在执行的时候不会显示在屏幕上. 例.创建一个test1.bat脚本文件,输入以下内容 echo apause@echo b@pause 双击test1.b ...

  7. js笔记15

    DOM2动态创建节点 1.生成节点的方法 document.createElement("div") 2.插入节点的方法 父元素.appendChild(新节点) 在父节点的子节点 ...

  8. excel VBA正则匹配单元格符号,并按符号把单元格拆分行(这里是按第一列分行,分行是从活动单元格的行开始,分行前需要选择所有需要填充内容的列,否则需要后期手动填充)

    Sub W()   ' MsgBox "行数:" & Selection.Rows.Count    Dim rows_count As Integer    Dim ro ...

  9. Linux系统安装-C7

    1.安装部署操作系统 (1)创建虚拟机,加载系统镜像 (2)进入系统引导界面进行配置 补充:centos7系统网卡名称 默认系统的网卡名称为 eth0 eth1 –centos6 默认系统的网卡名称为 ...

  10. 10、nginx+uwsgi+django部署(动静分离)

    10.1.说明: 1.介绍: 创建Django项目,可以通过 pyhon3 manage.py runserver 0.0.0.0:8080 & 命令更方便地调试程序,但是如果当一个项目完成了 ...