【强连通分量】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个节点(每个节点对应 ...
随机推荐
- mysql 高级和 索引优化,目的:查的好,查的快,性能好
1-事物隔离级别: 更新丢失, 并发情况下,对同一字段进行更新,就会出现更新丢失,采用乐观锁,比较版本号或时间戳可解决 读未提交 解决了更新丢失但是会引起脏读, 二个session.sessionA中 ...
- Netty 框架学习 —— ChannelHandler 与 ChannelPipeline
ChannelHandler 1. Channel 生命周期 Channel 的生命周期状态如下: 状态 描述 ChannelUnregistered Channel 已经被创建,但还未注册到 Eve ...
- selenium常用方法集合
一.selenium定位元素的8种方法: 1.find_element_by_id() 2.find_element_by_name() 3.find_element_by_css() 4.find_ ...
- 【NX二次开发】获取边的类型 UF_MODL_ask_edge_type
源码 extern DllExport void ufsta(char *param, int *returnCode, int rlen) { UF_initialize(); int edge_t ...
- Java 设置Word文本框中的文字旋转方向
Word文档中可添加文本框,并设置文本框为横向文本排列或是纵向文本排列,或者设置文本框中的文字旋转方向等.通过Java程序代码,也可以实现以上文本框的操作.下面以Java代码示例展示具体的实现步骤.另 ...
- .NET Worker Service 部署到 Linux 作为 Systemd Service 运行
上一篇文章我们了解了如何将.NET Worker Service 作为 Windows 服务运行,今天我接着介绍一下如何将 Worker Service 部署到 Linux 上,并作为 Systemd ...
- K8s 部署 Prometheus + Grafana
一.简介 1. Prometheus 一款开源的监控&报警&时间序列数据库的组合,起始是由 SoundCloud 公司开发的 基本原理是通过 HTTP 协议周期性抓取被监控组件的状态, ...
- Golang使用proto3协议导致零值字段不显示
Golang使用proto3协议导致零值字段不显示 问题描述 proto协议生成的结构体如果使用直接转成json会导致零值字段不显示,这样的json是有毛病的,可以使用如下方法解决 示例Demo pa ...
- 如何在Linux下部署Samba服务?
Samba简介 Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成.SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件 ...
- 快速了解ARP
目录 前言 一.MAC 1.MAC地址三种帧 二.ARP 1.五种ARP 三.ARP老化 四.什么时候会发送免费ARP 五.代理ARP 六.ARP欺骗 总结 前言 分别介绍MAC地址和五种ARP报文 ...