【强连通分量】Proving Equivalences
【题目链接】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的更多相关文章
- UVa 12167 & HDU 2767 强连通分量 Proving Equivalences
题意:给出一个有向图,问最少添加几条有向边使得原图强连通. 解法:求出SCC后缩点,统计一下出度为0的点和入度为0的点,二者取最大值就是答案. 还有个特殊情况就是本身就是强连通的话,答案就是0. #i ...
- Proving Equivalences(缩点+有环图变强连通分量)【tarjian算法】
Proving Equivalences 题目链接(点击) 参考博客(点击) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768 ...
- UVALive Proving Equivalences (强连通分量,常规)
题意: 给一个有向图,问添加几条边可以使其强连通. 思路: tarjan算法求强连通分量,然后缩点求各个强连通分量的出入度,答案是max(入度为0的缩点个数,出度为0的缩点个数). #include ...
- UVALive-4287 Proving Equivalences 有向图的强连通分量+缩点
题意:有n个命题,已知其中的m个推导,要证明n个命题全部等价(等价具有传递性),最少还需要做出几次推导. 思路:由已知的推导可以建一张无向图,则问题变成了最少需要增加几条边能使图变成强连通图.找出所有 ...
- UVALive - 4287 - Proving Equivalences(强连通分量)
Problem UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...
- Proving Equivalences UVALive - 4287(强连通分量 水题)
就是统计入度为0 的点 和 出度为0 的点 输出 大的那一个,, 若图中只有一个强连通分量 则输出0即可 和https://www.cnblogs.com/WTSRUVF/p/9301096.htm ...
- UVALive-4287 Proving Equivalences (有向图的强连通分量)
题目大意:有n个命题,已知其中的m个推导,要证明n个命题全部等价(等价具有传递性),最少还需要做出几次推导. 题目分析:由已知的推导可以建一张无向图,则问题变成了最少需要增加几条边能使图变成强连通图. ...
- UVALIVE 4287 Proving Equivalences (强连通分量+缩点)
题意:给定一个图,问至少加入多少条边能够使这个图强连通. 思路:首先求出这个图的强连通分量.然后把每个强连通分量缩成一个点.那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的 ...
- HDU2767Proving Equivalences[强连通分量 缩点]
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HD2767Proving Equivalences(有向图强连通分量+缩点)
题目链接 题意:有n个节点的图,现在给出了m个边,问最小加多少边是的图是强连通的 分析:首先找到强连通分量,然后把每一个强连通分量缩成一个点,然后就得到了一个DAG.接下来,设有a个节点(每个节点对应 ...
随机推荐
- 虹软人脸识别SDK接入Milvus实现海量人脸快速检索
一.背景 人脸识别是近年来最热门的计算机视觉领域的应用之一,而且现在已经出现了非常多的人脸识别算法,如:DeepID.FaceNet.DeepFace等等.人脸识别被广泛应用于景区.客运.酒店.办公室 ...
- (5)使用自定Web根目录
调整 Web 站点 http://server0.example.com 的网页目录,要求如下: 1) 新建目录 /webroot,作为此站点新的网页文件根目录 # mkdir /webroot # ...
- laya fgui 超简单的UI框架
FairyGUI 超简单的UI框架 Laya使用fgui的超简单UI框架 使用场景:用于使用fgui进行layaUI开发的程序人员 整个框架分为3个模块,共有4个类: FGUIManager :FGU ...
- Java核心API-日期时间
java.util.Date Date类用来表示时间点. 时间是用距离一个固定时间点的毫秒数表示的,这个时间点就是纪元. UTC时间是为表示这个纪元的科学标准时间,从1970年1月1日0时开始.另一种 ...
- Unity il2cpp加密
防市面上百分之九十的破解者,下面是Mono改为il2cpp的面板 点击:File > BuildSettings > PlayerSettings > OtherSettings & ...
- CentOS7日志管理工具 journalctl
1.简介 日志管理工具journalctl是centos7上专有的日志管理工具,该工具是从message这个文件里读取信息. Systemd统一管理所有Unit的启动日志.带来的好处就是,可以只用jo ...
- Java基础篇(JVM)——类加载机制
这是Java基础篇(JVM)的第二篇文章,紧接着上一篇字节码详解,这篇我们来详解Java的类加载机制,也就是如何把字节码代表的类信息加载进入内存中. 我们知道,不管是根据类新建对象,还是直接使用类变量 ...
- Gitlab 定时备份
要求 1.为了能够备份和恢复,请确保你的系统上安装了Rsync #Debian/Ubauntu sudo apt-get install rsync # RHEL/Centos sudo yum in ...
- 11、nginx+tomcat+redis_session共享
11.1.前言: 1.多个tomcat要一起协同工作可以考虑的方案如下: (1)使用tomcat自带的cluster方式,多个tomcat间自动实时复制session信息,配置起来很简单.但这个方案的 ...
- AcWing 90. 64位整数乘法
求a*b%p的值. 0<a,b,p<1e18; 原题链接 #include<bits/stdc++.h> #define ull unsigned long long usin ...