弯曲的手指

Time Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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
S
1D1
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值