hdu 3836 Equivalent Sets //tarjan+缩点
来源:互联网 发布:淘宝网韩都衣舍男装 编辑:程序博客网 时间:2024/04/29 17:13
题目链接
题意:给n个点和m条有向边,问最少再加几条边使其变成强连通图。和hdu2767同样的解法。
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#define N 22000#define M 55000using namespace std;struct node{ int next,to;}e[M];int head[N],cnt,scnt,top,n,m,dfn[N],low[N],v[N],belong[N],in[N],out[N],q[N];void init(){ memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(v,0,sizeof(v)); memset(belong,0,sizeof(belong)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); cnt=scnt=top=0;}void add_edge(int u,int v){ e[cnt].to=v; e[cnt].next=head[u]; head[u]=cnt++;}void tarjan(int t){ low[t]=dfn[t]=cnt++; q[top++]=t; v[t]=1; for(int i=head[t];i+1;i=e[i].next) { int u=e[i].to; if(!dfn[u]) { tarjan(u); low[t]=min(low[t],low[u]); } else if(v[u]) low[t]=min(low[t],dfn[u]); } int u; if(low[t]==dfn[t]) { scnt++; do { u=q[--top]; v[u]=0; belong[u]=scnt; }while(t!=u); }}void solve(){ for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); if(scnt==1) { cout<<0<<endl; return; } for(int i=1;i<=n;i++) for(int j=head[i];j+1;j=e[j].next) { int u=e[j].to; if(belong[u]!=belong[i]) { in[belong[u]]++; out[belong[i]]++; } } int a=0,b=0; for(int i=1;i<=scnt;i++) { if(in[i]==0) a++; if(out[i]==0) b++; } cout<<max(a,b)<<endl;}int main(){ while(~scanf("%d%d",&n,&m)) { init(); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); add_edge(u,v); } solve(); }}
0 0
- HDU 3836 Equivalent Sets(Tarjan+缩点)
- hdu 3836 Equivalent Sets //tarjan+缩点
- hdu 3836 Equivalent Sets 强联通 tarjan缩点
- HDU 3836 Equivalent Sets(强连通+缩点,tarjan)
- [tarjan] hdu 3836 Equivalent Sets
- HDU 3836 Equivalent Sets(强连通分量 Tarjan缩点)
- hdoj 3836 Equivalent Sets 【tarjan 求SCC + 缩点】
- HDU3836 Equivalent Sets :Tarjan缩点
- POJ 1236 Network of Schools HDU 3836 Equivalent Sets 强连通分量+缩点 tarjan or kosaraju
- hdu 3836 Equivalent Sets【强连通Kosaraju+缩点染色】
- hdu 3836 Equivalent Sets(强连通,tarjan,4级)
- HDU 3836 Equivalent Sets (tarjan求强联通分量)
- HDU 3836 Equivalent Sets(tarjan + 压缩强连通)
- hdu 3836 Equivalent Sets
- hdu 3836 Equivalent Sets
- HDU 3836 Equivalent Sets
- hdu 3836 Equivalent Sets
- hdu3836 Equivalent Sets(缩点)
- Oracle 12c 和 Hadoop:大数据的最佳存储和处理办法
- javascript设计模式--原型模式
- 在C#里面给PPT添加注释
- esds box解析
- source insight 3.5设置
- hdu 3836 Equivalent Sets //tarjan+缩点
- matlab——zeros函数
- 动手写一个快速集成网易新闻,腾讯视频,头条首页的ScrollPageView,显示滚动视图
- 从关系型数据库到非关系型数据库
- stdClass对象数组转化成php数组
- JAVA--享元模式(Flyweight)--设计模式十二
- 集群之session共享
- OpenGL选择-拾取机制(转载自zwqxin)
- LeetCode 67. Add Binary/ 66. Plus One