Aizu 2222

本文介绍了一种针对外星生物特殊手指弯曲规则的计数算法。通过分析手指间的依赖关系,利用Tarjan算法找到强连通分量,并进一步计算在特定规则下可表示的不同数字数量。
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;
}


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值