http://acm.hdu.edu.cn/showproblem.php?pid=3836

Time Limit: 12000/4000 MS (Java/Others)    Memory Limit: 104857/104857 K (Java/Others)
Total Submission(s): 4802    Accepted Submission(s): 1725

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.

 
Source
 
Recommend
xubiao   |   We have carefully selected several similar problems for you:  3835 3828 3834 3830 3829 
 
 
题意:求最少连几条边可以使整个图成为强连通图
可以先将图缩点,然后统计新图中入读==0,和出度==0 的点的个数,因为使加边最少,
所以应该是先给出度==0的点连一条向入读==0的点得边,然后再加上多余的(入读==0||出度==0)的点
ans=max(入读==0的点数,出读==0的点数)
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int N(+);
const int M(+);
int n,m; int head[N],sumedge;
struct Edge
{
int v,next;
Edge(int v=,int next=):v(v),next(next){}
}edge[M];
inline void ins(int u,int v)
{
edge[++sumedge]=Edge(v,head[u]);
head[u]=sumedge;
} int tim,dfn[N],low[N];
int top,Stack[N],instack[N];
int sumcol,col[N];
void DFS(int now)
{
dfn[now]=low[now]=++tim;
Stack[++top]=now; instack[now]=;
for(int i=head[now];i;i=edge[i].next)
{
int v=edge[i].v;
if(!dfn[v]) DFS(v),low[now]=min(low[now],low[v]);
else if(instack[v]) low[now]=min(low[now],dfn[v]);
}
if(dfn[now]==low[now])
{
col[now]=++sumcol;
for(;Stack[top]!=now;top--)
{
col[Stack[top]]=sumcol;
instack[Stack[top]]=;
}
instack[now]=; top--;
}
} int ans,ans1,ans2,rd[N],cd[N];
inline void init()
{
top=ans=ans1=ans2=tim=sumcol=sumedge=;
memset(rd,,sizeof(rd));
memset(cd,,sizeof(cd));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(head,,sizeof(head));
memset(Stack,,sizeof(Stack));
memset(instack,,sizeof(instack));
} int main()
{
for(;~scanf("%d%d",&n,&m);init())
{
for(int u,v,i=;i<=m;i++)
scanf("%d%d",&u,&v),ins(u,v);
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i);
for(int u=;u<=n;u++)
for(int i=head[u];i;i=edge[i].next)
{
int v=edge[i].v;
if(col[u]==col[v]) continue;
rd[col[v]]++; cd[col[u]]++;
}
for(int i=;i<=sumcol;i++)
{
if(!cd[i]) ans1++;
if(!rd[i]) ans2++;
}
ans=max(ans1,ans2);
if(sumcol==) ans=;
printf("%d\n",ans);
}
return ;
}

HUD——T 3836 Equivalent Sets的更多相关文章

  1. hdu 3836 Equivalent Sets

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

  2. [tarjan] hdu 3836 Equivalent Sets

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

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

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

  4. hdu 3836 Equivalent Sets trajan缩点

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

  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

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

  7. 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, ...

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

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

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

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

随机推荐

  1. js循环匹配组合成新对象或js循环组合新数据

    var Arry=[ {name: "vehicleTravelLicenseCopyBack", id: "a1"}, {name: "vehicl ...

  2. ip代理池的爬虫编写、验证和维护

    打算法比赛有点累,比赛之余写点小项目来提升一下工程能力.顺便陶冶一下情操 本来是想买一个服务器写个博客或者是弄个什么FQ的东西 最后刷知乎看到有一个很有意思的项目,就是维护一个「高可用低延迟的高匿IP ...

  3. [USACO14FEB]路障Roadblock

    题目:洛谷P2176. 题目大意:有n个点m条无向边,一个人要从1走到n,他会走最短路.现在可以让一条边的长度翻倍,求翻倍后这个人要多走多少距离. 解题思路:首先可以知道,翻倍肯定是在最短路上的某条边 ...

  4. Python 读写文件 小应用:生成随机的测验试卷文件

    去年学习了python的读写文件部分,了解了python读写的常用模块os.shelve,今天准备把课后作业试着自己做一下 目标:1)生成35份试卷.每个试卷有50道选择题 2)为了防止有学生作弊,需 ...

  5. PHP经常使用功能

    1)字符串 主要方法有:strops().substr().str_split().explode()等.很多其它方法查看PHP官方手冊. <?php /** * 字符串的方法:strpos() ...

  6. javascript模拟类的最佳实践

    1:怎样模拟一个类 在sencha touch2 系列里面定义一个类和new出这个类的对象 Ext.define( "Animal", { config: { name: null ...

  7. android CoordinatorLayout使用

    一.CoordinatorLayout有什么作用 CoordinatorLayout作为“super-powered FrameLayout”基本实现两个功能: 1.作为顶层布局 2.调度协调子布局 ...

  8. 如何更改jar包源码

    首先将你要更改的源码文件在eclipse中编译成.class文件 再找到你需要更改的.jar包 在桌面右键新建个文件夹把你要改的.jar包ctrl+c和ctrl+v 准备好一个压缩工具(这里推荐234 ...

  9. C#一些延时函数

    sleep延时方法: System.Threading.Thread.Sleep(); //毫秒 在C#窗口程序中,如果在主线程里调用Sleep,在Sleep完成之前, 界面呈现出假死状态,不能响应任 ...

  10. python 批量下载文件

    file.txt 的内容为: http://183.xxx.xxx.54:188/my/qqq.ico::qq.exe::0::http://183.xxx.xxx.54:186/my/ddnf.ic ...