hdu 3836 Equivalent Sets(强连通分量--加边)

时间:2022-03-04 23:50:33

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 &lpar;强连通分量&plus;DAG&rpar;

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

  2. hdu - 3836 Equivalent Sets&lpar;强连通&rpar;

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

  3. &lbrack;tarjan&rsqb; 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&plus;缩点)

    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&amp&semi;&amp&semi;缩点】【求最少加多少条边使图强连通】

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

随机推荐

  1. (bzoj2330)糖果

    2330: [SCOI2011]糖果 Time Limit: 10 Sec  Memory Limit: 128 MB Description 幼儿园里有N个小朋友,lxhgww老师现在想要给这些小朋 ...

  2. Java动态程序设计:反射介绍

    使用运行的类的信息使你的程序设计更加灵活 反射授予了你的代码访问装载进JVM内的Java类的内部信息的权限,并且允许你编写在程序执行期间与所选择的类的一同工作的代码,而不是在源代码中.这种机制使得反射 ...

  3. effective OC2&period;0 52阅读笔记(三 接口与API设计)

    第三章:接口与API设计 15 用前缀避免命名空间冲突 总结:避免重名符号错误的唯一办法是变相实现命名空间.为所有符号都加上命名前缀.类和分类都应加三字前缀.注意类实现文件中的纯C函数及全局变量,是算 ...

  4. html中css三种常见的样式选择器 zz

    1:标签选择器 标签选择器,是所有带有某种标签的都生效.这里以p为例,也就是所有的带有p标记的都会这样的样式 <html><head><styletype="t ...

  5. saltstack实战3--配置管理之grains

    grains是什么 grains是minion服务启动后,采集的客户端的一些基本信息,硬件信息,软件信息,网络信息,软件版本等.你可以在minion上自定义一些grains信息. 它是静态的信息,mi ...

  6. Redis&lowbar;php 学习

    转载内容: PhpRedis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系很有用;以下是redis官方提供的命令使用技巧: Redis::__construct构造函数 1 ...

  7. 转载:MyEclipse中防止代码格式化时出现换行的情况的设置

     转载出处:http://www.cnblogs.com/yjhrem/articles/2310013.html 编辑完成代码,用MyEclipse的代码格式化后,本来不长的代码也被自动转成了多行. ...

  8. log4net 使用与配置 每天一份log文件

    1.下载 或 在nuget安装 log4net 2. web.config (app.config) <configuration> <configSections> < ...

  9. asp&period;net下用js实现弹出子窗口选定值并返回

    对应上一篇博客代码: 父页面: <head runat="server"> <meta http-equiv="X-UA-Compatible&quot ...

  10. A ResourcePool could not acquire a resource from its primary factory or source

    出处:http://aaron81939097.iteye.com/blog/1144642 原配置: <bean id="dataSource" class="c ...