题意是告诉你有n个命题,m条递推关系,表示某个命题可以推出另外一个命题。

现在问你至少在增加多少个递推关系可以保证所有命题两两互推。

命题为点,关系为有向边,题目转化成为至少增加多少条有向边使得整个图强连通。

首先对于有向图,求出所有的联通分量,并且将所有的联通分量缩成一个点,最终得出一个无环图。

在新图里,设有A个出度为0的点,B个入度为0的点,那么我们只要保证增加max(A,B)条边就可以保证整个图是强连通的。

这个理解不是很难,只要把所有出度为0的点引出一条边,保证每个入度为0的点引入一条边就可以了。

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 200200
using namespace std; int first[maxn],to[maxn],next[maxn],edge;
int d[maxn],low[maxn],belong[maxn],stack[maxn],top;
int ind[maxn],outd[maxn],U[maxn],V[maxn];
int n,m,T,scc,ans1,ans2; void _init()
{
ans1=ans2=scc=top=,edge=-;
for (int i=; i<=n; i++)
{
first[i]=-;
d[i]=low[i]=belong[i]=;
}
} void addedge(int uu,int vv)
{
edge++;
to[edge]=vv,next[edge]=first[uu],first[uu]=edge;
} void dfs(int cur,int fa)
{
d[cur]=low[cur]=d[fa]+;
stack[++top]=cur;
for (int i=first[cur]; i!=-; i=next[i])
{
if (belong[to[i]]) continue;
if (!d[to[i]]) dfs(to[i],cur);
low[cur]=min(low[cur],low[to[i]]);
}
if (low[cur]>=d[cur])
for (scc++,ind[scc]=outd[scc]=;stack[top+]!=cur;) belong[stack[top--]]=scc;
} int main()
{
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
_init();
for (int i=; i<=m; i++)
{
scanf("%d%d",&U[i],&V[i]);
addedge(U[i],V[i]);
}
for (int i=; i<=n; i++)
if (!d[i]) dfs(i,);
for (int i=; i<=m; i++)
{
if (belong[U[i]]==belong[V[i]]) continue;
outd[belong[U[i]]]++;
ind[belong[V[i]]]++;
}
for (int i=; i<=scc; i++)
{
if (ind[i]==) ans1++;
if (outd[i]==) ans2++;
}
if (scc==) ans1=ans2=;
printf("%d\n",max(ans1,ans2));
}
return ;
}

UVAlive4287_Proving Equivalences的更多相关文章

  1. HDU2767Proving Equivalences[强连通分量 缩点]

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

  2. hdu 2767 Proving Equivalences

    Proving Equivalences 题意:输入一个有向图(强连通图就是定义在有向图上的),有n(1 ≤ n ≤ 20000)个节点和m(0 ≤ m ≤ 50000)条有向边:问添加几条边可使图变 ...

  3. hdoj 2767 Proving Equivalences【求scc&&缩点】【求最少添加多少条边使这个图成为一个scc】

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

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

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

  5. UVALive - 4287 - Proving Equivalences(强连通分量)

    Problem   UVALive - 4287 - Proving Equivalences Time Limit: 3000 mSec Problem Description Input Outp ...

  6. HDU 2767 Proving Equivalences(至少增加多少条边使得有向图变成强连通图)

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

  7. HDU 2767 Proving Equivalences (Tarjan)

    Proving Equivalences Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other ...

  8. UVA12167 Proving Equivalences

    UVA12167 Proving Equivalences 题意翻译 题目描述 在数学中,我们常常需要完成若干命题的等价性证明. 例如:有4个命题a,b,c,d,要证明他们是等价的,我们需要证明a&l ...

  9. HDU 2767 Proving Equivalences (强联通)

    pid=2767">http://acm.hdu.edu.cn/showproblem.php?pid=2767 Proving Equivalences Time Limit: 40 ...

随机推荐

  1. Flutter - Finished with error: FormatException: Bad UTF-8 encoding 0xc3 (at offset 169)

    方案1: 更改项目的Encoding方式 File -> Settings -> Editor,  choose "File Encodings", change Pr ...

  2. 关于SDK-manager中我们需要下载哪些?

    废话少说,直接看图说话…… 图片取自博客文章——链接跳转:点击跳转

  3. 车架号VIN码识别,合格证,购车发票,房产证,车牌,驾驶证,行驶证,征信报告等等识别 从易鑫、大搜车、淘车网,看汽车金融发展新模式

    随着我国汽车保有量和产销量的持续增长,汽车技术的日趋成熟,以及互联网+对汽车行业的不断影响,汽车金融的市场规模逐步扩大,市场主体逐步丰富,汽车金融模式也在不断演进. 2016年左右,美国主要汽车厂商通 ...

  4. 内置函数——eval、exec、compile

    内置函数——eval.exec.compile eval() 将字符串类型的代码执行并返回结果 print(eval('1+2+3+4')) exec()将自字符串类型的代码执行 print(exec ...

  5. linux 开机报错,error grub_efi_find_mmap_size not find

    开机报错,差点以为要重装系统了 搜到了官方的重建引导的教程 修复了错误 https://wiki.manjaro.org/index.php/Restore_the_GRUB_Bootloader#F ...

  6. 利用workbench对linux/Ubuntu系统中的mysql数据库进行操作

    在上一篇文章中,我分享了在linux中如何安装mysql数据库,但是这只是安装了mysql的服务,并没有图形化管理界面,所以这样子操作起来并没有那么方便,那么现在我们就来实现如何利用在window中安 ...

  7. SQL知识点脑图(一张图总结SQL)

    sql语言的分类DDL:create drop alter DML:insert delete update DCL:rollback grant revoke commit 概要,主外键,视图,索引 ...

  8. 3分钟手把手带你搭建基于selenium的自动化框架

    1 .什么是seleniumSelenium 是一个基于浏览器的自动化工具,它提供了一种跨平台.跨浏览器的端到端的web自动化解决方案.Selenium主要包括三部分:Selenium IDE.Sel ...

  9. 深度学习中数据的augmentation

    为了提高模型的泛化能力,同时也为了增大数据集,我们往往需要对数据进行augmentation,在这篇博客中,将总结一下可以对数据进行的augmentation. 1.颜色数据增强,对图像亮度.饱和度. ...

  10. CF 1064B Equations of Mathematical Magic(思维规律)

    Description Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for. ...