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. LNMP安装部署开源IP管理工具phpipam

    1.数据库 mariadb 安装 //依赖安装 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* \ cpp curl ...

  2. ATP自造8Gb内存颗粒供DDR3使用

    随着整个行业已经全面转向DDR4内存,不少厂商都陆续停产了DDR3,并准备好了迎接DDR5,但对于很多特殊用户,尤其是网络和嵌入式领域,仍然对DDR3有着强劲且持续的需求. 工业内存存储厂商ATP E ...

  3. 修复linux的grub2引导(单独/boot,lvm-root)

    root@ubuntu:/home/ubuntu# pwd /home/ubuntu root@ubuntu:/home/ubuntu# lsblk NAME                  MAJ ...

  4. 【Codeforces Round #421 (Div. 2) B】Mister B and Angle in Polygon

    [题目链接]:http://codeforces.com/contest/820/problem/B [题意] 给你一个正n边形; 然后让你在这正n边行中选3个点,组成一个角; 找出角的大小和所给的角 ...

  5. 以Append方式打开文件,设置偏移量无效

    #include<stdio.h> int main() { FILE * fd = fopen("btoo1.c", "ab+"); fpos_t ...

  6. 洛谷 P1407 工资

    P1407 工资 题目描述 有一家世界级大企业,他们经过调查,发现了一个奇特的现象,竟然在自己的公司里,有超过一半的雇员,他们的工资完全相同! 公布了这项调查结果后,众多老板对于这一现象很感兴趣,他们 ...

  7. List和iterator的区别

    (1)在用Query方法查询的时候,通过HQL语句来得到Query对象,并对Query对象进行操作,首先是用list方法获取到Query的List集合并输出 public void listQuery ...

  8. [B cannot be cast to java.lang.String

    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.inv ...

  9. hdu1501 Zipper--DFS

    原题链接:pid=1501">http://acm.hdu.edu.cn/showproblem.php?pid=1501 一:原题内容 Problem Description Giv ...

  10. FPGA设计中的电源管理(转载)

    过去,FPGA设计者主要关心时序和面积使用率问题.但随着FPGA不断取代ASSP和ASIC器件,设计者们现正期望能够开发低功耗设计,在设计流程早期就能对功耗进行正确估算,以及管理和对与FPGA相关的各 ...