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 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 ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the numbers of tree vertexes connected by the i-th edge.

Each of the next n lines contain two space-separated integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — 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
Input
  1. 3
    1 3
    2 3
    0 0
    1 1
    2 0
Output
  1. 1 3 2
Input
  1. 4
    1 2
    2 3
    1 4
    -1 -2
    3 5
    -3 3
    2 0
Output
  1. 4 2 1 3
    【分析】题意比较简单。给你一棵n个节点的树,再给你几何平面内的n个点,没有三点共线,
    问你能不能用着n个点在几何平面内表现出来,线段除了顶点处无其他交点。
    由于没有3点共线的情况,所以解总是存在的。
    我们考虑以当前平面左下角的点p作为树根,对平面上的点以p做基准进行极角排序,则所有点与p点的连线都不会有除了p以外的交点。
    现在我们已经会填树根处的点了,对于树根的每个子节点,我们都可以递归的处理以其为根的子树,
    假设该子树包含x个节点,我们考虑以一根从p出发,长度不限的射线,从p的正下方开始按逆时针扫过去,
    直到扫过的平面包含x个节点即可停止。此时扫过的平面即为该子树应当处理的平面。
    每次处理需要先找到左下角的点,然后对当前平面的所有点进行排序,共需要处理n次,所以复杂度O(n^2*logn)。
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <string>
  7. #include <stack>
  8. #include <queue>
  9. #include <vector>
  10. #define inf 0x3f3f3f3f
  11. #define met(a,b) memset(a,b,sizeof a)
  12. #define pb push_back
  13. typedef long long ll;
  14. using namespace std;
  15. const int N = ;
  16. const int M = ;
  17. pair<ll,ll>ori;
  18. vector<ll>edg[N];
  19. ll n,m,k;
  20. ll sz[N],ans[N];
  21. struct man{
  22. ll x,y,id;
  23. bool operator < (const man & b) const {
  24. return (x-ori.first)*(b.y-ori.second) - (y-ori.second)*(b.x-ori.first) > ;
  25. }
  26. }p[N];
  27. void dfs1(ll u,ll fa){
  28. sz[u]=;
  29. for(int i=;i<edg[u].size();i++){
  30. ll v=edg[u][i];
  31. if(v==fa)continue;
  32. dfs1(v,u);
  33. sz[u]+=sz[v];
  34. }
  35. }
  36. void dfs2(ll u,ll fa,ll s,ll e){
  37. int pos;
  38. ll x,y;
  39. x=y=1e10;
  40. for(int i=s;i<=e;i++){
  41. if(p[i].x<x||((p[i].x==x)&&p[i].y<y)){
  42. x=p[i].x;y=p[i].y;pos=i;
  43. }
  44. }
  45. ori.first=x;ori.second=y;
  46. swap(p[s],p[pos]);
  47. sort(p+s+,p+e+);
  48. ans[p[s].id]=u;
  49. int cnt=;
  50. for(int i=;i<edg[u].size();i++){
  51. ll v=edg[u][i];
  52. if(v==fa)continue;
  53. dfs2(v,u,s++cnt,s++cnt+sz[v]-);
  54. cnt+=sz[v];
  55. }
  56. }
  57. int main() {
  58. ll T,u,v;
  59. scanf("%lld",&n);
  60. for(int i=;i<n;i++){
  61. scanf("%lld%lld",&u,&v);
  62. edg[u].pb(v);edg[v].pb(u);
  63. }
  64. for(int i=;i<n;i++){
  65. scanf("%lld%lld",&p[i].x,&p[i].y);
  66. p[i].id=i;
  67. }
  68. dfs1(,);
  69. dfs2(,,,n-);
  70. for(int i=;i<n;i++)printf("%lld ",ans[i]);printf("\n");
  71. return ;
  72. }

Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)的更多相关文章

  1. Codeforces Round #124 (Div. 2)

    A. Plate Game 如果可以放置一个圆的情况下,先手将圆放置在矩形正中心,那么根据对称性,先手只要放后手的对称的位置即可,也就是先手必胜,否则后手胜. B. Limit 讨论\(n,m\)的大 ...

  2. Codeforces Round #592 (Div. 2) D - Paint the Tree

    题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...

  3. Codeforces Round #329 (Div. 2) D. Happy Tree Party 树链剖分

    D. Happy Tree Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/p ...

  4. Codeforces Round #329 (Div. 2) D. Happy Tree Party LCA/树链剖分

    D. Happy Tree Party     Bogdan has a birthday today and mom gave him a tree consisting of n vertecie ...

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  6. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  7. Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树

    F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...

  8. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  9. Codeforces Round #200 (Div. 1) D Water Tree 树链剖分 or dfs序

    Water Tree 给出一棵树,有三种操作: 1 x:把以x为子树的节点全部置为1 2 x:把x以及他的所有祖先全部置为0 3 x:询问节点x的值 分析: 昨晚看完题,马上想到直接树链剖分,在记录时 ...

随机推荐

  1. 根据约束id名找到表名

    数据库运行删除某条数据出现 [Err] ORA-02292: 违反完整约束条件 (ITOUCH_FDA.FKC7DB45E29C5A81ED) - 已找到子集 根据ITOUCH_FDA.FKC7DB4 ...

  2. Win10的WSL很好用呀

    WSL全名是Windows Subsystem for Linux,是win10版本号16xx之后推出的开发者功能,提供了如原生linux版的体验. 最近最新的win10春季版1803出来了,安装了看 ...

  3. POJ1637:Sightseeing tour(混合图的欧拉回路)

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10581   Accepted: 4466 ...

  4. APP兼容性测试

    一.APP兼容性范围以及问题 1.硬件 各个硬件结构 2.软硬件之间 硬件dll库(C++) 软硬件之间的通信,各个厂商提供的ROM 3.软件 浏览器.操作系统.数据库.手机.功能兼容性(功能修改,二 ...

  5. java消息中间件入门

    消息中间件来解耦服务调用 比如1个登录系统,登录的话需要调用很多系统的其他服务,如果中间调用失败,可能会导致登录信息一致无法返回,同时也增加了系统的耦合度.而用消息中间件的话,则是不发送服务到其他系统 ...

  6. 如何根据pom.xml文件下载jar包

    遇到过这种情况:从网上下载了一个项目, 使用的maven, 但是我想要新建一个项目, 但是不需要使用maven. 但是我怎么样才能将他那个项目的所有引用的jar包给下载下载下来呢; 1.下载一个mav ...

  7. rest service技术选型

    MySql workbench下载 http://dev.mysql.com/downloads/workbench/ 最好的8个 Java RESTful 框架 http://colobu.com/ ...

  8. WebComponents四大天王教程

    Shadow Dom: http://www.html5rocks.com/zh/tutorials/webcomponents/shadowdom/ http://www.html5rocks.co ...

  9. 【Codeforces】849D. Rooter's Song

    [算法]模拟 [题意]http://codeforces.com/contest/849/problem/D 给定n个点从x轴或y轴的位置p时间t出发,相遇后按对方路径走,问每个数字撞到墙的位置.(还 ...

  10. [bzoj3231][SDOI2008]递归数列——矩阵乘法

    题目大意: 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj和 cj ...