您的位置:首页 > 其它

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

2016-05-10 16:54 253 查看
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
, inst
;
int point
, ans = 0, color = 0, col
, in
, out
, sum
, cnt = 0, n, m, DFN
, low
, st
, 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;
}


现在才学是不是太晚了= =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: