Proving Equivalences

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
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

/*刚开始是强连通小白,现在稍微明白一点了,入度与出度有点难理解;
现在看来,应该是这样的,入度表示当前的scc上边有几个scc,出度是
当前scc下边有几个scc,如果上边或者下边没有点,这就说明,缩点后
这是一个叶子节点或者根节点。对于这道题来说,我们应该分别统计一下
根节点和叶子节点的个数,取其最大值*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 50010
int in[MAX],out[MAX],sumin,sumout;
vector<int>G[MAX];
vector<int>scc[MAX];
struct node
{
int u,v;
int next;
}edge[MAX];
int head[MAX],cnt,scc_cnt,dfs_clock;
int sccno[MAX],low[MAX],dfn[MAX];
bool Instack[MAX];
int m,n;
stack<int>s;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
}
}
void suodian()
{
for(int i=1;i<=scc_cnt;i++)
G[i].clear(),in[i]=0,out[i]=0;//缩点时清空G[i],存放每一个scc
for(int i=0;i<cnt;i++)
{
int u=sccno[edge[i].u];
int v=sccno[edge[i].v];
if(u!=v)
//原图中的u--v现在变成了新图中的u--v,这条路变成了两个scc的链接
G[u].push_back(v),out[u]++,in[v]++;
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfs_clock;//更新时间戳
s.push(u);//标记u进栈
Instack[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next)
{//遍历u下边的每一个点,同时判断是否遍历过,是否在栈里
v=edge[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
//新的scc出现的标志,不好理解的话,可以想象一下一个普通的叶子节点
{
scc_cnt++;//由此可以看出,scc的编号从1开始
scc[scc_cnt].clear();//存放新发现的scc
for(;;)
{
v=s.top();
s.pop();//取出当前scc中的每一个点,放入vector
Instack[v]=false;
sccno[v]=scc_cnt;
scc[scc_cnt].push_back(v);
if(u==v) break;
}
}
}
void solve()
{
sumin=sumout=0;
if(scc_cnt==1)
{
printf("0\n");
}
else
{
for(int i=1;i<=scc_cnt;i++)
{
if(in[i]==0) sumin++;
if(out[i]==0) sumout++;
}
int sum=max(sumin,sumout);
printf("%d\n",sum);
}
}
void find(int l,int r)
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(sccno,0,sizeof(sccno));
dfs_clock=scc_cnt=0;
for(int i=l;i<=r;i++)
if(!dfn[i])
tarjan(i,-1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
getmap();
find(1,n);
suodian();
solve();
}
return 0;
}

hdoj--2767--Proving Equivalences (scc+缩点)的更多相关文章

  1. hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】

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

  2. hdu 2767 Proving Equivalences 强连通缩点

    给出n个命题,m个推导,问最少添加多少条推导,能够使全部命题都能等价(两两都能互推) 既给出有向图,最少加多少边,使得原图变成强连通. 首先强连通缩点,对于新图,每一个点都至少要有一条出去的边和一条进 ...

  3. HDU 2767 Proving Equivalences (强联通)

    pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...

  4. hdu 2767 Proving Equivalences

    Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...

  5. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

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

  6. HDU 2767 Proving Equivalences (Tarjan)

    Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  7. hdu 2767 Proving Equivalences(tarjan缩点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2767 题意:问最少加多少边可以让所有点都相互连通. 题解:如果强连通分量就1个直接输出0,否者输出入度 ...

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

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

  9. hdu2767 Proving Equivalences Tarjan缩点

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  10. UVALive 4287 Proving Equivalences(缩点)

    等价性问题,给出的样例为 a->b的形式,问要实现全部等价(即任意两个可以互相推出),至少要加多少个形如 a->b的条件. 容易想到用强连通缩点,把已经实现等价的子图缩掉,最后剩余DAG. ...

随机推荐

  1. CSS清除浮动_清除float浮——详解overflow:hidden 与clear:both属性

    最近刚好碰到这个问题,看完这个就明白了.写的很好,所以转载了! CSS清除浮动_清除float浮动 CSS清除浮动方法集合 一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里 ...

  2. 软件架构自学笔记——常见的软件架构(https://jiajunhuang.com/articles/2018_09_16-common_software_archtecture_pattern.md.html)

    分层模式 这种模式主要是将设计分层,每一层为其上层提供服务.例如:web开发中我们常常将某些常用的RESTful接口抽象出一个service层. 客户端-服务端模式 客户端和服务端分离,从而解耦.通过 ...

  3. Beta冲刺-星期三

    这个作业属于哪个课程  <课程的链接>            这个作业要求在哪里 <作业要求的链接> 团队名称 Three cobblers 这个作业的目标 剩余任务预估,分配 ...

  4. (转)i686只是cpu的指令等级,包括32bit和64bit

    i代表intel系列的cpu 386 几乎适用于所有的 x86 平台,不论是旧的 pentum 或者是新的 pentum-IV 与 K7 系列的 CPU等等,都可以正常的工作! 那个 i 指的是 In ...

  5. 4、scala数组

    1.Array 2.ArrayBuffer 3.遍历Array和ArrayBuffer 4.数组常见操作 1.  Array Scala中,array代表的含义与java类似,也是长度不可改变的数组. ...

  6. 我的web前端自学之路-心得篇:我为什么要学习web前端?

    时光如流水,转眼间,自己已经是大三的学长了,看着一个个学弟学妹,心中有种莫名的感觉,很怀念大学的前两年时光,但也很憧憬着自己的未来,自己将要去经历很多从未经历的事.我是我们学校信科院的一名学生,在编程 ...

  7. eas之怎么设置单据保存或者提交完不跳到下个新增页面

    this.chkMenuItemSubmitAndAddNew.setSelected(false);

  8. Java核心技术读书笔记01

    Volume I Chapter 1  An Introduction to Java  • 1.1 Java as a Programming Platform• 1.2 The Java ‘Whi ...

  9. Codeforces 902D/901B - GCD of Polynomials

    传送门:http://codeforces.com/contest/902/problem/D 本题是一个数学问题——多项式整除. 对于两个整数a.b,求最大公约数gcd(a,b)的辗转相除法的函数如 ...

  10. 5、Linux的常用命令

    ls 查看当面目录结构 ls -l 列表查看当前目录 cd:切换目录 pwd:显示目前的目录 mkdir:创建一个新的目录 rmdir:删除一个空的目录 cp: 复制文件或目录 rm: 移除文件或目录 ...