洛谷1726
2018-03-09 11:19
106 查看
小伙子,不会了,就回来看看。。。
这是蒟蒻第一次写tarjan,刚开始有一些点子没有考虑到,所以本篇就作为板子来打啦!!!
#include<vector> #include<cstdio> #include<cctype> #include<stack> using namespace std; vector<int>a[5004]; stack<int>q; int dfn[5004],b[5004],low[5004],col[5004],whic[5004],num,ans; //读优比较弱,大神们有何高见? inline int read(){ char ch=getchar();int k=0; while(!isdigit(ch)) ch=getchar(); while(isdigit(ch)){k=(k<<3)+(k<<1)+ch-'0';ch=getchar();} return k; } int minn(int a,int b){return a<=b?a:b;} void tarjan(int x){ dfn[x]=low[x]=++num;b[x]=1;q.push(x); for(int i=1;i<=a[x][0];i++){ int temp=a[x][i]; if(!dfn[temp]) {tarjan(temp);low[x]=minn(low[x],low[temp]);}//若temp未被访问过,则访问下去 else if(b[temp])low[x]=minn(low[x],low[temp]);//即temp点不在其他强连通分量里 } if(dfn[x]==low[x]){ b[x]=0;whic[x]=++col[0];col[col[0]]++; while(q.top()!=x){ int temp=q.top(); b[temp]=0;whic[temp]=col[0];col[col[0]]++; q.pop(); } q.pop(); if(col[col[0]]>ans) ans=col[col[0]]; } } int main(){ int n=read(),m=read(); for(int i=1;i<=n;i++) a[i].push_back(0); for(int i=1;i<=m;i++){ int x=read(),y=read(),z=read(); a[x][0]++;a[x].push_back(y); if(z==2){a[y][0]++;a[y].push_back(x);} } for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); printf("%d\n",ans); for(int i=1;i<=n;i++) if(col[whic[i]]==ans) printf("%d ",i); return 0; }
相关文章推荐
- 缩点
- Tarjan 0.2
- 夜深人静写算法(十)- 有向图强连通和2-sat问题
- P3627 [APIO2009]抢掠计划
- HDU1289 Tarjan-模板题
- HDU 3836 Equivalent Sets(强连通+缩点,tarjan)
- tarjan算法
- UVA 11770 Lighting Away
- POJ2553 The Bottom of a Graph Tarjan 矩阵 pascal 解题报告
- POJ3180 The Cow Prom Tarjan 链接表 pascal 解题报告
- POJ1986 Distance Queries LCA pascal 解题报告
- tarjan
- 【图论】【强连通分量】【Tarjan】pascal+邻接表
- bzoj 1051 缩点重构图
- poj1236 Network of Schools (tarjan缩点+求入度为0的点和出度为0的点的个数)
- poj3177 tarjan缩点+割边 模板 【pascal】
- poj1144 割点模板
- NOIP2015信息传递
- 洛谷P2921/BZOJ1589[USACO08DEC]在农场万圣节Trick or Treat on the Farm
- hdu5934(tarjan算法+缩点)bomb