http://acm.nyist.net/JudgeOnline/problem.php?pid=120&&强连通分量
来源:互联网 发布:什么牌子足球鞋知乎 编辑:程序博客网 时间:2024/06/07 19:40
题意:让一个图变成强连通图,最少需要添加多少边
思路:先求出强连通图的个数,然后缩边后比较图中入度为0和出度为0的顶点个数
#include<iostream>#include<string>#include<string.h>#include<algorithm>#define N 101#define M 10010using namespace std;bool istack[N];int head[N],dfn[N],low[N],stack[N],belong[N],out[N],in[N];int n,m,res,indexx,num,top;typedef struct str{int to;int next;}Node;Node node[M];void initt(){ memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(out,0,sizeof(out)); memset(belong ,0,sizeof(belong)); memset(istack,false,sizeof(istack)); memset(stack,0,sizeof(stack)); memset(in,0,sizeof(in));res=top=num=0;indexx=1;}void dfs(int i){dfn[i]=low[i]=indexx++;istack[i]=true;stack[top++]=i;for(int j=head[i];j!=-1;j=node[j].next){ int v=node[j].to; if(!dfn[v]) { dfs(v); low[i]=min(low[i],low[v]); } else if(istack[v]) low[i]=min(dfn[v],low[i]);}int u;if(dfn[i]==low[i]){res++;do{ u=stack[--top];belong[u]=res;istack[u]=false;}while(i!=u);}}void tarjan(){for(int i=1;i<=n;++i)if(!dfn[i]) dfs(i);}void solve(){for(int i=1;i<=n;++i) for(int j=head[i];j!=-1;j=node[j].next)if(belong[i]!=belong[node[j].to]){out[belong[i]]++;in[belong[node[j].to]]++; }if(res==1) cout<<"0"<<endl;//当图本身就是强连通图时else{int p=0,q=0;for(int i=1;i<=res;++i){ if(!out[i]) p++; if(!in[i]) q++;}cout<<max(p,q)<<endl;}}int main(){ //freopen("c:\\Input.txt","r",stdin); //freopen("c:\\Out.txt","w",stdout);int T;cin>>T;while(T--){ initt();cin>>n;for(int i=1;i<=n;++i){ int flag=0;int a; while(cin>>a&&a) { node[num].to=a;node[num].next=head[i];head[i]=num++; } }tarjan();solve(); }return 0;}
- http://acm.nyist.net/JudgeOnline/problem.php?pid=120&&强连通分量
- http://acm.nyist.net/JudgeOnline/problem.php?pid=290
- http://acm.nyist.net/JudgeOnline/problem.php?pid=434
- http://acm.nyist.net/JudgeOnline/problem.php?pid=13
- http://acm.nyist.net/JudgeOnline/problem.php?pid=239
- http://acm.nyist.net/JudgeOnline/problem.php?pid=129
- http://acm.nyist.net/JudgeOnline/problem.php?pid=431
- http://acm.nyist.net/JudgeOnline/problem.php?pid=301
- http://acm.nyist.net/JudgeOnline/problem.php?pid=451
- http://acm.nyist.net/JudgeOnline/problem.php?pid=459
- http://acm.nyist.net/JudgeOnline/problem.php?pid=20
- http://acm.nyist.net/JudgeOnline/problem.php?pid=267
- http://acm.nyist.net/JudgeOnline/problem.php?pid=305
- http://acm.nyist.net/JudgeOnline/problem.php?pid=309
- http://acm.nyist.net/JudgeOnline/problem.php?pid=325
- http://acm.nyist.net/JudgeOnline/problem.php?pid=420
- http://acm.nyist.net/JudgeOnline/problem.php?pid=488
- http://acm.nyist.net/JudgeOnline/problem.php?pid=150
- opencv 在VC、MinGW-GCC、及C++Builder中的运用
- new,malloc,GlobalAlloc详解
- Android多媒体分析(二)MediaStore
- NSMutableArray使用中遇到的内存问题
- 树
- http://acm.nyist.net/JudgeOnline/problem.php?pid=120&&强连通分量
- oracle 视图
- myEclipse9 jar插件 反编译插件 jad 安装
- POJ-2504(简单几何)
- sql server出错信息
- java调用存储过程(output和return)
- C# 仿MVC从Request请求中返回对象,及获取客户机的MAC地址等.
- VC VB Delphi 清除IE缓存 internet临时文件 cookie 历史记录 表单记录 上网密码
- UISwipeGestureRecognizer ---手指动作