【POJ2186】Popular Cows(tarjan+缩点)
来源:互联网 发布:淘宝司法拍卖怎么样 编辑:程序博客网 时间:2024/04/30 15:17
记录一个菜逼的成长。。
第一道强连通图的题。。
tarjan算法+缩点 模板题
算法伪代码如下
tarjan(u)
{
DFN[u]=Low[u]=++Index // 为节点u设定次序编号和Low初值
Stack.push(u) // 将节点u压入栈中
for each (u, v) in E // 枚举每一条边
if (v is not visted) // 如果节点v未被访问过
tarjan(v) // 继续向下找
Low[u] = min(Low[u], Low[v])
else if (v in S) // 如果节点v还在栈内
Low[u] = min(Low[u], DFN[v])
if (DFN[u] == Low[u]) // 如果节点u是强连通分量的根
repeat
v = S.pop // 将v退栈,为该强连通分量中一个顶点
print v
until (u== v)
}
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <iostream>#include <cstring>#include <string>#include <algorithm>#include <cstdlib>#include <vector>#include <set>#include <map>#include <queue>#include <stack>#include <list>#include <deque>#include <cctype>#include <bitset>#include <cmath>using namespace std;#define ALL(v) (v).begin(),(v).end()#define cl(a,b) memset(a,b,sizeof(a))#define bp __builtin_popcount#define pb push_back#define mp make_pair#define fin freopen("D://in.txt","r",stdin)#define fout freopen("D://out.txt","w",stdout)#define lson t<<1,l,mid#define rson t<<1|1,mid+1,r#define seglen (node[t].r-node[t].l+1)#define pi 3.1415926#define exp 2.718281828459typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;typedef pair<LL,LL> PLL;typedef vector<PII> VPII;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;template <typename T>inline void read(T &x){ T ans=0; char last=' ',ch=getchar(); while(ch<'0' || ch>'9')last=ch,ch=getchar(); while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar(); if(last=='-')ans=-ans; x = ans;}/******************head***********************/const int maxn = 50000 + 10;int head[maxn],cnt;struct Edge{ int next,to;}edge[maxn];void add(int u,int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++;}int instack[maxn],dfn[maxn],low[maxn],ind,num,belong[maxn],setnum[maxn];stack<int>sta;int d[maxn];///出度void tarjan(int u){ ///dfn(u)为时间戳,即dfs序 ///Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号 dfn[u] = low[u] = ++ind; instack[u] = 1; sta.push(u); for( int i = head[u]; ~i; i = edge[i].next ){ int v = edge[i].to; if(!dfn[v]){ tarjan(v); low[u] = min(low[u],low[v]); } else if(instack[v]){ low[u] = min(low[u],dfn[v]); } } if(dfn[u] == low[u]){ num++; int v; do{ v = sta.top(); sta.pop(); instack[v] = 0; belong[v] = num;///点v属于第num个强连通分量 setnum[num]++;///每个强连通分量的数量 }while(u != v); }}void init(){ while(!sta.empty())sta.pop(); cl(head,-1),cnt = 0; cl(instack,0),cl(dfn,0),cl(low,0); ind = 0; num = 0; cl(belong,0); cl(setnum,0); cl(d,0);}int main(){ //fin,fout; int n,m; while(~scanf("%d%d",&n,&m)){ init(); int u,v; for( int i = 1; i <= m; i++ ){ scanf("%d%d",&u,&v); add(u,v); } for( int i = 1; i <= n; i++ ){ if(!dfn[i]){ tarjan(i); } } for( int i = 1; i <= n; i++ ){ for(int j = head[i]; ~j; j = edge[j].next ){ if(belong[i] != belong[edge[j].to]){ d[belong[i]]++; } } } int cnt = 0,res,ans = 0; for( int i = 1; i <= num; i++ ){ if(!d[i]){ cnt++; res = i; } } printf("%d\n",cnt == 1 ? setnum[res] : 0); } return 0;}
0 0
- poj2186 Popular Cows(tarjan + 缩点)
- POJ2186--Popular Cows(Tarjan+缩点)
- 【POJ2186】Popular Cows(tarjan+缩点)
- POJ2186 Popular Cows (Tarjan)
- poj2186 Popular Cows (Tarjan)
- POJ2186 Popular Cows(Tarjan算法)
- poj2186 Popular Cows (Tarjan )
- [复习][poj2186]Tarjan Popular Cows
- POJ2186 Popular Cows,Kosaraju算法+缩点
- POJ2186--Popular Cows(Korasaju+缩点)
- POJ2186 Popular Cows 强连通 Tarjan
- POJ2186 Popular Cows【Tarjan】【强连通分量】
- poj2186 Popular Cows(Tarjan+缩点水)
- poj2186 Popular Cows(强连通分量tarjan)
- poj 2186 Popular Cows (tarjan + 缩点)
- POJ 2186 Popular Cows -- tarjan 缩点
- poj Popular Cows(tarjan +缩点)
- POJ 2186 popular cows (tarjan + 缩点)
- 设计模式学习笔记-观察者模式
- Ubuntu 16.04 解压缩zip文件中文乱码
- 世界65%银行在3年内将采用区块链技术
- HDU2191悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
- 如何使用arraylist对存放class中某个元素进行排序
- 【POJ2186】Popular Cows(tarjan+缩点)
- Android RecyclerView 使用完全解析 体验艺术般的控件
- dos2unix命令将dos文件转换为unix格式
- 访问chrome摄像头报错
- 第24节--聚类之K-means算法(上)
- Android App 沉浸式状态栏解决方案
- windows下 composer常见错误及处理。
- GitHub创建SSH Keys
- javascript求点的凸集/凸包