E. Xenon's Attack on the Gangs,Codeforces Round #614 (Div. 2),树形dp

E. Xenon’s Attack on the Gangs

http://codeforces.com/contest/1293/problem/E
On another floor of the A.R.C. Markland-N, the young man Simon “Xenon” Jackson, takes a break after finishing his project early (as always). Having a lot of free time, he decides to put on his legendary hacker “X” instinct and fight against the gangs of the cyber world.

His target is a network of n small gangs. This network contains exactly n−1 direct links, each of them connecting two gangs together. The links are placed in such a way that every pair of gangs is connected through a sequence of direct links.

By mining data, Xenon figured out that the gangs used a form of cross-encryption to avoid being busted: every link was assigned an integer from 0 to n−2 such that all assigned integers are distinct and every integer was assigned to some link. If an intruder tries to access the encrypted data, they will have to surpass S password layers, with S being defined by the following formula:

S=∑1≤u<v≤nmex(u,v)
Here, mex(u,v) denotes the smallest non-negative integer that does not appear on any link on the unique simple path from gang u to gang v.

Xenon doesn’t know the way the integers are assigned, but it’s not a problem. He decides to let his AI’s instances try all the passwords on his behalf, but before that, he needs to know the maximum possible value of S, so that the AIs can be deployed efficiently.

Now, Xenon is out to write the AI scripts, and he is expected to finish them in two hours. Can you find the maximum possible S before he returns?

思路:首先,如果一条边的权值为0,那么所有经过这条边的路径权值都会加1,如果再将其相邻的一条边的权值赋为1,则所有经过这0,1两条边的路径权值都会再加上1,而其他路径不会有贡献,即只有连续的一条链有贡献(链的赋值为0,1,…,x)
现可以枚举所有链,设这条链为u,u1,…,v1,v,设长度为x,每条边的赋值有0,1,…,x-1,经过整条链增加的贡献很容易算出来,现考虑将链长缩短,有两种方式,即x-1赋给边(u,u1)或者(v,v1),对这两种赋值方法的贡献取max即可,可以每次将结果记录下来,复杂度为O( n 2 n^2 n2)

