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\n", ans[n]);
    return 0;
}

原文地址:https://www.cnblogs.com/Fxkkks/p/9922451.html

时间: 2024-10-17 15:44:15

CodeForces 196C.Paint Tree(分治+极角排序)的相关文章

Codeforces 196C Paint Tree(贪心+极角排序)

题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点,把根结点放到这个点中,然后对剩下的点进行极角排序,按逆时顺序一个个塞进来,类似地递归处理. 这样就满足了题意. #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i(a); i <= (b

Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)

C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output 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

C. Nearest vectors--cf598C(极角排序)

http://codeforces.com/problemset/problem/598/C 题目大意: 给你你个向量  向量的起点都是从(0,0)开始的   求哪个角最小  输出这两个向量 这是第一次接触极角 一个函数图像上某一点到原点的距离就是极径,极径与x轴的夹角就是极角 按照极角排序   是从第三象限的最右边的开始是最小  一直逆时针转到第二象限的最右边 求极角差最小就行了 但是卡精度问题  把double换成long double   就好了  (我也不知道为什么) #include<

Hdu5032 极角排序+树状数组

题目链接 思路:参考了题解.对询问进行极角排序,然后用树状数组维护一下前缀和即可. /* ID: onlyazh1 LANG: C++ TASK: test */ #include<bits/stdc++.h> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 typedef long long ll; const int maxn=1010; const int maxm=10

Paint Tree

题意: 给定一棵n个点的树,给定平面上n个点,将n个点用线段连起来画成树的形状,使得不存在不在端点相交的线段,构造出一种情况. 解法: 首先观察我们常规画出来的树形图可知,树的子树是根据极角分开的,这样,我们每一次找到最靠左下的点, 而后对剩余点极角排序,根据子树大小和极角的连续关系将点集划分,依次递归即可. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cma

HDU Always Cook Mushroom (极角排序+树状数组)

Problem Description Matt has a company, Always Cook Mushroom (ACM), which produces high-quality mushrooms. ACM has a large field to grow their mushrooms. The field can be considered as a 1000 * 1000 grid where mushrooms are grown in grid points numbe

分治——合并排序

分治策略中有一个经典的算法就是合并排序,这个算法的精髓也是分治二字,分而治之.将一个大规模的问题分割成若干个同样的小问题,小问题的规模很小,很容易解决,解决了小的问题后再对这些小问题的结果进行合并得到大规模问题的解答. 合并排序便是分治策略中比较经典的算法,首先是合并,两个排列有序的数列经过合并后成为有序的数组:代码如下: void _merge(int *A,int left,int middle,int right) { int i=left,j=middle+1,k=0; int *C;

BZOJ 1132 [POI2008]Tro(极角排序)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1132 [题目大意] 平面上有N个点. 求出所有以这N个点为顶点的三角形的面积和(N<=3000) [题解] 我们发现直接枚举三个点计算会造成很大部分的叉积重复被计算, 因此我们枚举i,计算pj和pi点差的后缀和,我们发现对于固定边ij, 其与后面的枚举量相关贡献就为pj-pi和点差后缀和的叉积. 因此我们针对每个i进行后面数据的极角排序,O(n)计算与i相关的所有答案贡献. [代码]

[POJ2007]Scrambled Polygon(计算几何 极角排序)

题目链接:http://poj.org/problem?id=2007 题意:给出凸包和起点,逆序输出. 极角排序可以用反三角函数求出角度,按照角度排序.也可以用叉乘来做.注意题目说给定第一个数据是0,0,这是凸包的起点,数据中有在x轴负半轴的数据,所以排序的时候0,0要跳过.只排1~n-1个坐标. 1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstri