|poj 1236|强连通分量|Network of Schools
来源:互联网 发布:淘宝上买药可靠吗 编辑:程序博客网 时间:2024/06/01 08:59
poj 1236
tarjan
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<stack>#define ms(i, j) memset(i, j, sizeof i)#define FN2 "poj1236" using namespace std;const int MAXN = 100 + 5;vector<int> G[MAXN];stack<int> s;int n, low[MAXN], dn[MAXN], tb, ex[MAXN];int cnt, scc_out[MAXN], scc_in[MAXN], scc_belong[MAXN];void tarjan(int u){ low[u] = dn[u] = ++tb; s.push(u), ex[u] = -1; for (int i=0;i<G[u].size();i++) { int v = G[u][i]; if (ex[v]==0) { tarjan(v); low[u] = min(low[u], low[v]); } else if (ex[v]==-1) { low[u] = min(low[u], dn[v]); } } if (low[u]==dn[u]) { int e; cnt++; do { e = s.top(); s.pop(); scc_belong[e] = cnt; ex[e] = 1; } while (u!=e); }}void rebuild(){ for (int u=1;u<=n;u++) { for (int i=0;i<G[u].size();i++) { int v = G[u][i]; if (scc_belong[u]!=scc_belong[v]) { scc_out[scc_belong[u]] = 1; scc_in[scc_belong[v]] = 1; } } }}void init(){ for (int i=1;i<=n;i++) { low[i] = dn[i] = ex[i] = scc_out[i] = scc_in[i] = scc_belong[i] = 0; G[i].clear(); } tb = cnt = 0; for (int i=1;i<=n;i++) { int x; while (scanf("%d", &x)==1&&x) { G[i].push_back(x); } }}void solve(){ for (int i=1;i<=n;i++) if (!dn[i]) tarjan(i); rebuild(); int ans = 0, ansin = 0, ansout = 0; for (int i=1;i<=cnt;i++) if (!scc_in[i]) ansin++; for (int i=1;i<=cnt;i++) if (!scc_out[i]) ansout++; ans = max(ansin, ansout); if (cnt==1) printf("1\n0\n"); else printf("%d\n%d\n", ansin, ans);}int main(){ #ifndef ONLINE_JUDGE freopen(FN2".in","r",stdin); freopen(FN2".out","w",stdout); #endif while (scanf("%d", &n)==1) { init(); solve(); } return 0;}
0 0
- 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 强连通分量
- 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(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- 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(强连通分量)
- Convex Hull
- include和require 区别
- UE4入门学习3:GamePlay框架
- 【Swift】代理使用举例
- 无知比愚蠢更可怕
- |poj 1236|强连通分量|Network of Schools
- Android 技术重温系列 (8)- AndroidManifest.xml
- Combo box的使用
- 监听器匿名类(一)
- Ubuntu安装inspectrum
- Python3.4.2 win32 安装 gdal
- 1090: 哥德巴赫猜测
- WebDriver api 中close、quit的区别
- Git操作