Equivalent Sets 【至少加几边使图为强联通】+【 scc +缩点】
来源:互联网 发布:战天堂翅膀进阶数据 编辑:程序博客网 时间:2024/05/17 01:47
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
Hint
Case 2: First prove set 2 is a subset of set 1 and then prove set 3 is a subset of set 1.
思路: 扫一遍SCC+缩点 ,图为有向无环图 ,计数 每个新点的入度和出度,对于一个强联通的图来说,每个点入度和出度都肯定有。。
所以统计出 入度为0的点个数,出度为0的点个数; 然后就开始 互相连线,如果入度为0为2个,出度为0的为1个,则用那个出度为0的点和其中一个入度为0的相连就可以了。另外其它的点连那个入度为0的点就可以了。所以 我们只要找到 其中最大值 就可以了
代码
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>#include<queue>#include<stack>#include<map>#include<vector>#include<set>#define CLR(a,b) memset((a),(b),sizeof(a))#define inf 0x3f3f3f3f#define mod 100009#define LL long long#define M 50000+100#define ll o<<1#define rr o<<1|1#define lson o<<1,l,mid#define rson o<<1|1,mid+1,rusing namespace std;void read(int &x){ x=0;char c; while((c=getchar())<'0'); do x=x*10+c-'0';while((c=getchar())>='0');}struct Edge { int from,to,next;}edge[M];int head[M],top;int n,m;vector<int>G[M];vector<int >scc[M];int scc_cnt,dfs_clock;int low[M],dfn[M],in[M],out[M];stack<int>S;int Instack[M];int sccno[M];void init(){ top=0; memset(head,-1,sizeof(head)); while(!S.empty()) S.pop();}void addedge(int a,int b){ Edge e={a,b,head[a]}; edge[top]=e; head[a]=top++;}void getmap(){ int i,j;int a,b; for(i=0;i<m;i++) { read(a);read(b); addedge(a,b); }}void tarjan(int now){ int nexts; low[now]=dfn[now]=++dfs_clock; S.push(now);Instack[now]=1; for(int i=head[now];i!=-1;i=edge[i].next) { Edge e=edge[i]; if(!dfn[e.to]) { tarjan(e.to); low[now]=min(low[e.to],low[now]); } else if(Instack[e.to]) low[now]=min(low[now],dfn[e.to]); } if(dfn[now]==low[now]) { scc_cnt++; scc[scc_cnt].clear(); for(;;) { nexts=S.top();S.pop();Instack[nexts]=0; sccno[nexts]=scc_cnt; scc[scc_cnt].push_back(nexts); if(nexts==now) break; } }}void find_cut(int le,int ri){ memset(low,0,sizeof(low)); memset(dfn,0,sizeof(dfn)); memset(Instack,0,sizeof(Instack)); memset(sccno,0,sizeof(sccno)); scc_cnt=dfs_clock=0; for(int i=le;i<=ri;i++) if(!dfn[i]) tarjan(i);}void suodian(){ int i,j; for(i=1;i<=scc_cnt;i++) G[i].clear(),in[i]=0,out[i]=0; for(i=0;i<top;i++) { int u=sccno[edge[i].from]; int v=sccno[edge[i].to]; if(v!=u) { G[u].push_back(v); in[v]++;out[u]++; // 统计新点的入度和出度 } }}void solve(){ int i,j;int a,b;a=b=0; if(scc_cnt==1) printf("0\n"); // 特殊情况 要讨论下 else { for(i=1;i<=scc_cnt;i++) { if(!in[i]) a++; if(!out[i]) b++; } printf("%d\n",max(a,b)); }}int main(){ while(~scanf("%d%d",&n,&m)) { init(); getmap(); find_cut(1,n); suodian(); solve(); } return 0;}
- Equivalent Sets 【至少加几边使图为强联通】+【 scc +缩点】
- hdu 3836 Equivalent Sets 强联通 tarjan缩点
- HDU3836--Equivalent Sets(强连通+缩点)
- hdoj 3836 Equivalent Sets 【tarjan 求SCC + 缩点】
- HDU3836 Equivalent Sets 强联通分量 Tarjan
- hdu 3836 Equivalent Sets【强联通】
- hdu3836 Equivalent Sets(有向图至少添加多少条边才能变为强连通图+tarjan缩点)
- 【强连通分量+缩点】 HDOJ 3836 Equivalent Sets
- hdu 3836 Equivalent Sets【强连通Kosaraju+缩点染色】
- HDU 3836 Equivalent Sets(强连通+缩点,tarjan)
- HDU 3836 Equivalent Sets (tarjan求强联通分量)
- hdu3836 Equivalent Sets(缩点)
- hdoj3836Equivalent Sets【scc +缩点】
- POJ 1236--Network of Schools【scc缩点构图 && 求scc入度为0的个数 && 求最少加几条边使图变成强联通】
- HDU 3836--Equivalent Sets【求有向图最少增加多少边使图强连通 && Scc缩点新建图】
- HDU 3836 Equivalent Sets(强连通分量 Tarjan缩点)
- HDU 3836 Equivalent Sets(Tarjan+缩点)
- hdu 3836 Equivalent Sets //tarjan+缩点
- Callable类和Executors类实现简单的异步运算
- 支持向量机
- SpringBoot非官方教程 | 第十二篇:springboot集成apidoc
- 虚拟机类加载机制
- JProfiler9.2使用说明
- Equivalent Sets 【至少加几边使图为强联通】+【 scc +缩点】
- Android 中java反射应用(三)——应用场景及总结
- 从零开始搭建Raspberry Pi机器视觉编程环境
- Uber的合作创始人告诉我们:人工智能如何改变世界
- android 与HTM5 互调入门例子
- Meeting Room Arrangement
- 物联网时代哪三种职业最吃香?--转自美国科技博客TechCrunch撰稿人扎克·苏帕拉(Zach Supalla)
- [动态规划] 洛谷P1063 能量项链 (石子合并)
- 嵌入式开发者技能大全