hdu 3836 Equivalent Sets(tarjan+缩点)
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.
The input file contains multiple test cases, in each case, the first line contains two integers N <= and M <= .
Next M lines, each line contains two integers X, Y, means set X in a subset of set Y.
For each case, output a single integer: the minimum steps needed.
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
把tarjan复习了一遍
题目描述:将题目中的集合转换为顶点,A集合是B集合的子集,转换为一条有向边<A,B>,即题目给我们一个有向图,问最少需要添加多少条边使之成为强连通图。
解题思路:通过tarjan算法找出图中的所有强连通分支,并将每一个强连通分支缩成一个点(因为强连通分量本身已经满足两两互相可达)。
要使缩点后的图成为强连通图,每个顶点最少要有一个入度和一个出度,一条边又提供一个出度和一个入度。
所以可以通过统计没有入度的顶点数 noInDegree 和 没有出度的顶点数 noOutDegree。
所需要添加的边数就是noInDegree和noOutDegree中的最大值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
using namespace std;
#define N 20006
#define inf 1<<26
int n,m;
struct Node
{
int from;
int to;
int next;
}edge[N<<];
int tot;//表示边数,边的编号
int head[N];
int vis[N];
int tt;
int scc;
stack<int>s;
int dfn[N],low[N];
int col[N]; void init()//初始化
{
tt=;
tot=;
memset(head,-,sizeof(head));
memset(dfn,-,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(col,,sizeof(col));
} void add(int s,int u)//邻接矩阵函数
{
edge[tot].from=s;
edge[tot].to=u;
edge[tot].next=head[s];
head[s]=tot++;
} void tarjan(int u)//tarjan算法找出图中的所有强连通分支
{
dfn[u] = low[u]= ++tt;
vis[u]=;
s.push(u);
int cnt=;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].to;
if(dfn[v]==-)
{
// sum++;
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(vis[v]==)
low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int x;
scc++;
do{
x=s.top();
s.pop();
col[x]=scc;
vis[x]=;
}while(x!=u);
}
} int main()
{
while(scanf("%d%d",&n,&m)== && n+m)
{ init(); scc=;//scc表示强连通分量的个数
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
add(a,b);//构造邻接矩阵
}
for(int i=;i<=n;i++)
{
if(dfn[i]==-)
{
tarjan(i);
}
}
//printf("%d\n",scc); int inde[N];
int outde[N];
memset(inde,,sizeof(inde));
memset(outde,,sizeof(outde));
for(int i=;i<tot;i++)
{
int a=edge[i].from;
int b=edge[i].to;
if(col[a]!=col[b])
{
inde[col[b]]++;
outde[col[a]]++;
}
} int ans1=;
int ans2=;
for(int i=;i<=scc;i++)
{
if(inde[i]==)
{
ans1++;
}
if(outde[i]==)
{
ans2++;
}
}
if(scc==)
{
printf("0\n");
continue;
}
printf("%d\n",max(ans1,ans2)); }
return ;
}
hdu 3836 Equivalent Sets(tarjan+缩点)的更多相关文章
- hdu 3836 Equivalent Sets trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- [tarjan] hdu 3836 Equivalent Sets
主题链接: http://acm.hdu.edu.cn/showproblem.php? pid=3836 Equivalent Sets Time Limit: 12000/4000 MS (Jav ...
- 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(强连通分量--加边)
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(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- HDU3836 Equivalent Sets (Tarjan缩点+贪心)
题意: 给你一张有向图,问你最少加多少条边这张图强连通 思路: 缩点之后,如果不为1个点,答案为出度为0与入度为0的点的数量的最大值 代码: #include<iostream> #inc ...
- 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 ...
随机推荐
- [原创作品] web项目构建(一)
今天开始,将推出web项目构建教程,与<javascript精髓整理篇>一并更新.敬请关注. 这篇作为这一系列开头,主要讲述web项目的构建技术大全.在众多人看来,web前端开发无非就是写 ...
- 七、Solr服务部署和安全
概念: 我们知道,Solr是以webapp的形式运行的,那么我们只需要把Solr.war文件部署到web容器中,便可以运行了,但是因为需要连接数据库做索引并且提供线上的服务调用query接口,那么So ...
- Struts的核心配置
一.配置struts.xml文件 1.struts.xml文件 2.常量配置 <constant> struts.properities web.xml中的<init-param&g ...
- jQuery中DOM操作
1 定义:jquery中对DOM的操作就是对DOM元素进行增删查改操作 2 分类: 1)DOM Core(核心):用途广泛 支持多种编程语言 2)HTML DOM:代码简短 只用于处理web ...
- css3背景总结与解析
一.常用基本属性: background-color:transparent || <color> 常用颜色格式有:颜色名.rgb.hls.十六进制.rgba.hlsa. b ...
- (转)SQL NEWID()随机函数
从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid()的返回值 是uniqueidenti ...
- java线层的使用
class Do3 { public static void main(String[] args) { XC d1=new XC("小王"); XC d2=new XC(&quo ...
- Word 2010发布博客文章
只测试了cnblog 1.新建文件选择word 2010自带的博客文章模板 2.在管理账户中新建一个博客账户,也就是你自己在博客园的账户,博客选其他 3.然后选择下一步,博客的URL在自己的博客设置里 ...
- (原)torch使用caffe时,提示CUDNN_STATUS_EXECUTION_FAILED
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6230227.html 提前说明:此文不能真正解决该问题,具体原因我也不知道... 以前使用某台电脑A上 ...
- uva 469 - Wetlands of Florida
题目:给你一个矩阵和某些点,找到给的点所处连续的W区域的面积(八个方向). 分析:搜索.floodfill算法,利用搜索直接求解就可以了. 说明:注意读入数据的格式. #include <cst ...