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. vue cli3.0^版本处理文件下载的问题

    downloadFile(url, fileName) { axios.get(url, { responseType: 'blob' }) .then(({ data }) => { // 为 ...

  2. Eclipse对Java项目打Jar包

    在本Java项目中,如下图一所示,Java项目含有外部依赖Jar包 -- fastjson-1.2.29.jar  包. 在经历了多次的失败后,最后我终于使用 Eclipse 对 "Java ...

  3. 本地Git连接GitLab(服务器)远程仓库

    1.简介 远程仓库是指托管在网络上的项目仓库,现在互联网上有很多项目托管平台,比如github.gitlab等.为了不公开自己项目代码,可以在自己的服务器上搭建自己的项目仓库,最常见的是搭建GitLa ...

  4. P4-verilog实现mips单周期CPU

    最近对学习的掌控可能出现了问题,左支右绌,p2挂了,p2.p3.p4.p5每周在计组花的连续时间少了很多,学习到的东西也少了很多,流水线都还没真正开始写,和别人比落后了一大截,随笔自然就荒废了,我得尽 ...

  5. opencv 4 图像处理 (1 线性滤波,非线性滤波)

    1 线性滤波:方框滤波.均值滤波.高斯滤波 1.1方框滤波(box Filter) 1.2均值滤波(blur函数) 缺陷: 1.3高斯滤波(GaussianBlur函数) 1.4线性滤波核心API函数 ...

  6. C#解析XML之流模型-XMLTextReader类

    C#读取XML文档之XMLTextReader 类有一些构造程序来适应各种各样的情况,比如从一个已经存在的数据流或统一资源定位网址读取数据.最常见的是,你或许想从一个文件读取XML数据,那么也就有一个 ...

  7. 【故障公告】docker swarm 集群问题造成新版博客后台故障

    非常抱歉,今天下午 16:55~17:05 左右,由于 docker swarm 集群的突发不稳定问题造成新版博客后台(目前处于灰度发布阶段)无法正常使用,由此给您带来麻烦,请您谅解. 出故障期时,新 ...

  8. GentOS 7 安装步骤

    附上原作者的博客网址: https://blog.csdn.net/qq_42570879/article/details/82853708 1.CentOS下载CentOS是免费版,推荐在官网上直接 ...

  9. Redis开发与运维:SDS与embstr、raw 深入理解

    对于上一篇文章,我又自己总结归纳并补充了一下,有了第二篇. 概览 <<左移 开始之前,我们先准备点东西:位运算 i<<n 总结为 i*2^n 所以 1<<5 = 2 ...

  10. centos7环境搭建一台mysql服务器启动多个端口

    在一台服务器上启动多个mysql实例,分别用不同的端口号,因centos7版本安装mysql5.7后不存在mysqld_multi .mysqld_safe等命令,做踩坑总结 Mysql多实例实现的3 ...