【强连通分量】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个节点(每个节点对应 ...
随机推荐
- apache jmeter下载与安装
JMeter是Apache软件基金会的产品,用于对静态的和动态的资源性能进行测试.jmeter可以运行在多个平台上,如Windows和Linux,本文讲的是在Windows安装jmeter. 工具/原 ...
- 用Python爬取分析【某东618】畅销商品销量数据,带你看看大家都喜欢买什么!
618购物节,辰哥准备分析一波购物节大家都喜欢买什么?本文以某东为例,Python爬取618活动的畅销商品数据,并进行数据清洗,最后以可视化的方式从不同角度去了解畅销商品中,名列前茅的商品是哪些?销售 ...
- 【题解】入阵曲 luogu3941 前缀和 压维
丹青千秋酿,一醉解愁肠. 无悔少年枉,只愿壮志狂 题目 题目描述 小 F 很喜欢数学,但是到了高中以后数学总是考不好. 有一天,他在数学课上发起了呆:他想起了过去的一年.一年前,当他初识算法竞赛的 时 ...
- 【单调栈】【前缀和】【二分查找】8.28题解-long
long 题目描述 AP神牛准备给自己盖一座很华丽的宫殿.于是,他看中了一块N*M的矩形空地.空地中每个格子都有自己的海拔高度.AP想让他的宫殿的平均海拔在海平面之上(假设海平面的高度是0,平均数都会 ...
- SCP,SSH应用
- 安卓控件RecycleView的简单使用
RecycleView的使用 目录 RecycleView的使用 技术概述 技术详述 遇到问题和解决 总结 参考文献 技术概述 RecycleView是谷歌官方对ListView的改进(并不是替代), ...
- eclipse语言怎么设置为中文
2021-05-30 方法:1.查找语言包下载网址,并复制:2.打开eclipse,点击"help"-"Install New Software"-" ...
- Java+Selenium 上传文件,点击选择“浏览文件”按钮,报错invalid argument
Java+Selenium 上传文件,点击选择"浏览文件"按钮,报错invalid argument 解决代码: Actions action=new Actions(driver ...
- 对象池技术和通用实现GenericObjectPool
对象池技术其实蛮常见的,比如线程池.数据库连接池 他们的特点是:对象创建代价较高.比较消耗资源.比较耗时: 比如 mysql数据库连接建立就要先建立 tcp三次握手.发送用户名/密码.进行身份校验.权 ...
- promise的基本使用
// 什么情况下适用promise? // 一般情况下是有异步请求操作时,使用promise对这个异步操作进行封装 // new ->构造函数(1.保存了一些状态信息 2.执行传入的函数) // ...