Equivalent Sets

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)

Total Submission(s): 2798    Accepted Submission(s): 962

Problem Description
To prove two sets A and B are equivalent, we can first prove A is a subset of B, and then prove B is a subset of A, so finally we got that these two sets are equivalent.

You are to prove N sets are equivalent, using the method above: in each step you can prove a set X is a subset of another set Y, and there are also some sets that are already proven to be subsets of some other sets.

Now you want to know the minimum steps needed to get the problem proved.
 
Input
The input file contains multiple test cases, in each case, the first line contains two integers N <= 20000 and M <= 50000.

Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
 
Output
For each case, output a single integer: the minimum steps needed.
 
Sample Input
4 0
3 2
1 2
1 3
 
Sample Output
4
2
Hint
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
 

求将原图的强连通分量缩点,得到有向无环图,求至少加多少条边能够使这个图变成一幅强连通图,max(入度为0的点,出度为0的点)即为答案。

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"algorithm"
using namespace std;
#define N 20005
#define M 50005
#define min(a,b) (a<b?a:b)
const int inf=1000000;
struct node
{
int u,v,next;
}e[M];
int t,bcnt,index,stop,ans;
int head[N],dfn[N],low[N],stap[N],mark[N],be[N];
int indeg[N],out[N];
void add(int u,int v)
{
e[t].u=u;
e[t].v=v;
e[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
int i,v;
dfn[u]=low[u]=++index;
stap[++stop]=u;
mark[u]=1;
for(i=head[u];i!=-1;i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(mark[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
bcnt++;
do
{
v=stap[stop--];
mark[v]=0;
be[v]=bcnt;
}
while(u!=v);
ans++;
}
}
void solve(int n)
{
int i;
memset(dfn,0,sizeof(dfn));
index=stop=bcnt=0;
for(i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
}
void work(int n)
{
int i,j,u,v,t1,t2;
memset(indeg,0,sizeof(indeg));
memset(out,0,sizeof(out));
for(i=1;i<=n;i++)
{
u=be[i];
for(j=head[i];j!=-1;j=e[j].next)
{
v=be[e[j].v];
if(u!=v)
{
indeg[v]++;
out[u]++;
}
}
}
t1=t2=0;
for(i=1;i<=bcnt;i++)
{
if(indeg[i]==0)
t1++;
if(out[i]==0)
t2++;
}
printf("%d\n",t1>t2?t1:t2);
}
int main()
{
int n,m,u,v;
while(scanf("%d%d",&n,&m)!=-1)
{
t=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d",&u,&v);
add(u,v);
}
ans=0;
solve(n);
// printf("%d\n",ans);
if(ans==1)
printf("0\n");
else
work(n);
}
return 0;
}

hdu 3836 Equivalent Sets(强连通分量--加边)的更多相关文章

  1. HDU - 3836 Equivalent Sets (强连通分量+DAG)

    题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...

  2. hdu - 3836 Equivalent Sets(强连通)

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...

  3. [tarjan] hdu 3836 Equivalent Sets

    主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...

  4. hdu 3836 Equivalent Sets

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...

  5. hdu——3836 Equivalent Sets

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  6. hdu 3836 Equivalent Sets(tarjan+缩点)

    Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...

  7. hdu 3836 Equivalent Sets trajan缩点

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

  8. hdu 3836 tarjain 求强连通分量个数

    // 给你一个有向图,问你最少加几条边能使得该图强连通 #include <iostream> #include <cstdio> #include <cstring&g ...

  9. hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】

    Equivalent Sets Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Other ...

随机推荐

  1. C# Json 序列化和反序列化 工具类 Newtonsoft.Json.dll

    引用: Newtonsoft.Json.dll // 引用: using Newtonsoft.Json; using Newtonsoft.Json.Converters; // 定义 实体测试类 ...

  2. Axure 简单原型设计

    简介 Axure RP是一个专业的快速原型设计工具.Axure(发音:Ack-sure),代表美国Axure公司:RP则是Rapid Prototyping(快速原型)的缩写. Axure RP是美国 ...

  3. 向项目中添加dtd文件实现代码提示

    我们在编辑代码的过程中,有代码提示的编辑工具总是受到青睐. 本文将讲解怎么在eclipse中加入dtd文件实现代码提示.myeclipse同理 选择window,选择perferences,然后拉到下 ...

  4. python 带颜色样式打印到终端

    #!/usr/bin/python # -*- coding: utf-8 -*- """ Created on Tue Aug 8 17:01:54 2017 @aut ...

  5. 《Linux内核设计与实现》笔记-1-linux内核简单介绍

    一.Linux内核相对于传统的UNIX内核的比較: (1):Linux支持动态内核模块. 虽然Linux内核也是总体式结构,但是同意在须要的时候动态哦卸除(rmmod xxx)和载入内核模块(insm ...

  6. 百度地图JS--2

    转载请注明出处 本文为原创 作者:injuer 严禁用于商业用途,仅学习交流 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transiti ...

  7. [Mac A]为什么国外程序员爱用 Mac?

    from http://www.vpsee.com/2009/06/why-programmers-love-mac/ Mac 在国外很受欢迎,尤其是在 设计/web开发/IT 人员圈子里.普通用户喜 ...

  8. 详细解说 STL 排序(Sort)(转)

    作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...

  9. 05-spring-bean注入

    spring中只有两大核心技术: 控制反转(IOC)&依赖注入(DI),AOP(面向切面编程) 依赖注入:指利用配置文件的关系,来决定类之间的引用关系,以及数据的设置操作. 构造方法注入 默认 ...

  10. 特殊文件权限(setuid、setgid 和 Sticky 位)

    可执行文件和公共目录可以使用三种特殊类型的权限:setuid.setgid 和 sticky 位.设置这些权限之后,运行可执行文件的任何用户都应采用该可执行文件属主(或组)的 ID. setuid 权 ...