POJ2186 Popular Cows 强连通分量
2017-01-01 13:58
337 查看
题目链接:http://poj.org/problem?id=2186
今天复习一下图论强连通,到poj上找到裸题,刷着玩一下,顺便水篇博客。
大概就是强连通缩一下点,然后我们看一下,缩点后的点如果有多个出度为0,则不存在,否则的话唯一一个出度为零的即为所求。
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
stack <int> dl;
const int MAXN = 150000;
int head[MAXN],to[MAXN],nxt[MAXN],dfn[MAXN],low[MAXN],ins[MAXN],sg[MAXN];
int oud[MAXN],sum[MAXN];
int cnt,n,m,a,b,tot,nfg,tjs;
void ad_edg(int x,int y)
{
nxt[++tjs] = head[x];
head[x] = tjs;
to[tjs] = y;
}
void sread()
{
scanf("%d%d",&n,&m);
for (int i = 1;i <= m;i++)
{
scanf("%d%d",&a,&b);
if (a == b) continue;
ad_edg(a,b);
}
}
void tarjan(int x)
{
dfn[x] = low[x] = ++cnt;
dl.push(x),ins[x] = 1;
for (int i = head[x];i;i = nxt[i])
{
if (!dfn[to[i]])
{
tarjan(to[i]);
low[x] = min(low[x],low[to[i]]);
}else if (ins[to[i]])
low[x] = min(low[x],dfn[to[i]]);
}
if (low[x] == dfn[x])
{
sg[x] = ++tot,sum[tot]++;
while (dl.top() != x) ins[dl.top()] = 0,sg[dl.top()] = tot,dl.pop(),sum[tot]++;
ins[x] = 0,dl.pop();
}
}
void swork()
{
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 = nxt[j])
if (sg[i] != sg[to[j]])
oud[sg[i]]++;
cnt = 0,a = 0;
for (int i = 1;i <= tot;i++)
if (!oud[i])
cnt++,a = i;
if(cnt > 1 || !cnt) printf("0\n");else
printf("%d\n",sum[a]);
}
int main()
{
sread();
swork();
return 0;
}
今天复习一下图论强连通,到poj上找到裸题,刷着玩一下,顺便水篇博客。
大概就是强连通缩一下点,然后我们看一下,缩点后的点如果有多个出度为0,则不存在,否则的话唯一一个出度为零的即为所求。
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
stack <int> dl;
const int MAXN = 150000;
int head[MAXN],to[MAXN],nxt[MAXN],dfn[MAXN],low[MAXN],ins[MAXN],sg[MAXN];
int oud[MAXN],sum[MAXN];
int cnt,n,m,a,b,tot,nfg,tjs;
void ad_edg(int x,int y)
{
nxt[++tjs] = head[x];
head[x] = tjs;
to[tjs] = y;
}
void sread()
{
scanf("%d%d",&n,&m);
for (int i = 1;i <= m;i++)
{
scanf("%d%d",&a,&b);
if (a == b) continue;
ad_edg(a,b);
}
}
void tarjan(int x)
{
dfn[x] = low[x] = ++cnt;
dl.push(x),ins[x] = 1;
for (int i = head[x];i;i = nxt[i])
{
if (!dfn[to[i]])
{
tarjan(to[i]);
low[x] = min(low[x],low[to[i]]);
}else if (ins[to[i]])
low[x] = min(low[x],dfn[to[i]]);
}
if (low[x] == dfn[x])
{
sg[x] = ++tot,sum[tot]++;
while (dl.top() != x) ins[dl.top()] = 0,sg[dl.top()] = tot,dl.pop(),sum[tot]++;
ins[x] = 0,dl.pop();
}
}
void swork()
{
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 = nxt[j])
if (sg[i] != sg[to[j]])
oud[sg[i]]++;
cnt = 0,a = 0;
for (int i = 1;i <= tot;i++)
if (!oud[i])
cnt++,a = i;
if(cnt > 1 || !cnt) printf("0\n");else
printf("%d\n",sum[a]);
}
int main()
{
sread();
swork();
return 0;
}
相关文章推荐
- [POJ2186]Popular Cows(强连通分量)
- [强连通分量]poj2186_Popular Cows
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】
- poj2186 Popular Cows(强连通分量)
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
- POJ2186 Popular Cows 强连通分量
- POJ2186 Popular Cows【Kosaraju】【强连通分量】
- POJ2186 Popular Cows [强连通分量|缩点]
- POJ2186_Popular cows_强连通分量_Korasaju算法
- POJ2186 Popular Cows【Tarjan】【强连通分量】
- POJ2186 Popular Cows(强连通分量)
- POJ2186 Popular Cows(强连通分量)
- poj2186 Popular Cows(强连通分量tarjan)
- poj2186 Popular Cows--Kosaraju算法 & 缩点 & 强连通分量
- POJ2186-Popular Cows(流行的奶牛)【tarjan,强连通分量,图论】
- POJ2186 Popular Cows 【图论】【强连通分量】
- 强连通分量tarjan缩点——POJ2186 Popular Cows
- poj2186 Popular Cows(强连通分量)(korasaju||tarjan模板题)
- poj2186 - Popular Cows
- POJ2186 Popular Cows ,有向图, Tarjan算法