【DFS】COGS 967. 观光旅游

题目链接:http://cojs.tk/cogs/problem/problem.php?pid=967

分析:
树结构,首先任取一个节点,用深搜构造一棵树。

由于蒟蒻不会邻接表,只有用vector了,还开了两个。。

然后深搜处理处理这棵树。

对于每个父节点,它的每个子节点都有取或不取两种情况,如果子节点能取到的最大的值大于零,那么就取,否则舍弃。而对于这个父节点,如果它能取到的最大值大于零,那么将这个值向上传递,否则返回0,代表不取。

另外由于最优解可能取不到根节点,所以要在过程中不断记录更新最大值,最后输出最大值,而不一定是根节点的值。

代码:

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
vector<int> g[200001],g1[200001]; 
bool b[200001]={false};
int a[200001],n,mx=-1000000;
void make_tree(int i)   //递归建树
{
	int hi=g[i].size();
	for (int j=0;j<hi;j++)
	{
		if (b[g[i][j]]!=true)  //如果已经在树中,舍去
		 {
		  b[g[i][j]]=true; 
		  g1[i].push_back(g[i][j]); 
	          make_tree(g[i][j]);
		 }
	}
}
int work(int i)  //递归求解
{
	int hi=g1[i].size();
	if (hi==0)
	{
	 mx=max(mx,a[i]); // 不断更新最大值
	 return max(a[i],0);
    }
	int ans=a[i];
	for (int j=0;j<hi;j++) 
	{
	    ans+=work(g1[i][j]);
		mx=max(mx,ans);
   } 
	return max(ans,0);
}
int main()
{
	freopen("photo.in","r",stdin);
	freopen("photo.out","w",stdout);
	cin>>n;
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<n;i++)
	 {
	 	int aa,bb;
	 	scanf("%d%d",&aa,&bb);
	 	g[aa].push_back(bb);
	 	g[bb].push_back(aa);
	 }
	 b[1]=true;
    make_tree(1);
    cout<<max(work(1),mx);
return 0;	    
}

以下是将代码修改为cot平滑的方法: 1. 首先,需要使用边界角的cot权重计算每个顶点的权重。 2. 然后,使用cot权重对每个顶点的邻域点进行加权计算,得到平滑后的坐标。 3. 最后,根据平滑后的坐标更新每个顶点的位置。 修改后的代码如下: float smooth() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); //cot平滑 for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = weight_sum = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); weight_sum += cot_weight; } cogs.push_back(cog / weight_sum); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } 其中cot函数的定义如下: double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } 注意,这里使用的是边界角的cot权重,因此在计算cot权重时需要判断当前边是否为边界。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值