hdoj 3836 Equivalent Sets
来源:互联网 发布:2012年欧洲杯决赛 知乎 编辑:程序博客网 时间:2024/06/05 09:10
题目链接:Equivalent Sets
题目大意:要证明两个集合A、B等价,需要得到X是Y的子集并且Y也是X的子集,现在有N个集合,M个条件,代表a是b的子集,问最少需要再添加多少条件使得所有N个集合都是等价的
题目思路:X是Y的子集,也就是Y到X有一条有向边,那么题目就转换为了给你一个有向图,问最少添加多少条边使得所有的点都能到达任何另外的点,tarjan缩点,找出度为0和入度为0的最小值就可以了
#include <map>#include <set>#include <cmath>#include <stack>#include <queue>#include <vector>#include <cstdio>#include <string>#include <utility>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int maxn = 20010;vector<int>Edge[maxn];stack<int>S;int Dfn[maxn],Low[maxn],sccno[maxn],tclock,sccnt;int InDeg[maxn],OutDeg[maxn];void tarjan(int u){ Dfn[u] = Low[u] = ++tclock; S.push(u); for(int i = 0;i < Edge[u].size();i++){ int v = Edge[u][i]; if(!Dfn[v]){ tarjan(v); Low[u] = min(Low[u],Low[v]); } else if(!sccno[v]){ Low[u] = min(Low[u],Dfn[v]); } } if(Low[u] == Dfn[u]){ sccnt += 1; int v = -1; while(v != u){ v = S.top(); S.pop(); sccno[v] = sccnt; } }}void findscc(int n){ tclock = sccnt = 0; memset(Dfn,0,sizeof(Dfn)); memset(Low,0,sizeof(Low)); memset(sccno,0,sizeof(sccno)); for(int i = 1;i <= n;i++) if(!Dfn[i]) tarjan(i);}int solve(int n){ if(sccnt == 1) return 0; memset(InDeg,0,sizeof(InDeg)); memset(OutDeg,0,sizeof(OutDeg)); for(int u = 1;u <= n;u++){ for(int i = 0;i < Edge[u].size();i++){ int v = Edge[u][i]; if(sccno[u] != sccno[v]){ InDeg[sccno[v]]++; OutDeg[sccno[u]]++; } } } int c1 = 0,c2 = 0; for(int i = 1;i <= sccnt;i++){ if(InDeg[i] == 0) c1++; if(OutDeg[i] == 0) c2++; } return max(c1,c2);}int main(){ int T,n,m; while(~scanf("%d%d",&n,&m)){ for(int i = 1;i <= n;i++) Edge[i].clear(); while(m--){ int u,v; scanf("%d%d",&u,&v); Edge[v].push_back(u); } findscc(n); printf("%d\n",solve(n)); }}
阅读全文
0 0
- HDOJ 3836 Equivalent Sets
- hdoj 3836 Equivalent Sets
- HDOJ/HDU 3836 Equivalent Sets
- HDU/HDOJ 3836 Equivalent Sets 多校联合1
- 【强连通分量+缩点】 HDOJ 3836 Equivalent Sets
- HDOJ 题目3836 Equivalent Sets(强连通分量)
- hdoj 3836 Equivalent Sets 【tarjan 求SCC + 缩点】
- hdu 3836 Equivalent Sets
- hdu 3836 Equivalent Sets
- HDU 3836 Equivalent Sets
- hdu 3836 Equivalent Sets
- [图论] hdu 3836 Equivalent Sets
- [tarjan] hdu 3836 Equivalent Sets
- HDU 3836 — Equivalent Sets
- hdu acm 3836 Equivalent Sets
- hdoj Equivalent Sets 3836 (强连通分量 最少添加几条边使图强连通)
- HDOJ--3836--Equivalent Sets(tarjan算法)//求连接几个SCC最少的边
- HDU 3836 Equivalent Sets 强连通分量
- MySQL Slave I/O Thread 失败问题
- 社团发现算法
- tomcat 7/8 启动非常慢的解决方法
- 通过yarn 查看完整的hql语句
- 操作系统---基本原理
- hdoj 3836 Equivalent Sets
- 纯js实现分页 原理:所有数据已加载好 js通过遍历部分显示 实现分页效果
- [笔记]区间最值RMQ---ST
- [code forces] 762A
- Redhat中ifconfig命令不能使用怎么办?
- SaltStack命令大全
- Oracle数据库知识和SQL语句总结
- elasticsearch5.4.0 生产环境部署 及插件 ik + head +kibana
- 关于include指令和include动作的区别