[tarjan] hdu 3836 Equivalent Sets
主题链接:
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
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.
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
4 0
3 2
1 2
1 3
4
2HintCase 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
pid=3834" target="_blank" style="color:rgb(26,92,200); text-decoration:none">3834
3830pid=3833" target="_blank" style="color:rgb(26,92,200); text-decoration:none">3833
Statistic | Submit | problemid=3836" style="color:rgb(26,92,200); text-decoration:none">Discuss
题目意思:
求至少须要增添多少条边,使得该图为强连通图。
解题思路:
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的更多相关文章
- hdu 3836 Equivalent Sets
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu 3836 Equivalent Sets(强连通分量--加边)
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu——3836 Equivalent Sets
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- 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, ...
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- HDU - 3836 Equivalent Sets (强连通分量+DAG)
题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...
- hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- HUD——T 3836 Equivalent Sets
http://acm.hdu.edu.cn/showproblem.php?pid=3836 Time Limit: 12000/4000 MS (Java/Others) Memory Lim ...
随机推荐
- Windows 怎么知道我已连线到网际网络? 原来当中大有文章!
原文 Windows 怎么知道我已连线到网际网络? 原来当中大有文章! 标题这个问题好像很简单, 但原来深入研究起来还真的不是”因为我能看到网页嘛”这样简单的答案. 研究这个课题是由于公司内部的防火墙 ...
- [SignalR]Groups操作——后续
原文:[SignalR]Groups操作——后续 继续http://www.cnblogs.com/YanZhiwei/p/3359175.html之前,那怎么实现发送的消息推送到隶属于这个组的用户呢 ...
- [SignalR]注册路由
原文:[SignalR]注册路由 1.注册SignalR路由 在Asp.Net中,若是SignalR 1.*版本,在Global.asax文件中定义如下: 在Asp.Net中,若是SignalR 2. ...
- WIN8 、WIN7 下IIS7.5、IIS8 的rewrite 伪静态功能设置方法
原文 WIN8 .WIN7 下IIS7.5.IIS8 的rewrite 伪静态功能设置方法 win7和win8系统都自带有iis的功能.关于IIS的安装,上一篇已经讲述,这里就不重复了. 下面说下在w ...
- Online网站集
http://tool.oschina.net/apidocs/ 在线工具(IT技术工具)
- JAXB 操作XML 与 Object
Java Architecture for XML Binding) 是一个业界的标准,是一项能够依据XML Schema产生Java类的技术.是一种xml与object映射绑定技术标准. JDK5下 ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- from声明
在整个应用程序,只有三行声明.这是最短单WIN32应用,但它的功能是非常有限,简单地显示一个消息框,示出来,其他什么事情也没有做.以下就来分析这三行语句了.别小看这三行语句.其实是隐藏着非常多知识点在 ...
- jquery选择器基础知识
$("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("di ...
- POJ1201-Intervals(差动限制)
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20786 Accepted: 7866 Descri ...