POJ 1236 Network of Schools tarjan强连通缩点
来源:互联网 发布:秦皇岛网络问政平台 编辑:程序博客网 时间:2024/04/30 08:10
tarjan算法很犀利,不解释,不解释。。。
缩点然后判断度数
#include<iostream>#include<vector>#include<cstring>#include<deque>#include<string>#include<cstdio>#include<cstdlib>#include<iomanip>#include<algorithm>#include<cassert>#define pb push_backusing namespace std;const int maxn = 111;vector<int>g[maxn];vector<int>v[maxn];vector<int>s;bool hash[maxn][maxn];int be[maxn];int dfn[maxn];int low[maxn];bool ins[maxn];int n,df,nb;int in[maxn];int out[maxn];int sa,sd;void tarjan(int now){ low[now] = dfn[now] = df++; s.pb(now); ins[now] = true; int to; for(int i=0;i<g[now].size();i++) { to = g[now][i]; if(!dfn[to]) { tarjan(to); low[now] = min(low[to],low[now]); } else if( ins[to] ) { low[now] = min(low[to],low[now]); } } if(low[now] == dfn[now] ) { while(s.back()!=now) { to = s.back(); be[to] = nb; s.pop_back(); ins[to] = false; } be[now] = nb++; ins[now] = false; s.pop_back(); } return ;}void start(){ s.clear(); nb = df = 1; memset(ins,false,sizeof(ins)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(be,0,sizeof(be)); memset(hash,false,sizeof(hash)); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(int i=1;i<=n;i++) { if(!dfn[i]) { tarjan(i); } } /* for(int i=1;i<=n;i++) { cout<<"dfn["<<i<<"] == "<<dfn[i]<<endl; cout<<"low["<<i<<"] == "<<low[i]<<endl; cout<<"be["<<i<<"] == "<<be[i]<<endl; } */ nb--; if(nb==1) { sa = 1; sd = 0; return ; } int from,to; for(int i=1;i<=n;i++) { from = be[i]; for(int j=0;j<g[i].size();j++) { to = be[g[i][j]]; if(from != to && !hash[from][to] ) { hash[from][to] = true; v[from].pb(to); } } } for(int i=1;i<=nb;i++) { from = i; for(int j=0;j<v[i].size();j++) { to = v[i][j]; out[from]++; in[to]++; } } sa = 0; sd = 0; for(int i=1;i<=nb;i++) { if(!in[i]) { sa++; } if(!out[i]) { sd++; } } sd = max(sa,sd); return ;}int main(){ while(cin>>n) { for(int i=1;i<=n;i++) { g[i].clear(); v[i].clear(); } int x; for(int i=1;i<=n;i++) { while(cin>>x) { if( !x ) { break; } g[i].push_back(x); } } start(); cout<<sa<<endl<<sd<<endl; } return 0;}
- POJ 1236 Network of Schools tarjan强连通缩点
- POJ 1236 Network of Schools【强连通缩点】【Tarjan算法】
- poj 1236 Network of Schools 强连通缩点
- poj 1236 Network of Schools(强连通缩点)
- poj-1236 Network Of Schools【强连通缩点】
- POJ 1236 Network of Schools【强连通缩点】
- poj 1236 Network of Schools 强连通缩点
- 【强连通缩点】Network of Schools POJ1236
- POJ 1236 Network of Schools【强连通分量分解&&缩点||tarjan&&缩点】
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
- Network of Schools(POJ 1236)(强连通+缩点)(Tarjan算法)
- POJ 1236 Network of Schools(强连通分量缩点+tarjan算法)
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- POJ 1236 Network of Schools (tarjan求强连通,缩点)
- POJ 1236 Network of Schools (Tarjan算法求强连通分量+缩点) 代码详解
- POJ 1236 Network of Schools 强连通图 tarjan
- poj 1236 Network of Schools(强连通分量 Tarjan)
- poj 1236 Network of Schools(强连通分量 Tarjan算法)
- Source Insight 3 打开代码后日文注释都是乱码后怎么办
- 程序开始前的准备工作
- Iterator迭代器
- google开放的一些接口
- 2011年北京大学计算机研究生机试真题(dijkstra+优先队列)
- POJ 1236 Network of Schools tarjan强连通缩点
- sql2000安装时,挂起的问题。
- Matrix类中的getColVector问题
- strcm--wcscmp
- 喝水时刻表
- 29、Failure Groups in ASM
- Android应用开发之短信窃听器(补充)
- Gridview用法大总结(牛年珍藏版)
- ASp.net 剖析三层架构