Proving Equivalences

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

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
 
 
代码:
 #include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int head[N],dfn[N],low[N],belong[N],stak[N],instack[N];
int in[N],out[N];
int incnt,outcnt;
int cnt,indexx,top,ans;
struct node{
int u,v,next;
}edge[N*]; void add(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
} void Init()
{
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(instack,,sizeof(instack));
cnt=indexx=top=ans=;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
incnt=outcnt=;
} void tarjan(int u)
{
dfn[u]=low[u]=++indexx;
stak[++top]=u;
instack[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u]){
ans++;
while(){
int v=stak[top--];
instack[v]=;
belong[v]=ans;
if(u==v)
break;
}
}
} int main()
{
int T,n,m;
int u,v;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
Init();
while(m--){
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=; i<=n; i++){
if(!dfn[i])
tarjan(i);
}
if(ans==){
printf("0\n");
continue;
}
for(int i=; i<=n; i++){
for(int j=head[i]; j!=-; j=edge[j].next){
int v=edge[j].v;
if(belong[v]!=belong[i]){
in[belong[v]]++;
out[belong[i]]++;
}
}
}
for(int i=; i<=ans; i++){
if(!in[i])
incnt++;
if(!out[i])
outcnt++;
}
printf("%d\n",max(incnt,outcnt));
}
return ;
}

HDU 2767.Proving Equivalences-强连通图(有向图)+缩点的更多相关文章

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

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

  2. hdu 2767 Proving Equivalences(tarjan缩点)

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

  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 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. hdu 2767 Proving Equivalences

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

  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 强连通缩点

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

  8. HDU 2767:Proving Equivalences(强连通)

    题意: 一个有向图,问最少加几条边,能让它强连通 方法: 1:tarjan 缩点 2:采用如下构造法: 缩点后的图找到所有头结点和尾结点,那么,可以这么构造:把所有的尾结点连一条边到头结点,就必然可以 ...

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

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

随机推荐

  1. CentOS6.5生产环境系统安装

    CentOS 6.5系统安装 1-1 将预先准备的CentOS 6.5安装光盘插入光驱中,开机/重启系统时,系统会进行自检,自检完毕就会出现安装系统时的引导界面,如图1-1所示.1-2 使用键盘方向键 ...

  2. 菜鸟学Linux - 文件/文件夹的隐藏属性

    文件/文件夹居然还有隐藏属性?没错,隐藏属性对于文件/文件夹的安全很重要.好比如说,我们需要使用”鉴定符“来揭开装备的隐藏属性:在Linux中chattr/lsattr就是“鉴定符”. chattr基 ...

  3. 成为谷歌的java程序员首先要做到这五点!

    成为谷歌的java程序员首先要做到这五点! 在现在,就是现在,程序员称霸武林,但是这是一个现实的社会,并没有天下第一这么一说,总是人外有人山外有山,想要成为谷歌程序员,你还要听听谷歌员工给的5个重要建 ...

  4. 3、CSS基础 part-1

    1.给body设置颜色 <html> <body text="red"> <p> hello world</p> <p> ...

  5. leetcode 【 Find Peak Element 】python 实现

    题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...

  6. ogre3D学习基础2 -- 顶点程序与片断程序

    三.顶点程序与片断程序 顶点或者片断程序定义可以被多个材质使用,唯一的前提条件是必须在引用它之前在材质的渲染通路部分中定义. 一个低级顶点程序示例如下: vertex_program myVertex ...

  7. 第九届极客大挑战 部分WP

    CODE 和0xpoker分0day 百度 取石子游戏. https://blog.csdn.net/qq_33765907/article/details/51174524 已经说得很详细了,慢慢来 ...

  8. Robotium之Android控件定位实践和建议

    本人之前曾经撰文描述Appium和UIAutomator框架是如何定位Android界面上的控件的. UIAutomator定位Android控件的方法实践和建议Appium基于安卓的各种FindEl ...

  9. SpringBoot中Async异步方法和定时任务介绍

    1.功能说明 Spring提供了Async注解来实现方法的异步调用. 即当调用Async标识的方法时,调用线程不会等待被调用方法执行完成即返回继续执行以下操作,而被调用的方法则会启动一个独立线程来执行 ...

  10. "R6002 floating point support not loaded"错误

    R6002 floating point support not loaded 错误,在Debug模式下会弹出如下错误: "floating point support not loaded ...