HDU 3836 Equivalent SetsTarjan+缩点)
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<limits.h>
typedef long long LL;
using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a ) const int maxn=20000+100;
const int maxm=100000;
struct node{
int u,v;
int next;
}e[maxm];
int head[maxn],cntE;
int DFN[maxn],low[maxn];
int s[maxm],top,index,cnt;
int belong[maxn],instack[maxn];
int in[maxn],out[maxn];
int n,m;
void init()
{
top=cntE=0;
index=cnt=0;
CLEAR(DFN,0);
CLEAR(head,-1);
CLEAR(instack,0);
// CLEAR(belong,0);
}
void addedge(int u,int v)
{
e[cntE].u=u;e[cntE].v=v;
e[cntE].next=head[u];
head[u]=cntE++;
}
void Tarjan(int u)
{
DFN[u]=low[u]=++index;
instack[u]=1;
s[top++]=u;
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(!DFN[v])
{
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],DFN[v]);
}
int v;
if(DFN[u]==low[u])
{
cnt++;
do{
v=s[--top];
belong[v]=cnt;
instack[v]=0;
}while(u!=v);
}
}
void work()
{
REPF(i,1,n)
if(!DFN[i]) Tarjan(i);
if(cnt<=1)
{
puts("0");
return ;
}
CLEAR(in,0);
CLEAR(out,0);
for(int i=0;i<cntE;i++)
{
int u=e[i].u,v=e[i].v;
if(belong[u]!=belong[v])
in[belong[v]]++,out[belong[u]]++;
}
int d_1=0,d_2=0;
REPF(i,1,cnt)
{
if(!in[i])
d_1++;
if(!out[i])
d_2++;
}
printf("%d\n",max(d_1,d_2));
}
int main()
{
int u,v;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
addedge(u,v);
}
work();
}
return 0;
}
HDU 3836 Equivalent SetsTarjan+缩点)的更多相关文章
- hdu 3836 Equivalent Sets
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3836 Equivalent Sets Description To prove two sets A ...
- [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(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 Equivalent Sets(强连通)
http://acm.hdu.edu.cn/showproblem.php?pid=3836 判断至少需要加几条边才能使图变成强连通 把图缩点之后统计入度为0的点和出度为0的点,然后两者中的最大值就是 ...
- 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 (强连通分量+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 ...
随机推荐
- MySQL错误:You are using safe update mode and you tried to update a table without a WHERE that uses a K
转载自:http://blog.csdn.net/dragonpeng2008/article/details/7279590 Error: 1175 SQLSTATE: HY000 (ER_UPDA ...
- Houdini Pyro流体的插值变速
用简单的节点尝试了下Houdini流体的变速,这里的流体指的是Pyro,而不是FLIP.FLIP仅仅须要记录ID属性然后TimeBlend & TimeShift就可以. Vimeo 上图是一 ...
- 类是公共,它应该被命名为.java文件声明
当类的设置public时间,,public只要类的文件名必须是相同的,..这种错误可能发生在不同的
- 在不同版本号hdfs集群之间转移数据
在不同版本号hdfs集群之间转移数据 最简单的办法就是把src集群的数据导到本地,然后起还有一个进程将本地数据传到des集群上去. 只是这有几个问题: 效率减少 占用本地磁盘空间 不能应付实时 ...
- hdu1428之spfa+dfs
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- WCF之添加自定义用户名密码认证
1.创建WCF服务应用以及调用客户端(请自行google). 2.创建X509证书 cmd 进入 C:\Program Files\Microsoft SDKs\Windows\v6. ...
- Nutch+HBase
Nutch+HBase 当我们为nutch的架构发愁的时候,nutch的开发人员送来了nutchbase.我一些简单的测试表明,在hadoop0.20.1和hbase0.20.2上,稍加修改可以运行起 ...
- “简密”App Store处女作开发总结
前言 今天是我的iOS App Store上架应用处女作"简密"第一天上线的日子,简密是我从事iOS开发三年以来的第一款个人上架应用,之前做过两年的企业级应用开发以及公司的电商应用 ...
- Effective C++ 条款24
若全部參数皆需类型转换,请为此採用non-member函数 我们直奔主题 假设你定义一个有理数类例如以下 class Rational{ public: Rational(int numerator= ...
- Mutex(测量)
游标共享怎样使用Mutex kks 使用mutex以便保护对于下述基于parent cursor父游标和子游标child cursor的一系列操作 对于父游标parent cursor的操作: 基于发 ...