换根dp 2021江西 gym103366I. Homework

这篇博客探讨了两个与树形结构相关的算法问题。第一个问题是关于求解在一个由正整数构成的序列中,对于连续子序列,找到能最大化分割数的最大质因数。第二个问题是寻找一棵树的根节点,使得以该节点为根时,所有节点的深度之和最大。这两个问题都涉及到深度优先搜索(DFS)的应用,通过递归地遍历树的节点来计算和优化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I. Homework

Tsiying has a sequence of positive integers with a length of n and quickly calculates all the factors of each number. In order to exercise his factor calculation ability, he has selected q consecutive subsequences from the sequence and found a positive integer p greater than 1 for each subsequence, so that p can divide as many numbers in this subsequence as possible. He has also found that p​ may have more than one.
So the question is, how many numbers in each subsequence can be divided at most?
Input
The first line contains an integer T (1≤T≤5×104), indicating that there is T​​ test cases next.
The first line of each test cases has two positive integers n​ (1≤n≤5×104​), q (1≤q≤5×104)​​​.
Next line n integers ai (1≤i≤n,1≤ai≤1×106), which representing the numbers in this sequence. The two adjacent numbers are separated by a space.
Each of the next q lines contains two integers l,r (1≤l≤r≤n), representing a subsequence being queried, al,al+1,⋯,ar, and l,r are separated by a space.
The input guarantees that the sum of n​ does not exceed 5×104​ and the sum of q​ does not exceed 5×104​.
Output
For each test case, output q lines, each line contains a positive integer, indicating the answer.

有n个人组成有n-1条边的无根树,点权代表自己做作业的时间,边权代表去抄作业需要花的时间。
问让所有人都做完作业需要花费的时间。
f [ u ] = m a x ( w [ u ] , f [ v ] + d ( u , v ) ) f[u]=max(w[u], f[v]+d(u,v)) f[u]=max(w[u],f[v]+d(u,v))
换根dp。

#include<bits/stdc++.h>
using namespace std;

#define read(x) scanf("%d",&x)
#define maxn int(1e5)
#define ll long long

struct Edge{
	int y,z;
	Edge(){}
	Edge(int _y,int _z) {y=_y,z=_z;}
};

struct ID{
	int x,y,p1,p2;
	ID() {}
	ID(int _x,int _y,int _p1,int _p2) {x=_x,y=_y,p1=_p1,p2=_p2;} 
};

int n,q;
int w[maxn+5];
vector<Edge> a[maxn+5];
ID id[maxn+5];	//第i条边在vector中的位置 

ll f[maxn+5];	//完成作业的最小时间 

void dfs1(int x,int fa) {
	f[x]=w[x];	//自己做作业
	for(int i=0;i<a[x].size();i++) {
		int y=a[x][i].y,z=a[x][i].z;
		if(y==fa) continue;
		dfs1(y,x);
		f[x]=min(f[x],f[y]+z);
	} 
}

void dfs2(int x,int fa) {
	for(int i=0;i<a[x].size();i++) {
		int y=a[x][i].y,z=a[x][i].z;
		if(y==fa) continue;
		f[y]=min(f[y],f[x]+z);
		dfs2(y,x);
	}
}

int main() {
	read(n),read(q);
	for(int i=1;i<=n;i++) read(w[i]);
	for(int i=1;i<n;i++) {
		int x,y,z;
		read(x),read(y),read(z);
		a[x].push_back(Edge(y,z));
		a[y].push_back(Edge(x,z));
		id[i]=ID(x,y,a[x].size()-1,a[y].size()-1);
	}
	
	while(q--) {
		int opr;
		read(opr);
		if(opr==1) {
			int x,d;
			read(x),read(d);
			w[x]=d; 
		} else if(opr==2) {
			int e,d;
			read(e),read(d);
			int x=id[e].x,y=id[e].y,p1=id[e].p1,p2=id[e].p2;
			a[x][p1].z=d,a[y][p2].z=d;
		} else {
			dfs1(1,0);
			dfs2(1,0);
			ll ans=0;
			for(int i=1;i<=n;i++) ans^=f[i];
			printf("%lld\n",ans);
		} 
	}
	
	
	return 0;
}

