您的位置:首页 > 大数据 > 人工智能

Aizu 2222

2013-09-13 10:43 155 查看
Time Limit:8000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status Practice Aizu
2222
Description

Natsuki and her friends were taken to the space by an alien and made friends with a lot of aliens. During the space travel, she discovered that aliens’ hands were often very different from humans’. Generally speaking, in a kind of aliens, there are N fingers
and M bend rules on a hand. Each bend rule describes that a finger A always bends when a finger B bends. However, this rule does not always imply that the finger B bends when the finger A bends.

When she were counting numbers with the fingers, she was anxious how many numbers her alien friends can count with the fingers. However, because some friends had too complicated rule sets, she could not calculate those. Would you write a program for her?

Input

N M

S1D1

S2D2
.

.

.

SMDM

The first line contains two integers N and M (1 ≤ N ≤ 1000, 0 ≤ M ≤ 1000) in this order. The following M lines mean bend rules. Each line contains two integers Si and Di in
this order, which mean that the finger Di always bends when the finger Si bends. Any finger appears at most once in S.

Output

Calculate how many numbers her alien friends can count with the fingers. Print the answer modulo 1000000007 in a line.

Sample Input 1

5 4
2 3
3 4
4 3
5 4

Output for the Sample Input 1

10

Sample Input 2

5 5
1 2
2 3
3 4
4 5
5 1

Output for the Sample Input 2

2

Sample Input 3

5 0

Output for the Sample Input 3

32

#include <iostream>
#include <string.h>

using namespace std;

#define MAXN 1001
#define mod 1000000007

int n, m;

struct Edge
{
int v, next;
}edge[MAXN];

int head[MAXN];
int dfn[MAXN], low[MAXN], col[MAXN], ind[MAXN], st[MAXN];
int indexs, countn, e, to;
bool ins[MAXN];
int c[MAXN][MAXN];

void add(int u, int v)
{
edge[e].v = v;
edge[e].next = head[u];
head[u] = e++;
}

void init()
{
e = 0;
indexs = 0;
countn = 1;
to = 0;
memset(head, -1, sizeof(head));
memset(dfn, -1, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(col, 0, sizeof(col));
memset(ins, false, sizeof(ins));
memset(c, 0, sizeof(c));
}

void tarjan(int u)
{
low[u] = dfn[u] = indexs++;
st[++to] = u;
ins[u] = true;

for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if (dfn[v] == -1)
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
{
low[u] = min(low[u], dfn[v]);
}
}

if (dfn[u] == low[u])
{
int temp;
do
{
temp = st[to--];
ins[temp] = false;
col[temp] = countn;
}while (temp != u);
countn++;
}
}

long long dfs(int u)
{
long long res = 1, temp;

for (int i = 1; i < countn; i++)
{
if (c[u][i] == 1 && i != u)
{
temp = dfs(i);
res = (res * temp) % mod;
}
}

res = (res + 1) % mod;

return res;
}

void solve()
{
for (int i = 1; i <= n; i++)
{
if (dfn[i] == -1)
{
tarjan(i);
}
}

for (int i = 1; i <= n; i++)
{
for (int j = head[i]; j != -1; j = edge[j].next)
{
int v = edge[j].v;
if (col[v] != col[i])
{
ind[col[i]]++;
c[col[v]][col[i]] = 1;
}
}
}

long long ans = 1, temp;

for (int i = 1; i < countn; i++)
{
if (!ind[i])
{
int temp = dfs(i);
ans = (ans * temp) % mod;
}
}

cout << ans << endl;
}

void input()
{
int u, v;

while (cin >> n >> m)
{
init();

for (int i = 0; i < m; i++)
{
cin >> u >> v;
add(u, v);
}

solve();
}
}

int main()
{
std::ios::sync_with_stdio(false);
input();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: