CodeForces 196C.Paint Tree(分治+极角排序)

本文介绍了一种在平面上绘制树状结构的算法。该算法确保了树的边不会相交,并且每个顶点对应平面上的一个点。通过递归地分配点的位置并进行极角排序,可以有效地解决绘制问题。

C. Paint Tree

time limit per test 2 seconds
memory limit per test 256 megabytes

You are given a tree with nnn vertexes and nnn points on a plane, no three points lie on one straight line.

Your task is to paint the given tree on a plane, using the given points as vertexes.

That is, you should correspond each vertex of the tree to exactly one point and each point should correspond to a vertex. If two vertexes of the tree are connected by an edge, then the corresponding points should have a segment painted between them. The segments that correspond to non-adjacent edges, should not have common points. The segments that correspond to adjacent edges should have exactly one common point.

Input
The first line contains an integer n(1 ≤ n ≤ 1500)n (1 ≤ n ≤ 1500)n(1n1500) — the number of vertexes on a tree (as well as the number of chosen points on the plane).

Each of the next n−1n-1n1 lines contains two space-separated integers $u_i $and vi(1 ≤ ui, vi ≤ n,ui ≠ vi)v_i (1 ≤ u_i, v_i ≤ n, u_i ≠ v_i)vi(1ui,vin,ui̸=vi) — the numbers of tree vertexes connected by the iii-th edge.

Each of the next nnn lines contain two space-separated integers $x_i andandand y_i ( - 10^9 ≤ x_i, y_i ≤ 10^9)$ — the coordinates of the iii-th point on the plane. No three points lie on one straight line.

It is guaranteed that under given constraints problem has a solution.

Output
Print nnn distinct space-separated integers from 111 to nnn: the iii-th number must equal the number of the vertex to place at the iii-th point (the points are numbered in the order, in which they are listed in the input).

If there are several solutions, print any of them.

Examples
input1

3
1 3
2 3
0 0
1 1
2 0

output1

1 3 2

input2

4
1 2
2 3
1 4
-1 -2
3 5
-3 3
2 0

output2

4 2 1 3

Note
The possible solutions for the sample are given below.

p1

p2

Solution

对于每一棵树,以最左下角的点作为该树的根,进行极角排序,再根据该树每棵子树的大小分配点的数量,进行递归。由于不存在三点共线所以解是一定存在的。
如图所示:
p3

Code

#include <cstdio>
#include <algorithm>
#define N 1510
using namespace std;
typedef long long LL;

struct Node {
	LL to, nxt;
}e[N << 1];

struct Point {
	LL x, y, id;
}p[N], pp[N], poi;

LL cnt, n, lst[N], ans[N], size[N];

bool cmp(Point a, Point b) {//按极角排序
	LL ax = a.x - poi.x, ay = a.y - poi.y, bx = b.x - poi.x, by = b.y - poi.y;
	if (ax >= 0 && bx <= 0) return 1;
	if (ax <= 0 && bx >= 0) return 0;
	return (ax * by > bx * ay);
}

inline void add(LL u, LL v) {
	e[++cnt].to = v;
	e[cnt].nxt = lst[u];
	lst[u] = cnt;
}

void predfs(LL x, LL fa) {//预处理出子树大小
	size[x] = 1;
	for (int i = lst[x]; i; i = e[i].nxt) {
		if (e[i].to == fa) continue;
		predfs(e[i].to, x);
		size[x] += size[e[i].to];
	}
}

void dfs(LL l, LL r, LL x, LL fa)//对区间进行分治
	LL mini = l;
	for (int i = l + 1; i <= r; ++i)
		if (p[i].y < p[mini].y || p[i].y == p[mini].y && p[i].x < p[mini].x) mini = i;
	swap(p[l], p[mini]);
	ans[p[l].id] = x;
	poi = p[l];
	sort(p + l + 1, p + r + 1, cmp);
	LL now = l + 1;
	for (LL i = lst[x]; i; i = e[i].nxt) {
		if (e[i].to == fa) continue;
		dfs(now, now + size[e[i].to] - 1, e[i].to, x);
		now += size[e[i].to];
	}
}

int main() {
	scanf("%lld", &n);
	for (LL i = 1; i < n; ++i) {
		LL a, b;
		scanf("%lld%lld", &a, &b);
		add(a, b);
		add(b, a);
	}
	for (LL i = 1; i <= n; ++i) {
		scanf("%lld%lld", &p[i].x, &p[i].y);
		p[i].id = i;
	}
	predfs(1, 1);
	dfs(1, n, 1, 1);
	for (LL i = 1; i < n; ++i) {
		printf("%lld ", ans[i]);
	}
	printf("%lld\n", ans[n]);
	return 0;
}
【电能质量扰动】基于ML和DWT的电能质量扰动分类方法研究(Matlab实现)内容概要:本文研究了一种基于机器学习(ML)和离散小波变换(DWT)的电能质量扰动分类方法,并提供了Matlab实现方案。首先利用DWT对电能质量信号进行多尺度分解,提取信号的时频域特征,有效捕捉电压暂降、暂升、中断、谐波、闪变等常见扰动的关键信息;随后结合机器学习分类器(如SVM、BP神经网络等)对提取的特征进行训练与分类,实现对不同类型扰动的自动识别与准确区分。该方法充分发挥DWT在信号去噪与特征提取方面的优势,结合ML强大的模式识别能力,提升了分类精度与鲁棒性,具有较强的实用价值。; 适合人群:电气工程、自动化、电力系统及其自动化等相关专业的研究生、科研人员及从事电能质量监测与分析的工程技术人员;具备一定的信号处理基础和Matlab编程能力者更佳。; 使用场景及目标:①应用于智能电网中的电能质量在线监测系统,实现扰动类型的自动识别;②作为高校或科研机构在信号处理、模式识别、电力系统分析等课程的教学案例或科研实验平台;③目标是提高电能质量扰动分类的准确性与效率,为后续的电能治理与设备保护提供决策依据。; 阅读建议:建议读者结合Matlab代码深入理解DWT的实现过程与特征提取步骤,重点关注小波基选择、分解层数设定及特征向量构造对分类性能的影响,并尝试对比不同机器学习模型的分类效果,以全面掌握该方法的核心技术要点。
Codeforces Round 1036 是一场同时面向 Div.1 和 Div.2 参赛者的比赛,通常这类比赛会包含多个具有挑战性的编程题目,涵盖算法、数据结构、数学等多个领域。比赛的题解和题目信息可以帮助参赛者回顾解题思路,提升编程能力。 ### 比赛基本信息 - **比赛名称**:Codeforces Round #1036 (Div. 1 and Div. 2) - **比赛时间**:具体时间为 UTC+X(根据实际举办日期和时间表) - **比赛链接**:[Codeforces 官方页面](https://codeforces.com/contest/1343) - **题解发布位置**:通常在比赛结束后不久,官方或社区成员会在 Codeforces 博客、GitHub 或其他技术平台上发布题解。 ### 题目类型与难度分布 该轮比赛通常包括 5 到 7 道题目,难度从简单实现到复杂算法不等。例如: - **A题**:通常是简单的模拟或数学问题。 - **B题**:可能涉及字符串处理或基础贪心策略。 - **C题**:中等难度,可能需要掌握基本的数据结构如数组、排序等。 - **D题及以后**:较高难度,可能涉及图论、动态规划、数论等高级算法。 ### 参赛情况与亮点 - **参与人数**:通常超过 10,000 名选手参加。 - **热门话题**:比赛中某些题目可能会引发广泛讨论,尤其是那些需要用到巧妙构造或优化技巧的问题。 - **知名选手表现**:顶尖选手如 tourist、Um_nik 等通常会以快的速度完成所有题目,并占据排行榜前列。 ### 示例代码片段 以下是一个典型的 Codeforces 题目解法示例,适用于某道中等难度题目: ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long l, r; cin >> l >> r; // 假设 e 是一个预处理好的符合条件的数组 // 使用二分查找来统计区间 [l, r] 内的有效数字个数 long long ans = upper_bound(e.begin(), e.end(), r) - lower_bound(e.begin(), e.end(), l); cout << ans << endl; } return 0; } ``` ### 题解资源推荐 - **Codeforces 官方博客**:通常会有详细的题解和作者说明。 - **GitHub 仓库**:许多参赛者会将自己的解法上传至 GitHub,便于他人学习。 - **知乎专栏 / 优快云 / 博客园**:中文社区中也常有高质量的赛后总结与分析文章。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值