# Iowa_Battleship

## BZOJ1051或洛谷2341 [HAOI2006]受欢迎的牛

### 洛谷原题链接

#include<cstdio>
using namespace std;
const int N = 1e4 + 10;
const int M = 5e4 + 10;
struct eg {
int x, y;
};
eg a[M];
int fi[N], di[M], ne[M], dfn[N], low[N], sta[N], bl[N], si[N], chu[N], l, ti, tp, SCC;
bool v[N];
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '0';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline void add(int x, int y)
{
di[++l] = y;
ne[l] = fi[x];
fi[x] = l;
}
inline int minn(int x, int y)
{
return x < y ? x : y;
}
void tarjan(int x)
{
int i, y;
dfn[x] = low[x] = ++ti;
sta[++tp] = x;
v[x] = 1;
for (i = fi[x]; i; i = ne[i])
if (!dfn[y = di[i]])
{
tarjan(y);
low[x] = minn(low[x], low[y]);
}
else
if (v[y])
low[x] = minn(low[x], dfn[y]);
if (!(dfn[x] ^ low[x]))
{
SCC++;
do
{
y = sta[tp--];
bl[y] = SCC;
v[y] = 0;
si[SCC]++;
} while (x ^ y);
}
}
int main()
{
int i, n, m, x, y, an = 0;
n = re();
m = re();
for (i = 1; i <= m; i++)
{
a[i].x = re();
a[i].y = re();
}
for (i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
for (i = 1; i <= m; i++)
{
x = bl[a[i].x];
y = bl[a[i].y];
if (x ^ y)
chu[x]++;
}
for (i = 1; i <= SCC; i++)
if (!chu[i])
{
if (an)
{
printf("0");
return 0;
}
an = si[i];
}
printf("%d", an);
return 0;
}

posted on 2018-09-24 21:22  Iowa_Battleship  阅读(...)  评论(... 编辑 收藏

• 随笔 - 172
• 文章 - 0
• 评论 - 17
• 引用 - 0