Aizu 2222

来源:互联网 发布:马克思主义中国化 知乎 编辑:程序博客网 时间:2024/05/18 11:50
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
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 42 33 44 35 4

Output for the Sample Input 1

10

Sample Input 2

5 51 22 33 44 55 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 1000000007int 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;}


原创粉丝点击