码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-11-07 15:25:12      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:span   ever   +=   oss   examples   cond   distinct   The   efi   

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;
}

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

标签:span   ever   +=   oss   examples   cond   distinct   The   efi   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!