http://poj.org/problem?id=2186

tarjan求强连通分量。

因为SD省选用WinXP+Cena评测而且不开栈,所以dfs只好写手动栈了。

写手动栈时思路清晰一点应该是不会出错的吧。。。

这里tarjan要开两个栈,一个是tarjan用来记录强连通分量的栈,另一个是记录dfs路径的栈。

cur记录当前弧,fa记录dfs树上的father。

对于扫到一个环不需要dfs下去的情况就直接处理,对于需要dfs下去的情况就把下一个点压入栈,记录这下一个点的fa为当前点,同时当前点的cur向下一个位置跳一下。

dfs时经常需要当前点dfs所有它的dfs树上的孩子然后统计它的孩子的信息,如果写手动栈记录哪个后继节点被dfs了需要统计信息和哪个后继节点没有被dfs(这只是图上的情况,树上显然所有后继节点都必须被dfs)不需要统计信息就非常麻烦了。

有一个比较巧的做法是记录上述的fa,如果一个点的当前弧都跳完了,说明这个点的信息都处理好了,然后把这个点的信息传到它的fa上,它的fa就不需要记录哪个后继节点需要统计信息哪个后继节点不需要统计信息了。

#include<cstdio>
#include<bitset>
#include<cstring>
#include<algorithm>
using namespace std; const int N = 10003;
const int M = 50003; bitset <N> inst;
struct node {int nxt, to;} E[M];
int dfn[N], low[N], tot = 0, fa[N], bel[N], sta[N], statop = 0, st[N], top, cnt = 0, point[N], cur[N], n, m; void ins(int u, int v) {E[++cnt] = (node) {point[u], v}; point[u] = cnt;} void tarjan(int x) {
st[top = 1] = x;
while (top) {
int u = st[top];
if (!dfn[u]) {
inst[u] = 1; sta[++statop] = u;
low[u] = dfn[u] = ++cnt;
}
if (cur[u]) {
int v = E[cur[u]].to;
if (inst[v]) low[u] = min(low[u], dfn[v]);
else if (!dfn[v]) fa[st[++top] = v] = u;
cur[u] = E[cur[u]].nxt;
} else {
low[fa[u]] = min(low[fa[u]], low[u]);
if (low[u] == dfn[u]) {
++tot;
while (sta[statop] != u) {
bel[sta[statop]] = tot;
inst[sta[statop]] = 0;
--statop;
}
inst[u] = 0;
bel[u] = tot; --statop;
}
--top;
}
}
} int du[N]; int main() {
int u, v;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &u, &v);
ins(u, v);
}
for (int i = 1; i <= n; ++i)
cur[i] = point[i]; cnt = 0;
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i); for (int i = 1; i <= n; ++i)
for (int j = point[i]; j; j = E[j].nxt)
if (bel[i] != bel[E[j].to])
++du[bel[i]]; int mark = 0;
for (int i = 1; i <= tot; ++i)
if (du[i] == 0)
if (mark == 0)
mark = i;
else
{puts("0"); return 0;} int ret = 0;
for (int i = 1; i <= n; ++i)
if (bel[i] == mark)
++ret;
printf("%d\n", ret);
return 0;
}

【POJ 2186】Popular Cows的更多相关文章

  1. 【2186】Popular Cows(强连通分支及其缩点)

    id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS   Memory Limit: 65536 ...

  2. 【POJ 2182】Lost Cows

    [题目链接] http://poj.org/problem?id=2182 [算法] 树状数组 + 二分 [代码] #include <algorithm> #include <bi ...

  3. POJ 2186:Popular Cows Tarjan模板题

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25945   Accepted: 10612 De ...

  4. 【POJ - 2456】Aggressive cows(二分)

    Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...

  5. bzoj 2295: 【POJ Challenge】我爱你啊

    2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec  Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...

  6. 【链表】BZOJ 2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 382  Solved: 111[Submit][S ...

  7. BZOJ2288: 【POJ Challenge】生日礼物

    2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 284  Solved: 82[Submit][St ...

  8. BZOJ2293: 【POJ Challenge】吉他英雄

    2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 80  Solved: 59[Submit][Stat ...

  9. BZOJ2287: 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 254  Solved: 140[Submit][S ...

随机推荐

  1. Vuejs - 强大的指令系统

    在 Vuejs 中,指令(Directives)是带有 v- 前缀的特殊属性.指令属性的预期值是 单个 Javascript 表达式(v-for 是个例外).指令的职责是,当表达式改变时,将其产生的连 ...

  2. Shuffle Cards(牛客第三场+splay)

    题目: 题意:将1~n的数进行m次操作,每次操作将第pi位到pi+si-1位的数字移到第一位,求最后的排列. 思路:现在还没不会写splay,在知道这是splay模板题后找了一波别人的模板,虽然过了, ...

  3. bzoj 1030 ac自动机

    比较容易看出来先建立ac自动机,然后在自动机上做DP,设w[0..1][i][j]为当前不包括/包括字典中的字符串,当前在自动机中走到第i个节点,完成的文本的长度为j的方案数,那么比较容易的转移w[i ...

  4. 在Linux中使用C语言实现控制流保护(CFG)【转】

    转自:http://www.codesec.net/view/537311.html 一.前言 最近版本的windows有一个新的缓解措施叫做控制流保护(CFG).在一个非直接调用之前――例如,函数指 ...

  5. kernel defconfig

    Some defconfig files are placed on below path. Only one *_defconfig can be selected. android/kernel/ ...

  6. 64_l4

    libnormaliz-devel-3.1.4-2.fc26.i686.rpm 23-May-2017 00:24 31214 libnormaliz-devel-3.1.4-2.fc26.x86_6 ...

  7. FineReport——自定义登录页

    统一的接口: http://localhost:8075/WebReport/ReportServer?op=fs_load&cmd=sso&fr_username=XX&fr ...

  8. P2511 [HAOI2008]木棍分割

    目录 Description Solution Code Description 有n根木棍, 第i根木棍的长度为Li,n根木棍依次连结了一起, 总共有n-1个连接处. 现在允许你最多砍断m个连接处, ...

  9. MySQL查找出重复的记录

    问题 查找表中多余的重复记录,重复记录是根据单个字段来判断的.例如:有张表中有uid和uname两个字段,现在需要查找出uname重复的所有数据列.数据表如下: id o_id uname 1 11 ...

  10. LeetCode218. The Skyline Problem

    https://leetcode.com/problems/the-skyline-problem/description/ A city's skyline is the outer contour ...