hdu 3836 Equivalent Sets(强连通分量--加边)
Equivalent Sets
Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Others)
Total Submission(s): 2798 Accepted Submission(s): 962
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.
求将原图的强连通分量缩点,得到有向无环图,求至少加多少条边能够使这个图变成一幅强连通图,max(入度为0的点,出度为0的点)即为答案。
#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"algorithm"
using namespace std;
#define N 20005
#define M 50005
#define min(a,b) (a<b?a:b)
const int inf=1000000;
struct node
{
int u,v,next;
}e[M];
int t,bcnt,index,stop,ans;
int head[N],dfn[N],low[N],stap[N],mark[N],be[N];
int indeg[N],out[N];
void add(int u,int v)
{
e[t].u=u;
e[t].v=v;
e[t].next=head[u];
head[u]=t++;
}
void tarjan(int u)
{
int i,v;
dfn[u]=low[u]=++index;
stap[++stop]=u;
mark[u]=1;
for(i=head[u];i!=-1;i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(mark[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
bcnt++;
do
{
v=stap[stop--];
mark[v]=0;
be[v]=bcnt;
}
while(u!=v);
ans++;
}
}
void solve(int n)
{
int i;
memset(dfn,0,sizeof(dfn));
index=stop=bcnt=0;
for(i=1;i<=n;i++)
{
if(!dfn[i])
tarjan(i);
}
}
void work(int n)
{
int i,j,u,v,t1,t2;
memset(indeg,0,sizeof(indeg));
memset(out,0,sizeof(out));
for(i=1;i<=n;i++)
{
u=be[i];
for(j=head[i];j!=-1;j=e[j].next)
{
v=be[e[j].v];
if(u!=v)
{
indeg[v]++;
out[u]++;
}
}
}
t1=t2=0;
for(i=1;i<=bcnt;i++)
{
if(indeg[i]==0)
t1++;
if(out[i]==0)
t2++;
}
printf("%d\n",t1>t2?t1:t2);
}
int main()
{
int n,m,u,v;
while(scanf("%d%d",&n,&m)!=-1)
{
t=0;
memset(head,-1,sizeof(head));
while(m--)
{
scanf("%d%d",&u,&v);
add(u,v);
}
ans=0;
solve(n);
// printf("%d\n",ans);
if(ans==1)
printf("0\n");
else
work(n);
}
return 0;
}
hdu 3836 Equivalent Sets(强连通分量--加边)的更多相关文章
- HDU - 3836 Equivalent Sets (强连通分量+DAG)
题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...
- hdu - 3836 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- [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(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 trajan缩点
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
- hdu 3836 tarjain 求强连通分量个数
// 给你一个有向图,问你最少加几条边能使得该图强连通 #include <iostream> #include <cstdio> #include <cstring&g ...
- hdoj 3836 Equivalent Sets【scc&&缩点】【求最少加多少条边使图强连通】
Equivalent Sets Time Limit: 12000/4000 MS (Java/Others) Memory Limit: 104857/104857 K (Java/Other ...
随机推荐
- ExpandListView onChildClickListener 失效
http://stackoverflow.com/questions/11529472/expandablelistview-onchildclicklistener-not-work 首先声明: ...
- Visual Studio Image Library
The Visual Studio Image Library Visual Studio 2013 The Visual Studio Image Library contains applic ...
- block高级功能
/* -*- c++ -*- */ /* * Copyright 2004,2007,2009,2010,2013 Free Software Foundation, Inc. * * This fi ...
- Windows进程通信 -- 共享内存
享内存的方式原理就是将一份物理内存映射到不同进程各自的虚拟地址空间上,这样每个进程都可以读取同一份数据,从而实现进程通信.因为是通过内存操作实现通信,因此是一种最高效的数据交换方法. 共享内存在 Wi ...
- JavaScript(三)-- DOM编程
JavaScript编程中最基本的就是DOM编程,DOM是 Document Object Model文本对象模型,就是对DOM对象进行编程的过程. Java语言和Js都有针对于DOM的编程,两者类似 ...
- flashback database(drop tablespace)
1.首先记录时间 select to_char(systimestamp,'yyyy-mm-dd HH24:MI:SS') from dual;--2014-04-25 13:55:48 查看表sel ...
- Linux命令-网络命令:traceroute
首先需要设置VM里面的linux系统能够上网.菜单项->虚拟机->设置,选择NAT模式 菜单项->编辑->虚拟网络编辑器->VMnet8 NAT模式->NAT设置, ...
- C#7.0之元组数据
static (string,string,string) LookupName(int a) { return ("","",""); } ...
- python selenium--常用函数3
ActionChains类鼠标操作的常用方法: 引入ActionChains类:from selenium.webdriver.common.action_chains import ActionCh ...
- APP消息推送功能
1.APP内部最好设计-我的消息-的功能,以便用户查看推送消息历史记录,通过角标.已读.未读等设计吸引用户读取消息.(画下来这都是重点) 2.建议提供推送设置功能,允许用户设置推送消息是否显示于通知栏 ...