码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-10 18:03:43      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

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

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

标签:

原文地址:http://www.cnblogs.com/abclzr/p/5478418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!