主题链接:

http://acm.hdu.edu.cn/showproblem.php?

pid=3836

Equivalent Sets

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

Total Submission(s): 2890    Accepted Submission(s): 1006

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
 

Statistic | Submit | 

problemid=3836" style="color:rgb(26,92,200); text-decoration:none">Discuss | Note
题目意思:

求至少须要增添多少条边,使得该图为强连通图。

解题思路:

tarjan求强连通。然后统计入度为0和出度为0的强连通分量个数,两者的最大值即为答案。

代码:

//#include<CSpreadSheet.h>

#include<iostream>
#include<cmath>
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<string>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#include<bitset>
#include<cmath>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define LL long long
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#define M 1000000007
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; #define Maxn 21000
int low[Maxn],dfn[Maxn],dindex,n;
int sta[Maxn],belong[Maxn],bcnt,ss;
bool iss[Maxn];
int de1[Maxn],de2[Maxn];
vector<vector<int> >myv; void tarjan(int cur)
{
//printf(":%d\n",cur);
//system("pause");
int ne; dfn[cur]=low[cur]=++dindex;
iss[cur]=true;
sta[++ss]=cur; for(int i=0;i<myv[cur].size();i++)
{
ne=myv[cur][i];
if(!dfn[ne])
{
tarjan(ne);
low[cur]=min(low[cur],low[ne]);
}
else if(iss[ne]&&dfn[ne]<low[cur])
low[cur]=dfn[ne];
} if(dfn[cur]==low[cur])
{
bcnt++;
do
{
ne=sta[ss--];
iss[ne]=false;
belong[ne]=bcnt;
}while(ne!=cur);
}
} void solve()
{
int i;
ss=bcnt=dindex=0;
memset(dfn,0,sizeof(dfn));
memset(iss,false,sizeof(iss));
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(~scanf("%d",&n))
{
myv.clear();
myv.resize(n+1);
int m;
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
myv[a].push_back(b);
}
solve(); if(bcnt==1)
{
printf("1\n0\n");
continue;
}
int ansa=0,ansb=0; memset(de1,0,sizeof(de1));
memset(de2,0,sizeof(de2)); for(int i=1;i<=n;i++)
{
for(int j=0;j<myv[i].size();j++)
{
int ne=myv[i][j];
if(belong[i]!=belong[ne])
{
de1[belong[ne]]++;//Èë¶È
de2[belong[i]]++; //³ö¶È
} }
}
for(int i=1;i<=bcnt;i++)
{
if(!de1[i])
ansa++;
if(!de2[i])
ansb++;
} ansb=max(ansb,ansa); printf("%d\n",ansb);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

[tarjan] hdu 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. hdu 3836 Equivalent Sets trajan缩点

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

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

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

  4. hdu——3836 Equivalent Sets

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

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

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

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

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

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

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

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

  9. HUD——T 3836 Equivalent Sets

    http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. 有趣 IOS 开展 - block 使用具体解释

    Block 它是iOS于4.0新的程序语法之后,于iOS SDK 4.0之后,block应用几乎无处不在. 在其他语言中也有类似的概念,称为闭包(closure),实例object C兄弟swift ...

  2. Linux在device is busy处理

    在Linux管理umount设备时,时常会遇到"device is busy", 假设umount一个文件系统碰到这样的情况.而且你并没有在所需卸载的文件夹下.那么非常可能实用户或 ...

  3. 在SurfaceView中自由书写和擦除

    /////////继承SurfaceView 的类 public class PaintView extends SurfaceView implements Runnable,SurfaceHold ...

  4. 恢复SQLSERVER被误删除的数据

    原文:恢复SQLSERVER被误删除的数据 恢复SQLSERVER被误删除的数据 曾经想实现Log Explorer for SQL Server的功能,利用ldf里面的日志来还原误删除的数据 这里有 ...

  5. STM32电源管理

     (1)3时钟模式 ①睡眠模式②停止模式③待机模式 1.睡眠模式:Cortex-M3内核(理解为CPU)停止工作,CPU供电1.8V有着,周边任何执行.执行 2.停机模式:全部时钟都停止,CPU电 ...

  6. web富客户端应用下,前端架构问题。

    前言: 以前的工作大部分都是,前端做页面 稍微写几个js效果就算是 有复杂的效果 也没有涉及到 需要去调用后端数据的层面.总体来说,以前的页面逻辑会相对简单后端会做更多的事. 而现在,这些任务都抛给前 ...

  7. react学习笔记2--练习Demos

    准备工作 # 0.react核心库 <script src="../build/react.js"></script> # 将JSX 语法转为 JavaSc ...

  8. 最少换乘(Dijkstra)

    Description 欧洲某城是一个著名的旅游胜地,每年都有成千上万的人前来观光旅行.Dr. Kong决定利用暑假好好游览一番.. 年轻人旅游不怕辛苦,不怕劳累,只要费用低就行.但Dr. Kong年 ...

  9. decimal system 2016

    Problem Description As we know , we always use the decimal system in our common life, even using the ...

  10. jsmart 前结合案例

    前绑定jsmart这是一个不错的选择.之前通过经常使用的项目中的.最近涉及的领域的后端部.jsmart有些使用相对较少,主要是因为他想引用文件,我写的模板,在一个简单的项目,直接使用js界,很复杂的前 ...