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?
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.
& %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 MS1D1
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; }
相关文章推荐
- 1.求2+22+222+2222+…+22…22(精确计算).
- [Hdu 2222] 字符串(三) {Aho-Corasick自动机}
- hdu 2222 Keywords Search(AC自动机入门题)
- HDU 2222 Keywords Search(AC自动机)
- hdu 2222 Keywords Search
- HDU 2222 Keywords Search
- hdu 2222 Keywords Search
- dfa - hdu 2222 AC自动机示例
- hdu 2222 ac自动机
- H264 RTP打包发送 2222
- Aizu 解题报告索引
- HDU 2222 Keywords Search (AC 自动机入门)
- hdu 2222 Keywords Search(AC自动机入门题)
- hdu 2222 ac自动机 模式串计数
- Hdu 2222 Keywords Search [AC自动机多模匹配]
- HDU 2222 Keywords Search(AC自动机)
- Aizu 2249 单源最短路变形 spfa模板改写
- HDU 2222 最简单的AC自动机套模板应用
- BZOJ2222 [Cqoi2006]猜数游戏
- C语言求S(n) = a+aa+aaa+aaaa+...+aa..a之值,其中a是一个数字,n表示a的位数例如:2+22+222+2222+22222(此时n=5),n和a都从键盘输入。