Equivalent Sets

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

Total Submission(s): 2526    Accepted Submission(s): 857

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

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std; const int MAX=20000+10;
int n,m,size,top,index,ind,oud;
int head[MAX],dfn[MAX],low[MAX],stack[MAX];
int mark[MAX],flag[MAX];
//dfn表示点u出现的时间,low表示点u能到达所属环中最早出现的点(记录的是到达的时间) struct Edge{
int v,next;
Edge(){}
Edge(int V,int NEXT):v(V),next(NEXT){}
}edge[50000+10]; void Init(int num){
for(int i=0;i<=num;++i)head[i]=-1;
size=top=index=ind=oud=0;
} void InsertEdge(int u,int v){
edge[size]=Edge(v,head[u]);
head[u]=size++;
} void tarjan(int u){
if(mark[u])return;
dfn[u]=low[u]=++index;
stack[++top]=u;
mark[u]=1;
for(int i=head[u];i != -1;i=edge[i].next){
int v=edge[i].v;
tarjan(v);
if(mark[v] == 1)low[u]=min(low[u],low[v]);//必须点v在栈里面才行
}
if(dfn[u] == low[u]){
++ind,++oud;//记录缩点之后的点的个数,方便计算入度和出度为0的点的个数
while(stack[top] != u){
mark[stack[top]]=-1;
low[stack[top--]]=low[u];
}
mark[u]=-1;
--top;
}
} int main(){
int u,v;
while(~scanf("%d%d",&n,&m)){
Init(n);
for(int i=0;i<m;++i){
scanf("%d%d",&u,&v);
InsertEdge(u,v);
}
memset(mark,0,sizeof mark);
for(int i=1;i<=n;++i){
if(mark[i])continue;
tarjan(i);
}
if(ind == 1){printf("0\n");continue;}//仅仅剩一个点了表示原图数个强联通图
for(int i=0;i<=n;++i)mark[i]=flag[i]=0;
for(int i=1;i<=n;++i){
for(int j=head[i];j != -1;j=edge[j].next){
v=edge[j].v;
if(low[i] == low[v])continue;
if(mark[low[i]] == 0)--oud;//mark标记点i是否已有出度
if(flag[low[v]] == 0)--ind;//flag标记点v是否已有入度
mark[low[i]]=flag[low[v]]=1;
}
}
printf("%d\n",max(ind,oud));
}
return 0;
}

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

hdu3836联通的强还原性点的更多相关文章

  1. 【强联通图 | 强联通分量】HDU 1269 迷宫城堡 【Kosaraju或Tarjan算法】

      为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的,就是说若称某通道连通了A房间和B房间,只说明 ...

  2. 有向连通图增加多少边构成强联通(hdu3836,poj1236)

    hdu3836 求出强分量后缩点处理得到分支图,对分支图的每个强连通分量统计出度和入度.需要的边数就是:统计 入度=0 的顶点数 和 出度=0 的顶点数,选择两者中较大的一个,才能确保一个强连通图. ...

  3. 有向图的强联通tarjan算法(判断是否为强联通模板)(hdu1269)

    hdu1269 迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  4. Tarjan 求图点强联通,桥的应用

    在图中求双联通和强联通分量是我们解决非树结构的图连通问题的利器 通过求求图的双联通和强联通分量能把图转化成DAG进行求解: 行走 Description 给出一个有向图,你可以选择从任意点出发走到任意 ...

  5. 开源Web自动化测试工具Selenium IDE

    Selenium IDE(也有简写SIDE的)是一款开源的Web自动化测试工具,它实现了测试用例的录制与回放. Selenium IDE目前版本为 3.6 系列,支持跨浏览器运行,所以IDE的UI从原 ...

  6. 转贴:天然VC的迷局

    天然VC的迷局作者:棱子 http://www.jkzgr.net/jiankangguanli/176.html 维生素C对人类来说是一种必不可少的维生素.我们可以通过正常饮食获取所需的VC.市场上 ...

  7. hdu畅通工程

    传送门 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. $tarjan$简要学习笔记

    $QwQ$因为$gql$的$tarjan$一直很差所以一直想着要写个学习笔记,,,咕了$inf$天之后终于还是写了嘻嘻. 首先说下几个重要数组的基本定义. $dfn$太简单了不说$QwQ$ 但是因为有 ...

  9. Kosaraju算法---强联通分量

    1.基础知识 所需结构:原图.反向图(若在原图中存在vi到vj有向边,在反向图中就变为vj到vi的有向边).标记数组(标记是否遍历过).一个栈(或记录顶点离开时间的数组).      算法描叙: :对 ...

随机推荐

  1. Microsoft Build 2016 Day 2

    Microsoft Build 2016 Day 2 Microsoft Build 2016 Day 1 记录 Microsoft Build 2016 进行到了第二天,我觉得这一天的内容非常精彩, ...

  2. Hdu 5073 Galaxy 精度问题

    思路: 其实求解很简单直接说解法,移动K个后 上下的角动量最小,能肯定是相连的(n-k)个,至于为什么 你自己好好想想(easy): 对于一些等质量的质点中心在 所在位置和除以点的个数 average ...

  3. 牟大哥:《App自我促销》连载2 直立人迁移走

    [谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代.是一个奇妙的东西,它可以非常快地把信息传播到其它地方,突破了短距离. 然而能人的后代直立人学会了直立行走,他们開始走出非洲,到达遥远的中东.中国 ...

  4. zookeeper错误KeeperErrorCode = ConnectionLoss解决

    原因: 一般是由于连接还未完成就执行zookeeper的get/create/exsit操作引起的. 解决方法: 利用"CountDownLatch 类 + zookeeper的watche ...

  5. 【SICP读书笔记(四)】练习2.27 --- 表序列reverse的扩展:树结构的deep-reverse

    题目要求是,修改练习2.18所做的reverse过程,得到一个deep-reverse过程.它以一个表为参数,返回另一个表作为值,结果表中的元素反转过来,其中的子树也反转. 例如: (define x ...

  6. 【leetCode百题成就】Gas Station解题报告

    题目: There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. ...

  7. Java 的swing.GroupLayout布局管理器的使用方法和实例(转)

    The following builds a panel consisting of two labels in one column, followed by two textfields in t ...

  8. 【Web探索之旅】第二部分第三课:框架和内容管理系统

    内容简介 1.第二部分第三课:框架和内容管理系统 2.第二部分第四课预告:数据库   第二部分第三课:框架和内容管理系统 上一课我们介绍了服务器端的编程语言,有PHP,Java,Python,Ruby ...

  9. Java AIO 入门实例(转)

    Java7 AIO入门实例,首先是服务端实现: 服务端代码 SimpleServer: public class SimpleServer { public SimpleServer(int port ...

  10. Fuel 5.1安装openstack I版本号环境

    声明:本博客欢迎转载,但请保留原作者信息! 作者:傅斌杰 团队:华为杭州OpenStack团队 Fuel 简单介绍 Fuel是Mirantis公司开发的部署openstack集群工具,主要功能为裸机P ...