CodeForces 196C.Paint Tree(分治+极角排序)
Posted fxkkks
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 (n) vertexes and (n) 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)) — the number of vertexes on a tree (as well as the number of chosen points on the plane).
Each of the next (n-1) lines contains two space-separated integers $u_i $and (v_i (1?≤?u_i,?v_i?≤?n, u_i?≠?v_i)) — the numbers of tree vertexes connected by the (i)-th edge.
Each of the next (n) lines contain two space-separated integers $x_i (and) y_i (?-?10^9?≤?x_i,?y_i?≤?10^9)$ — the coordinates of the (i)-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 (n) distinct space-separated integers from (1) to (n): the (i)-th number must equal the number of the vertex to place at the (i)-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.
Solution
对于每一棵树,以最左下角的点作为该树的根,进行极角排序,再根据该树每棵子树的大小分配点的数量,进行递归。由于不存在三点共线所以解是一定存在的。
如图所示:
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
", ans[n]);
return 0;
}
以上是关于CodeForces 196C.Paint Tree(分治+极角排序)的主要内容,如果未能解决你的问题,请参考以下文章
PHP PHPBBForumlarındaKullanıcılarıŞifresiniTopluHa