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=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-;char ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
const int maxn=;
const int maxm=;
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,-);mem(dfn,);
mem(ind,);mem(outd,);
Blocks=tot=top=cnt=;
} 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]=;
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]=;
} while(t!=u);
}
}
void solve()
{
for(int i=;i<=n;++i)
if(!dfn[i]) Tarjan(i);
} int main()
{
T=read();
while(T--)
{
n=read();m=read();
Init();
for(int i=;i<=m;++i)
{
int u,v;
u=read();v=read();
vec.pb(mkp(u,v));
AddEdge(u,v);
}
solve();
if(Blocks==) {puts("");continue;} for(int i=,len=vec.size();i<len;++i)
{
int x=vec[i].fi,y=vec[i].se;
if(Belong[x]!=Belong[y])
outd[Belong[x]]=,ind[Belong[y]]=;
}
int ans,res1=,res2=;
for(int i=;i<=Blocks;++i)
{
if(!ind[i]) ++res1;
if(!outd[i]) ++res2;
}
ans=max(res1,res2); printf("%d\n",ans);
} return ;
}
 

HDU2767 Proving Equivalences(加边变为强联通图)的更多相关文章

  1. hdu2767 Proving Equivalences,有向图强联通,Kosaraju算法

    点击打开链接 有向图强联通,Kosaraju算法 缩点后分别入度和出度为0的点的个数 answer = max(a, b); scc_cnt = 1; answer = 0 #include<c ...

  2. 【强联通图 | 强联通分量】HDU 1269 迷宫城堡 【Kosaraju或Tarjan算法】

      为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明 ...

  3. HDU2767 Proving Equivalences

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

  4. hdu2767 Proving Equivalences Tarjan缩点

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

  5. hdu2767 Proving Equivalences --- 强连通

    给一个图,问至少加入�多少条有向边能够使图变成强连通的. 原图是有环的,缩点建图,在该DAG图上我们能够发现,要使该图变成强连通图必须连成环 而加入�最少的边连成环,就是把图上入度为0和出度为0的点连 ...

  6. 判断强联通图中每条边是否只在一个环上(hdu3594)

    hdu3594 Cactus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  7. HDU 4635 多校第四场 1004 强联通

    我还有什么好说,还有什么好说...... 我是SBSBSBSBSBSBSBSBSBSBSBSBBSBSBSBSBSBSBSBSBS........................ 题意 思路什么的都不 ...

  8. POJ 2762Going from u to v or from v to u?(强联通 + 缩点 + 拓扑排序)

    [题意]: 有N个房间,M条有向边,问能否毫无顾虑的随机选两个点x, y,使从①x到达y,或者,②从y到达x,一定至少有一条成立.注意是或者,不是且. [思路]: 先考虑,x->y或者y-> ...

  9. Proving Equivalences(加多少边使其强联通)

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

随机推荐

  1. Java ->在mybatis和PostgreSQL Json字段作为查询条件的解决方案

    Date:2019-11-15 读前思考: 你没想到解决办法? PostgreSQL 数据库本身就支持还是另有解决办法? 说明:首先这次数据库使用到Json数据类型的原因,这次因为我们在做了一个app ...

  2. python 快速发送大量邮件

    因为公司需求,需要发送千万封级别邮件. # coding:utf-8 import csv import smtplib from email.mime.text import MIMEText im ...

  3. java中的线程安全

    在Java中,线程的安全实际上指的是内存的安全,这是由操作系统决定的. 目前主流的操作系统都是多任务的,即多个进程同时运行.为了保证安全,每个进程只能访问分配给自己的内存空间,而不能访问别的.分配给别 ...

  4. python 豆瓣top250电影的爬取

    我们先看一下豆瓣的robot.txt 然后我们查看top250的网页链接和源代码 通过对比不难发现网页间只是start数字发生了变化. 我们可以知道电影内容都存在ol标签下的 div class属性为 ...

  5. 南开大学校徽及手写字的Tikz源码

    话不多说,直接上内容. % ---------------------------------- % !TeX enginee = pdfLaTeX/XeLaTeX % !TeX encoding = ...

  6. [LC] 112题 路径总和(在二叉树里判断是否有哪条路径之和等于某个值)

    ①题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum ...

  7. 区块链轻松上手:原理、源码、搭建与应用pdf电子版下载

    区块链轻松上手:原理.源码.搭建与应用pdf电子版下载 链接:https://pan.baidu.com/s/1rKF4U9wq612RMIChs0zv8w提取码:hquz <区块链轻松上手:原 ...

  8. oracle 数据库,能不能将查询的结果创建成新表。

    这个是可以的.sql:create table tablename1 as select t2. * from tablename2 t2 where t2.filename =‘张三’. 解释:就是 ...

  9. Java虚拟机的内存

    JDK1.8之前,java内存分为 线程共享区:堆.方法区.直接内存(非运行时数据区的一部分).线程私有区:程序计数器.虚拟机栈.本地方法栈. JDK1.8开始,虚拟机取消了方法区,改为元空间. 程序 ...

  10. Java多线程——对象及变量的并发访问

    Java多线系列文章是Java多线程的详解介绍,对多线程还不熟悉的同学可以先去看一下我的这篇博客Java基础系列3:多线程超详细总结,这篇博客从宏观层面介绍了多线程的整体概况,接下来的几篇文章是对多线 ...