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. Hive计算的临时文件清理

    hive 的存储路径的 .hive-staging_hive_yyyy-MM-dd_HH-mm-ss_SSS_xxxx-x 文件可以清理掉吗 https://blog.csdn.net/sparkex ...

  2. android apktool 基本的安装与使用

    apktool: 1. 安装 http://ibotpeaches.github.io/Apktool/install/ 2. 基本使用 http://ibotpeaches.github.io/Ap ...

  3. phpcms 后台验证码不显示date() expects parameter 2 to be long

    方法一: 主要在于是否开启gd库 查看办法 找到php.ini文件 搜索extension=php_gd2.dll这段代码 然后把前面的;符号去掉即可. 方法二: 关键一点是获取验证码的图片与全局变量 ...

  4. C++ Primer笔记2_四种类型转换_异常机制

    1.类型转换 命名的强制类型转换: 有static_cast.dynamic_cast.const_cast.reinterpret_cast static_cast: 编译器隐式运行的不论什么类型转 ...

  5. JMeter 十六:加密处理

    假设采用MD5进行加密 JMeter 内置的没有MD5加密方法.网上有说采用__MD5函数的,但是我在 Jmeter 2.13 以及 Jmeter 3.2 版本上都没有找到这个函数,官方文档也没有看到 ...

  6. Core Data NSAttribute Type 数据类型

    一:使用Core Data 的可用数据类型 NSAttributeType Defines the possible types of NSAttributeType properties. Thes ...

  7. 【转】JAVA – 虚函数、抽象函数、抽象类、接口

    1. Java虚函数 虚函数的存在是为了多态. C++中普通成员函数加上virtual关键字就成为虚函数 Java中其实没有虚函数的概念,它的普通函数就相当于C++的虚函数,动态绑定是Java的默认行 ...

  8. Android studio及eclipse中的junit单元測试

    转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/51179106 前一段时间有人问我单元測试的相关内容,我稍作总结做日志例如以下: 由于我接 ...

  9. android4.4系统永不锁屏

    android4.4\frameworks\base\packages\Keyguard\src\com\android\keyguard\KeyguardViewMediator.java /** ...

  10. linux Java 手动GC 手动回收垃圾

    logs_paths[0]="xxxx_tomcat8_9001"; logs_paths[1]="xxxx_tomcat8_9002"; for logs_p ...