洛谷P3478 [POI2008]STA-Station

给定一个 nn 个点的树,请求出一个结点,使得以这个结点为根时,所有结点的深度之和最大。
一个结点的深度之定义为该节点到根的简单路径上边的数量。

#include<bits/stdc++.h>
using namespace std;

#define read(x) scanf("%d",&x)
#define maxn int(1e6)
#define ll long long

int n;
vector<int> a[maxn+5];

ll sz[maxn+5],d[maxn+5],f[maxn+5];	//子树的大小,对于节点1的深度,答案 

void dfs1(int x,int fa) {
	sz[x]=1;
	for(int i=0;i<a[x].size();i++) {
		int y=a[x][i];
		if(y==fa) continue;
		d[y]=d[x]+1;
		dfs1(y,x);
		sz[x]+=sz[y];
	}
}

void dfs2(int x,int fa) {	//当前x为新根 
	for(int i=0;i<a[x].size();i++) {
		int y=a[x][i];
		if(y==fa) continue;
		f[y]=f[x]+(n-sz[y])-sz[y];
		dfs2(y,x); 
	}
}

int main() {
	read(n);
	for(int i=1;i<n;i++) {
		int x,y;
		read(x),read(y);
		a[x].push_back(y);
		a[y].push_back(x);
	}
	
	d[1]=1;
	dfs1(1,0);
	for(int i=1;i<=n;i++) f[1]+=d[i];
	dfs2(1,0);
	
	int ans=1;
	ll m=-1;
	for(int i=1;i<=n;i++) {
		if(f[i]>m) {
			m=f[i],ans=i;
		}
	}
	
	printf("%d",ans);
	
	return 0;
}
### 使用 `gym.spaces.Box` 定义动作空间 在OpenAI Gym环境中定义连续的动作空间通常会使用到 `gym.spaces.Box` 类。此类允许创建一个多维的盒子形状的空间,其边界由低限(low)和高限(high)参数指定[^1]。 对于给定的例子,在类 `ActionSpace` 中静态方法 `from_type` 返回了一个基于输入类型的行动空间实例: 当 `space_type` 是 `Continuous` 时,返回的是一个三维向量形式的动作空间对象,该对象表示三个维度上的取值范围分别为 `[0.0, 1.0]`, `[0.0, 1.0]`, 和 `[-1.0, 1.0]` 的实数集合,并且数据类型被设定为了 `np.float32`: ```python import numpy as np import gym class ActionSpace: @staticmethod def from_type(action_type: int): space_type = ActionSpaceType(action_type) if space_type == ActionSpaceType.Continuous: return gym.spaces.Box( low=np.array([0.0, 0.0, -1.0]), high=np.array([1.0, 1.0, 1.0]), dtype=np.float32, ) ``` 此段代码展示了如何通过传递最低限度(`low`)数组以及最高限度(`high`)数组来初始化一个新的Box实例,从而构建出一个具有特定界限的多维连续数值区间作为环境可能采取的一系列合法行为的选择集的一部分。 另外值得注意的是,每个环境都应当具备属性 `action_space` 和 `observation_space` ,这两个属性应该是继承自 `Space` 类的对象实例;Gymnasium库支持大多数用户可能会需要用到的不同种类的空间实现方式[^2]。 #### 创建并测试 Box 动作空间的一个简单例子 下面是一个简单的Python脚本片段用于展示怎样创建和验证一个基本的 `Box` 空间成员资格的方法: ```python def check_box_space(): box_space = gym.spaces.Box(low=-1.0, high=1.0, shape=(2,), dtype=np.float32) sample_action = box_space.sample() # 获取随机样本 is_valid = box_space.contains(sample_action) # 检查合法性 print(f"Sampled action {sample_action} within bounds? {'Yes' if is_valid else 'No'}") check_box_space() ``` 上述函数首先建立了一个二维的 `-1.0` 到 `1.0` 范围内的浮点型 `Box` 空间,接着从中抽取了一组随机样本来检验它确实位于所规定的范围内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值