【BZOJ 1051】【HAOI 2006】受欢迎的牛

tarjan缩点模板

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10003;
const int M = 50003;
void read(int &k) {
k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 1) + (k << 3) + c - '0';
k = k * fh;
} struct node {
int from, nxt, to;
}E[M];
bool vis[N], inst[N];
int point[N], ans = 0, color = 0, col[N], in[N], out[N], sum[N], cnt = 0, n, m, DFN[N], low[N], st[N], top = 0;
void ins(int x, int y) {E[++cnt] = (node) {x, point[x], y}; point[x] = cnt;}
void tarjan(int x) {
DFN[x] = low[x] = ++cnt;
st[++top] = x; inst[x] = vis[x] = 1;
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt)
if (!vis[E[tmp].to])
tarjan(E[tmp].to), low[x] = min(low[x], low[E[tmp].to]);
else if (inst[E[tmp].to]) low[x] = min(low[x], DFN[E[tmp].to]);
if (DFN[x] == low[x]) {
++color;
int u = 0;
while (u != x) {
u = st[top--];
col[u] = color;
inst[u] = 0;
++sum[color];
}
}
} int main() {
read(n); read(m);
int u, v;
for(int i = 1; i <= m; ++i) {read(u); read(v); ins(u, v);}
cnt = 0;
tarjan(1);
for(int i = 1; i <= m; ++i) {
u = col[E[i].from]; v = col[E[i].to];
if (u != v) ++out[u];
}
for(int i = 1; i <= color; ++i)
if (out[i] == 0) {
if (ans == 0) ans = sum[i];
else {ans = 0; break;}
}
printf("%d\n", ans);
return 0;
}

现在才学是不是太晚了= =

上一篇:中国(北方)大学生程序设计训练赛(第一周) (D E)


下一篇:Sagit.Framework For IOS 开发框架入门教程3:Start引导页及框架布局和隐藏事件的内幕