#include<bits/stdc++.h>
#define ll long long
#define MAXN 3005
using namespace std;
ll dp[MAXN][MAXN],sz[MAXN][MAXN];
int fa[MAXN][MAXN],head[MAXN],tot;
struct edge
{
	int v,nxt;
}edg[MAXN << 1];
inline void addedg(int u,int v)
{
	edg[tot].v = v;
	edg[tot].nxt = head[u];
	head[u] = tot++;
}
int n,rt;
inline void dfs(int u,int f)
{
	int v;
	sz[rt][u] = 1;
	fa[rt][u] = f;
	for(int i = head[u];i != -1;i = edg[i].nxt)
	{
		v = edg[i].v;
		if(v != f)
		{
			dfs(v,u);
			sz[rt][u] += sz[rt][v];
		}
	}
}
inline ll solve(int u,int v)
{
	if(dp[u][v] != -1) return dp[u][v];
	if(u == v) return 0;
	return dp[u][v] = sz[u][v] * sz[v][u] + max(solve(u,fa[u][v]),solve(v,fa[v][u]));
}
inline void init()
{
	tot = 0;
	memset(head,-1,sizeof(head));
	memset(dp,-1,sizeof(dp));
}
int main()
{
	while(~scanf("%d",&n))
	{
		init();
		int u,v;
		for(int i = 1;i < n;++i)
		{
			scanf("%d%d",&u,&v);
			addedg(u,v);
			addedg(v,u);
		}
		for(int i = 1;i <= n;++i)
			rt = i,dfs(i,i);
		ll ans = 0;
		for(int i = 1;i <= n;++i)
			for(int j = 1;j <= n;++j)
				ans = max(ans,solve(i,j));
		printf("%lld\n",ans);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Tensorflow、OpenAI搭建的强化学习框架,训练机器自动操盘 强化学习(Reinforcement Learning, RL),又称再励学习、评价学习或增强学习,是机器学习的范式和方法论之一。它主要用于描述和解决智能体(agent)在与环境的交互过程中通过学习策略以达成回报最大化或实现特定目标的问题。强化学习的特点在于没有监督数据,只有奖励信号。 强化学习的常见模型是标准的马尔可夫决策过程(Markov Decision Process, MDP)。按给定条件,强化学习可分为基于模式的强化学习(model-based RL)和无模式强化学习(model-free RL),以及主动强化学习(active RL)和被动强化学习(passive RL)。强化学习的变体包括逆向强化学习、阶层强化学习和部分可观测系统的强化学习。求解强化学习问题所使用的算法可分为策略搜索算法和值函数(value function)算法两类。 强化学习理论受到行为主义心理学启发,侧重在线学习并试图在探索-利用(exploration-exploitation)间保持平衡。不同于监督学习和非监督学习,强化学习不要求预先给定任何数据,而是通过接收环境对动作的奖励(反馈)获得学习信息并更新模型参数。强化学习问题在信息论、博弈论、自动控制等领域有得到讨论,被用于解释有限理性条件下的平衡态、设计推荐系统和机器人交互系统。一些复杂的强化学习算法在一定程度上具备解决复杂问题的通用智能,可以在围棋和电子游戏中达到人类水平。 强化学习在工程领域的应用也相当广泛。例如,Facebook提出了开源强化学习平台Horizon,该平台利用强化学习来优化大规模生产系统。在医疗保健领域,RL系统能够为患者提供治疗策略,该系统能够利用以往的经验找到最优的策略,而无需生物系统的数学模型等先验信息,这使得基于RL的系统具有更广泛的适用性。 总的来说,强化学习是一种通过智能体与环境交互,以最大化累积奖励为目标的学习过程。它在许多领域都展现出了强大的应用潜力。
尝试用基于值函数逼近的强化学习方法玩经典的马里奥游戏,取得了一定成果 强化学习(Reinforcement Learning, RL),又称再励学习、评价学习或增强学习,是机器学习的范式和方法论之一。它主要用于描述和解决智能体(agent)在与环境的交互过程中通过学习策略以达成回报最大化或实现特定目标的问题。强化学习的特点在于没有监督数据,只有奖励信号。 强化学习的常见模型是标准的马尔可夫决策过程(Markov Decision Process, MDP)。按给定条件,强化学习可分为基于模式的强化学习(model-based RL)和无模式强化学习(model-free RL),以及主动强化学习(active RL)和被动强化学习(passive RL)。强化学习的变体包括逆向强化学习、阶层强化学习和部分可观测系统的强化学习。求解强化学习问题所使用的算法可分为策略搜索算法和值函数(value function)算法两类。 强化学习理论受到行为主义心理学启发,侧重在线学习并试图在探索-利用(exploration-exploitation)间保持平衡。不同于监督学习和非监督学习,强化学习不要求预先给定任何数据,而是通过接收环境对动作的奖励(反馈)获得学习信息并更新模型参数。强化学习问题在信息论、博弈论、自动控制等领域有得到讨论,被用于解释有限理性条件下的平衡态、设计推荐系统和机器人交互系统。一些复杂的强化学习算法在一定程度上具备解决复杂问题的通用智能,可以在围棋和电子游戏中达到人类水平。 强化学习在工程领域的应用也相当广泛。例如,Facebook提出了开源强化学习平台Horizon,该平台利用强化学习来优化大规模生产系统。在医疗保健领域,RL系统能够为患者提供治疗策略,该系统能够利用以往的经验找到最优的策略,而无需生物系统的数学模型等先验信息,这使得基于RL的系统具有更广泛的适用性。 总的来说,强化学习是一种通过智能体与环境交互,以最大化累积奖励为目标的学习过程。它在许多领域都展现出了强大的应用潜力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值